2014-03-27 102 views
1

我有一個頂部有一些表的.docx文檔。這些包含需要替換的文本佔位符,它工作正常。但是,這些表格中的一個需要重複並填寫不同的值。我可以深度複製表格並將其添加到文檔的末尾,但我不知道如何將其插入到適當的位置。我嘗試了模板表的索引處添加副本,但給人一種「未知的圖形格式」的錯誤在LibreOffice中,甚至當我刪除原始:如何使用docx4j插入元素

template.getMainDocumentPart().getContent().add(index, copy); 
template.getMainDocumentPart().getContent().remove(table); 

有什麼想法?

編輯:

我創建了Windows中的示例項目,但現在我得到一個IndexOutOfBoundsException異常如表中不存在主文檔部分內容列表(它被包裹在一個的JAXBElement代替) 。請參見下面的代碼,這需要有三個單獨的表,其中的第一個具有與它的文本「第一」的小區,等

package test; 

import org.docx4j.XmlUtils; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.wml.ContentAccessor; 
import org.docx4j.wml.Tbl; 
import org.docx4j.wml.Text; 

import javax.xml.bind.JAXBElement; 
import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

public class Test { 

    private WordprocessingMLPackage template; 

    public void getTemplate(String name) { 
     try { 
      template = WordprocessingMLPackage.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(name)); 
     } catch (Exception e) { 
     } 
    } 

    private List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) { 
     List<Object> result = new ArrayList<Object>(); 
     if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue(); 
     if (obj.getClass().equals(toSearch)) 
      result.add(obj); 
     else if (obj instanceof ContentAccessor) { 
      List<?> children = ((ContentAccessor) obj).getContent(); 
      for (Object child : children) { 
       result.addAll(getAllElementFromObject(child, toSearch)); 
      } 
     } 
     return result; 
    } 

    public void duplicate() { 
     List<Object> tables = getAllElementFromObject(template.getMainDocumentPart(), Tbl.class); 
     for (Object table : tables) { 
      List list = template.getMainDocumentPart().getContent(); 
     // Workaround for table being wrapped in JAXBElement 
     // This simple code assumes table is present and top level 
     int index = 0; 
     for (Object o : list) { 
      if (XmlUtils.unwrap(o)== table) { 
       break; 
      } 
      index++; 
     } 
     List<Object> texts = getAllElementFromObject(table, Text.class); 
     for (Object t : texts) { 
      Text text = (Text) t; 
      if (text.getValue().contains("second")) { 
       Tbl copy = (Tbl) XmlUtils.deepCopy(table); 
       template.getMainDocumentPart().getContent().add(index, copy); 
       System.out.println(template.getMainDocumentPart().getXML()); 
       return;      
      } 
     }   } 
    } 

    public void save() { 
     try { 
      template.save(new File("out.docx")); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     Test test = new Test(); 
     test.getTemplate("test.docx"); 
     test.duplicate(); 
     test.save(); 
    } 

} 

文檔我不知道如何以最佳方式處理這個問題。

回答

0

聽起來像表中可能有圖像?你是從另一個docx複製它嗎?

如果您只有當前主文檔部分的克隆內容,並根據您的問題插入,那就沒有問題,並且不會導致您報告的錯誤。

+0

沒有任何圖像。現在我只是試圖複製其中一個表格,所以沒有第二個文件涉及。我會看看生成的XML。 – user3170702

+1

使用IndexOutOfBoundsException的簡單解決方法編輯您的代碼;它工作正常。 LibreOffice中的任何「未知圖形格式」錯誤要麼是不相關的,要麼是誤導性的。 – JasonPlutext

+0

謝謝傑森,工作正常! – user3170702