iTextsharp - PDF file size after inserting image

16,672

Solution 1

The answer appears to have been that you need to set an appropriate version of the PDF spec to target and then set the compression as follows:

PdfWriter writer = PdfWriter.GetInstance(doc, ms);
PdfContentByte contentPlacer;

writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_5);

writer.CompressionLevel = PdfStream.BEST_COMPRESSION;

This has brought my file size down considerably. I also found that PNG's were giving me the best results as regards to final size of document.

Solution 2

I did some experiments this morning. My test image was 800x600 with a file size of 100.69K when saved as a PNG. I inserted this into a PDF (using iTextSharp and the usual GetInstance() method) and the file size increased from 301.71K to 402.63K. I then re-saved my test image as a raw bitmap with file size of 1,440,054. I inserted this into the PDF and the file size went DOWN to 389.81K. Interesting!

I did some research on the web for a possible explanation, and, based on what I found, it looks like iTextSharp does not compress images, but rather it compresses everything with some generic compression. So in other words, the BMP is not actually converted to another file type, it's just compressed very much like you would by ZIPping it. Whatever they're doing, it must be good, for it compressed better than the image with PNG compression. I assume iTextSharp woudld try to compress the PNG but would compress at 0% since it already is compressed. (This is inconsistent with the original author's observations, though... Paddy said his PDF size increased much more than the size of the PNG... not sure what to make of that. I can only go on my own experiments).

Conclusions:

1) I don't need to add some fancy library to my project to convert my (eventual dynamically-created) image to PNG; it actually does better to leave it totally uncompressed and let iTextSharp do all the compression work.

2) I also read stuff on the web about iTextSharp saving images at a certain DPI. I did NOT see this problem... I used ScalePercent() method to scale the bitmap to 1% and the file size was the same and there was no "loss" in the bitmap pixels in the bitmap... this confirms that iTextSharp is doing a simple, nice, generic lossless compression.

Share:
16,672
Paddy
Author by

Paddy

Updated on June 20, 2022

Comments

  • Paddy
    Paddy about 2 years

    I'm currently converting some legacy code to create PDF files using iTextSharp. We're creating a largish PDF file that contains a number of images, which I'm inserting like so:

    Document doc = new Document(PageSize.A4, 50, 50, 25, 25);
    PdfWriter writer = PdfWriter.GetInstance(doc, myStream);
    
    writer.SetFullCompression();
    
    doc.Open();
    
    Image frontCover = iTextSharp.text.Image.GetInstance(@"C:\MyImage.png");
    
    //Scale down from a 96 dpi image to standard itextsharp 72 dpi
    frontCover.ScalePercent(75f);
    
    frontCover.SetAbsolutePosition(0, 0);
    
    doc.Add(frontCover);
    
    doc.Close();
    

    Inserting an image (20.8 KB png file) seems to increase the PDF file size by nearly 100 KB.

    Is there a way of compressing the image before entry (bearing in mind that this needs to be of reasonable print quality), or of further compressing the entire PDF? Am I even performing any compression in the above example?