Add image into a word .docx document header using

2019-09-04 20:23发布

问题:

Can anyone please help me to add image into word document (.docx) header using Apache POI's XWPF component?.

I tried very hard, but unable to find solution.

回答1:

XWPFDocument doc = new XWPFDocument();

FileInputStream fis = new FileInputStream("/images/image.png"); 
XWPFParagraph par1 = doc.createParagraph();
XWPFRun runAppName = par1.createRun();  
runAppName.addPicture(fis, Document.PICTURE_TYPE_PNG, "image.png", 257, 185);

doc.write(new FileOutputStream(new File("/folder/document.docx")));