I have created an application which puts all the images at a user defined location in a rtf document. The code is working fine and the images are getting inserted.

However, if I open the document and add some text manually and save the file, the file size increases dramatically. For eg, initally the doc size was 277 Kb , but when I added some text, like , 'Hello' the file size increased to 40 MB.

Can someone please explain why? I am using the iText API for inserting the images.

      com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(listOfScreenshots[i].toString());
      image.scaleAbsolute(400, 300);
      doc.add(image);   
      log.createLog(listOfScreenshots[i]);

Dani AI

Generated

Short summary and practical next steps based on the thread: correctly identified that the large jump usually comes from what Word does after you open/save an RTF that contains images; Word can add a device/metafile representation that balloons the file. ’s suggestion to look at the raw file is on point — you want to confirm which bytes were added before deciding which fix to apply. (biblioscape.com)

Quick diagnostics (fast, low-risk)

  • Do a binary diff of the original vs. edited file (Windows: fc /b) to see where the new bytes appear; that will show whether a huge \pict block was added. (learn.microsoft.com)
  • Open the RTF in a hex editor or look for {\pict blocks in a text viewer; \pngblip/\jpegblip vs \wmetafile (or \emfblip) identifies the image copies. The RTF spec documents these picture control words. (biblioscape.com)

Practical fixes for a Java/server workflow

  • Best: stop producing RTF if you can. Generate DOCX (Apache POI / docx4j) or PDF instead — DOCX stores each image once in the ZIP package under word/media, avoiding the metafile duplication. If the target consumers accept DOCX/PDF, this is the simplest, most robust change. (deparkes.co.uk)
  • If you must emit RTF programmatically: use a library that gives you control over the RTF save options (for example, Aspose.Words lets you avoid writing metafile copies). That prevents Word from having to invent an uncompressed metafile fallback. Test the produced RTF in WordPad/Word to confirm size. (reference.aspose.com)
  • If you cannot change the generator nor the format, two last-resort options are available: (a) instruct/patch client machines (registry setting ExportPictureWithMetafile=0) to stop Word writing the metafile copy (requires admin/user consent), or (b) post-process the RTF to remove the unwanted \wmetafile/\emfblip pict blocks — but post-processing is fragile and must be tested against many Word versions. (jeffpar.github.io)

Recommendation (concise): for a server-side Java app, switch to DOCX or PDF (Apache POI/docx4j or iText for PDF) where images are stored once. If RTF is unavoidable, prefer an RTF writer that exposes the “save as metafile” option (or use Aspose) and validate output with a binary diff before deployment. (poi.apache.org)

Recommended Answers

All 7 Replies

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20 This is RTF\par
}

if I open the document and add some text manually and save the file

What program are you using? Open the file in another program like Notepad and see what it contains before and then after you add the text to see what is added to it.
The above is what a simple RTF file looks like in Notepad:

I open the RTF file in MS Word.

What am I looking for when I open it with notepad? The inital document had images in it and then some text is added. Why is the size increasing and how can it be resolved, if possible?

Look to see what has changed.

I have no idea what MS Word does.

What do I do when I find out what has changed? Finding out is in itself a challenge , because in notepad only garbled text is shown.

If you can find out what the extra bytes in the file are, that could be a clue to what the problem is.
Try making as small a file as possible for testing.

What do you mean by "garbled text"? I see ASCII text when I open a RTF file in notepad. See an earlier post I made with a sample.

The problem is that RTF file saves 2 copies of the same image. For better compatibility with Microsoft products, some RTF writers include the same picture in two different picture types in one RTF file:

1) one of the supported picture types (e.g. JPG or PNG) - it uses either the original format of the inserted graphics file (if this graphics file uses one of RTF-supported formats - such as PNG, JPG) or a RTF-supported picture type created by RTF writer in conversion from RTF-unsupported graphics file (e.g. conversion from BMP or GIF to PNG)
2) a Windows Metafile (WMF) copy of the original picture - for better compatibility with some Microsoft applications (e.g. Wordpad). The Windows Metafile copy is included without any compression.
This method increases the RTF file size rapidly. The RTF specification does not require this method and there are various implementations that include pictures without the WMF copy (e.g. Abiword or Ted).

Source : Wikipedia

So there is actually nothing that we can do about it i.e. if some body else has a better option. :(

Have a look at for the solution from Microsoft.

It worked for me !!!!

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.