#1 2016-03-14 22:59:58

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

About images compression in PDF files

Hello,

I'm creating a PDF file containing many images. The source for these images are usually a TGraphic or TBitmap, that I draw on the VCLCanvas directly. I have the following questions:
- Are all my images converted internally in JPEG by SynPdf? If not, what is the encoding rule?
- I found a property named ForceJPEGCompression. By setting it to 90, my resulting PDF is 2Kb heavier than an uncompressed PDF (of 3263Kb). Setting to 60, I gain only 31Kb. How this property should be used?
- Are the image streams compressed internally by the SynPdf, e.g. using the ZIP algorithm? If yes, is there a way to change the compression configuration?

Regards

Offline

#2 2016-03-15 07:32:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: About images compression in PDF files

The doc gives some information:

    /// this property can force saving all canvas bitmaps images as JPEG
    // - handle bitmaps added by VCLCanvas/TMetaFile and bitmaps added as TPdfImage
    // - by default, this property is set to 0 by the constructor of this class,
    // meaning that the JPEG compression is not forced, and the engine will use
    // the native resolution of the bitmap - in this case, the resulting
    // PDF file content will be bigger in size (e.g. use this for printing)
    // - 60 is the prefered way e.g. for publishing PDF over the internet
    // - 80/90 is a good ration if you want to have a nice PDF to see on screen
    // - of course, this doesn't affect vectorial (i.e. emf) pictures
    property ForceJPEGCompression: integer read fForceJPEGCompression write fForceJPEGCompression;

So default 0 value would let TBitmap be compressed using the deflate algorithm, and 7 as compression ratio - see TPdfStream.InternalWriteTo() method.
I guess changing this fixed value of 7 to 8 or 9 would not change the resulting file size, but make the compression much slower.

If setting ForceJPEGCompression to 60 does not change the file size, I guess because the file is not big due to pictures.
Or perhaps there are too many pictures.
You may try to reduce the number of pictures per page.

See then TPdfDocument.CreateOrGetImage method, which calls the TPdfImage.Create method:

    /// create the image from a supplied VCL TGraphic instance
    // - handle TBitmap and SynGdiPlus picture types, i.e. TJpegImage
    // (stored as jpeg), and TGifImage/TPngImage (stored as bitmap)
    // - use TPdfForm to handle TMetafile in vectorial format
    // - an optional DontAddToFXref is available, if you don't want to add
    // this object to the main XRef list of the PDF file
    constructor Create(aDoc: TPdfDocument; aImage: TGraphic; DontAddToFXref: boolean); reintroduce;

Offline

#3 2016-03-15 12:23:46

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Re: About images compression in PDF files

Hi ab,

Thank you for your reply, this confirms what I thought.

By the way, it allows me to point a small typo in the comments:

    // - 80/90 is a good ration if you want to have a nice PDF to see on screen

should be

    // - 80/90 is a good ratio if you want to have a nice PDF to see on screen

It's right?

Regards

Offline

#4 2016-03-15 13:42:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: About images compression in PDF files

Another ir-ration-al issue...

Offline

Board footer

Powered by FluxBB