How to change image compression from RLE to flate in a PDF document.
📌 Overview
There is sometimes a requirement to recompress images to a different filter, often to make the PDF smaller. This article explains how to edit all images that use the RLE filter so that they use the flate decode filter instead.
📗 Instructions
The example below uses a custom transform (see Custom transforms) which can iterate through all the images on a page and replace the RLE filter images with flate decode images. It can also be amended to handle other filter types.
🪜 Steps
Create a custom transform: Override the
transformImage()
method.Create a new image: generate variables for constructing a new IDOMPDFImage using the flate decode filter. Here is a code snippet from the example below:
CPPIDOMPDFImage::create(jawsMako, reader, eDITFlate, IDOMPDFImage::FlateLZWParams::create(), colorspace, width, height, bps);
Apply custom transform to page(s): call
ICustomTransform::transform()
on each page in the document.
⌨️ Sample Code
Here is an example of a custom transform and how it can be used on each page in a document.
Recompression Custom Transform
This custom transform can then be used as follows:
Recompression recompression(jawsMako);
auto tf = ICustomTransform::create(jawsMako, &recompression);
for (uint8 i = 0; i < document->getNumPages(); i++)
{
auto newPage = document->getPage(i)->clone();
tf->transform(newPage->getContent(), changed);
newDocument->appendPage(newPage);
}
☑️ Conclusion
Changing image compression from RLE to flate in a PDF document can significantly reduce file size and improve performance. By using a custom transform to iterate through images and replace the RLE filter with the flate decode filter, users can efficiently manage image compression. This method not only optimizes the document but also provides flexibility to handle other filter types if needed.
📚 Additional Resources
If you need additional help, see our API documentation for detailed information on class/method usage, or raise a support ticket via our customer portal.