I created an application which allows a user to capture screenshots and save them as images at a pre-defined location. Now, I want to insert the saved images to a word document.

I am aware of Apache POI, but that is still under development and it seems that you can only read documents with POI and not write.

Can anyone please suggest how I can solve this problem. Please don't reccomend any open source tools.

Anuj Sharma

Recommended Answers

All 3 Replies

You can write Word document with POI, you better to read documentation properly. Example Writing Microsoft Word Documents in Java With Apache POI (Part 1 – Writing Paragraphs)

Class XWPFDocument has methods addPictureData(byte[] pictureData, int format) and addPictureData(java.io.InputStream is, int format)

If you do not want open source solution, then I suggest you google for paid products as I'm not gone advertise those.

I tried the following:

document.createParagraph().createRun().addPicture(new FileInputStream(saveLocation),document.PICTURE_TYPE_JPEG,imageName, 328, 247);
catch (FileNotFoundException e) 
catch (IOException e) 

I can see the document file size increase, however when I open the document it is asking me to "Select the encoding that makes your document readable".

Anu suggestions.

The method addPictureData only add the picture data in the word document, thats why we can see the document size increase.

However it does not show the picture becuase POI doesn't support it. iText is a better alternative.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.