Hi,

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.

Thanks,
Anuj Sharma

Dani AI

Generated

Quick summary and correction to earlier posts: Apache POI can create and write modern Word files and embed images — use the XWPF (OOXML/.docx) API rather than expecting full image support in the legacy HWPF/.doc path. The XWPF run API actually attaches picture data and expects dimensions in EMUs (English Metric Units), so producing a valid .docx with embedded pictures is supported when the document package and relationships are created correctly. (poi.apache.org)

Why the file grew but Word asked for an encoding: that dialog usually appears when Word detects a mismatch between file bytes and the expected format (for example, writing OOXML bytes but saving with a .doc extension, or writing binary content via a text writer). Practical checklist that fixes 90% of cases seen in threads like this:

  • Use XWPFDocument and write a .docx file (not a .doc).
  • Write binary output (FileOutputStream or Files.newOutputStream), not a FileWriter/OutputStreamWriter.
  • Close the XWPFDocument and streams (try-with-resources).
  • When inserting images, give addPicture correct dimensions (POI expects EMUs; convert pixels with POI Units helpers). (support.microsoft.com)

If true legacy .doc output is required, note HWPF is limited and image/shape support is incomplete — switching to .docx is usually the simplest reliable fix; otherwise consider a dedicated commercial library or exporting screenshots to PDF. If problems continue, show the code that opens/writes the output stream and the exact filename/extension so the package vs. extension mismatch can be diagnosed. (stackoverflow.com)

Recommended Answers

All 3 Replies

You can write Word document with POI, you better to read documentation properly. Example

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);
try 
{
    document.write(outStream);
    outStream.close();
} 
catch (FileNotFoundException e) 
{
    e.printStackTrace();
} 
catch (IOException e) 
{
    e.printStackTrace();
}

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, networking, learning, and sharing knowledge.