Skip to content

Compression

galkahana edited this page Sep 14, 2010 · 5 revisions

By default, the PDF Library compresses all streams with Flate compression using zlib.
This is true except for TIFF Images which are compressed as CCIT, in which case they remain with this compression, to save the decoding-encoding time.
It is possible to control two aspects of the compression. one – whether compression takes place, two – what kind of compression.

Controlling compression

You can control whether streams are compressed or not with the PDFWriter object.
when Initializing the PDFWriter object you use the following method:
EStatusCode InitializePDFWriter(const wstring& inOutputFilePath, const LogConfiguration& inLogConfiguration, const PDFCreationSettings& inPDFCreationSettings)
The 3rd parameter is of type PDFCreationSettings (found in the header of PDFWriter), which has a single boolean member named CompressStreams. This member determines whether streams are compressed or not. For example:

PDFWriter pdfWriter;
pdfWriter.InitializePDFWriter(L"c:\\test.pdf",
                              LogConfiguration::DefaultLogConfiguration,
                              PDFCreationSettings(false));

The above code will shut down compression, note how PDFCreationSettings is created with false value in the constructor, to indicate that compression is not to happen.

Extending compression

It is possible to replace the compression mechanism. To do that you have to create an object that implements IByteWriter interface, which is the base for all Output streams. read about output stream in IO.
The new implementation, which you will provide, will be a stream object that is able to write compressed buffers to another stream. This way, the library will be able to provide the object the file stream to write to, and the implemented stream will only have to cover the compression itself.

Apart from implementing IByteWriter, you will also have to create an extender object for the ObjectsContext.

And Extender object for the ObjectsContext object implements IObjectsContextExtender.
You can read more about the methods of the extender in the “Extensibility” section in the help on the ObjectsContext here – The ObjectsContext object.
The extender implementation simply enables hooking between the new compression stream implementation and the actual stream that is being written to.

Therefore the only part that remains is to show how to set the extender object on the ObjectsContext. easy. check the following block:

IObjectsContextExtender* myObjectsExtender = new MyObjectsExtender();
pdfWriter.GetObjectsContext().SetObjectsContextExtender(myObjectsExtender);

The first line creates a new extender of the class MyObjectsExtender, which implements the IObjectsContextExtender interface. it then gets the ObjectsContext from the pdf writer object, and sets the extender on it using the SetObjectsContextExtender function.

Clone this wiki locally