I've created a simple docx-Document, using Java POI 3.7. XWPF. Then, I added a picture by
using the Method XWPFDocument.addpicture(byte[] arg0, int arg1)
.
XWPFDocument docx = new XWPFDocument();
XWPFParagraph par = docx.createParagraph();
XWPFRun run = par.createRun();
run.setText("Hello, World. This is my first java generated docx-file. Have fun.");
run.setFontSize(13);
InputStream pic = new FileInputStream("logo.jpg");
byte [] picbytes = IOUtils.toByteArray(pic);
docx.addPicture(picbytes, Document.PICTURE_TYPE_JPEG);
Since file size of the docx grows, the picture was added "physically" to the document; but it is not displayed at all in MS Word. Seems like there is a reference to the picture missing in the document.
What's the way to do this? How is picture handling done with apache POI? And where are more tutorials on the web, there is nearly no documents or tutorials at all for XWPF, which explains handling of paragraphs, runs, etc.
The only thing I found about this is here: https://issues.apache.org/bugzilla/show_bug.cgi?id=49765 but it does not help at all.
Many thanks in advance.
Best Answer
I know this post is very old but still I am posting the answer so that all those who are searching for this answer can use it. For inserting the picture in the word document you have to write two programs. The first one is :-
Now here I have used "CustomeXwPFDocument" in this code and you will not get any imports through any jar file so you have to add another .java class in your package. the code for "CustomXWPFDocument" class goes like this:-
Use POI 3.9 jars for this program. The best URL is:- http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.9-20121203.zip
Now you are ready to fly. Best of luck.