简体   繁体   English

使用Java将.pdf doc或.png图像内容插入.docx文件

[英]Insert .pdf doc or .png image content into a .docx file using java

How can I insert pdf or png content into a docx file using java? 如何使用Java将pdf或png内容插入docx文件?

I've tried using Apache POI API in the following way, but it is not working (it generates some junk doc file): 我尝试以以下方式使用Apache POI API ,但无法正常工作(它会生成一些垃圾文档文件):

XWPFDocument doc = new XWPFDocument();  
String pdf = "D://capture1.pdf"; 
PdfReader reader = new PdfReader(pdf); 
PdfReaderContentParser parser = new PdfReaderContentParser(reader); 
for (int i = 1; i <= reader.getNumberOfPages(); i++) { 
  TextExtractionStrategy strategy = parser.processContent(i,new SimpleTextExtractionStrategy());    
  String text = strategy.getResultantText();        
  XWPFParagraph p = doc.createParagraph();  
  XWPFRun run = p.createRun();     
  run.setText(text);        
  run.addBreak(BreakType.PAGE);   
} 
FileOutputStream out1 = new FileOutputStream("D://javadomain1.docx");    
doc.write(out1);   
out1.close();   
reader.close();   
System.out.println("Document converted successfully"); 

You should be able to do it with POI, and you can certainly do it using docx4j. 您应该可以使用POI做到这一点,当然可以使用docx4j做到这一点。

Here's sample code for inserting an image using docx4j. 这是使用docx4j插入图像的示例代码

Note that to "insert a PDF", you need to OLE embed it. 请注意,要“插入PDF”,您需要OLE嵌入它。 That's more difficult, since you need to convert the PDF to a suitable binary OLE object. 这更加困难,因为您需要将PDF转换为合适的二进制OLE对象。 In docx4j, helper code for doing this is part of the commercial Enterprise edition. 在docx4j中,用于执行此操作的帮助程序代码是商业企业版的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM