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]);

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 this 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.