2013-04-05 47 views
1

我們如何從docx4j中移除圖像。Docx4j - 文檔中的圖像

說我有10張,我想用我自己的字節數組/二進制數據,以取代8個圖像,我想刪除剩餘2

我也有在定位圖像的麻煩。

是否可以用圖像替換文檔中的文本佔位符?

回答

2

參考這個帖子:http://vixmemon.blogspot.com/2013/04/docx4j-replace-text-placeholders-with.html

for(Object obj : elemetns){ 
    if(obj instanceof Tbl){ 
     Tbl table = (Tbl) obj; 
     List rows = getAllElementFromObject(table, Tr.class); 
      for(Object trObj : rows){ 
     Tr tr = (Tr) trObj; 
     List cols = getAllElementFromObject(tr, Tc.class); 
     for(Object tcObj : cols){ 
      Tc tc = (Tc) tcObj; 
      List texts = getAllElementFromObject(tc, Text.class); 
      for(Object textObj : texts){ 
       Text text = (Text) textObj; 
        if(text.getValue().equalsIgnoreCase("${MY_PLACE_HOLDER}")){ 
         File file = new File("C:\\image.jpeg"); 
       P paragraphWithImage = addInlineImageToParagraph(createInlineImage(file)); 
         tc.getContent().remove(0); 

         tc.getContent().add(paragraphWithImage); 
       } 
        } 
      System.out.println("here"); 
     } 
      } 
     System.out.println("here"); 
    } 
} 

wordMLPackage.save(new java.io.File("C:\\result.docx")); 
+0

offcourse它不是perfet,我把它留給你,如果改變它,你有一天需要類似的解決方案 – 2013-04-05 10:36:16

1

查看docx4j checking checkboxes查找東西(XPath或非XPath遍歷)的兩種方法。

VariableReplace允許您替換文本佔位符,但不能替換圖像。我認爲可能存在一些代碼(在docx4j論壇中),它擴展它來做到這一點。

但我建議你使用內容控制數據綁定來代替。請參閱how to create a new word from template with docx4j

您可以在XML數據中使用base64編碼的圖像,而docx4j和/或Word將完成剩下的工作。

+0

我已經玩了它,而且發現了一個竅門做.. – 2013-04-05 10:11:25