Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce size of RTF with embedded images?

We have some code which produces an RTF document from a RTF template. It is basically doing string search and replaces of special tags within the RTF file. This is accessible via a web page.

Typically, the processing time for this is really quick.

However, we need to embed an image within a template. We've been embedding these as JPEG images using Word's "Insert/Picture/From File..." functionality. But we've found that the resultant RTF file size is massively dependant upon the image.

For example, I've inserted a 20k JPEG logo (which is basically a solid background with some text). The RTF file increased in size from around 390k (without the image) to 510k (with the image).

Then we inserted a JPEG containing a screenshot, i.e. the image contains text, multiple colours, etc. The JPEG is around 150k. Using this image, the RTF file increased in size from 390k to 3.5MB.

So the encoding that Word uses for storing images into an RTF doesn't perform linearly. I'm guessing it is dependant upon what is in the JPEG image.

I need to keep the size of the RTF templates to a minimum to try and keep our file processing times to a minimum.

  • Does anyone have any ideas on how to minimize the size of the RTF files with embedded images?
  • Is there any way of controlling the encoding that Word uses? I can't see any options anywhere.
  • Does anyone know what type of binary encoding Word/RTF uses?

Thanks in advance.

like image 646
A_M Avatar asked Sep 10 '09 12:09

A_M


People also ask

How do I reduce the RTF file size that contains an image?

Another way is to convert the file to JPEG or PNG format, which are great options but have the drawback of not being able to be edited. If you need an RTF file that is smaller and can still be edited, you can use a program like Adobe Illustrator or Microsoft Word.

Why RTF files are so large?

Another disadvantage is that RTF files can be larger than other types of text files, such as plaintext files. This is because RTF files contain more information about the formatting of the document.

How do I compress a rich text file?

Steps for RTF Files Compression in C#Define folder path containing RTF files. Create Object of Archive. Using the Archive object, load file with a full path using its CreateEntry method. Call the Save() method and pass the compressed file name (full path) as a parameter.


2 Answers

An image in an RTF file gets stored as a WMF, uncompressed. On mac, it it would be macpict. Your best bet to keep the file size down is to link the image to the document rather than insert a copy in the document. The trade-off is that you have to keep the files together.

EDIT Is compressing the RTF an option? Using zip/rar, you'll get your file size back, but you'll have to uncompress, first obviously. There are supposed to be tools that will do rtf compression, but I have never used them.

like image 45
DaveParillo Avatar answered Sep 17 '22 20:09

DaveParillo


Here is the best solution

http://support.microsoft.com/kb/224663

Excerpt:

SYMPTOMS

When you save a Microsoft Word document that contains an EMF, PNG, GIF, or JPEG graphic as a different file format (for example, Word 6.0/95 (.doc) or Rich Text Format (.rtf)), the file size of the document may dramatically increase.

For example, a Microsoft Word 2000 document that contains a JPEG graphic that is saved as a Word 2000 document may have a file size of 45,568 bytes (44.5KB). However, when you save this file as Word 6.0/95 (.doc) or as Rich Text Format (.rtf), the file size may grow to 1,289,728 bytes (1.22MB).

CAUSE

This functionality is by design in Microsoft Word. If an EMF, a PNG, a GIF, or a JPEG graphic is inserted into a Word document, when the document is saved, two copies of the graphic are saved in the document. Graphics are saved in the applicable EMF, PNG, GIF, or JPEG format and are also converted to WMF (Windows Metafile) format.

RESOLUTION

Warning If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.

To prevent Word from saving two copies of the graphic in the document, and to reduce the file size of the document, add the ExportPictureWithMetafile=0 string value to the Microsoft Windows registry.

like image 151
swartbees Avatar answered Sep 17 '22 20:09

swartbees