ITextsharp - PDF file size after inserting an image

I am currently converting some legacy codes for creating PDF files using iTextSharp. We create a large PDF file containing several images, which I insert as follows:

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(); 

The image insert (20.8 KB png file) seems to increase the size of the PDF file by almost 100 KB.

Is there a way to compress the image before entering (given that this should be of reasonable print quality) or further compress the entire PDF file? Am I even doing compression in the above example?

+4
source share
3 answers

Apparently, the answer is that you need to install the appropriate version of the PDF specification for purposes, 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 greatly reduced the size of my file. I also found that PNG gives me the best results regarding the final size of the document.

+7
source

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

I did some research on the Internet for a possible explanation, and based on what I found, it seems that iTextSharp is not compressing images, but rather compressing everything with some general compression. In other words, BMP is not actually converted to another type of file, it is simply compressed in the same way as in a ZIP archive. No matter what they do, this should be good, as it is compressed better than an image with PNG compression. I assume that iTextSharp woudld will try to compress PNG, but will compress by 0% since it is already compressed. (This is incompatible with the original comments of the author, although ... Paddy said that his size in PDF format is much larger than the size of PNG ... I'm not sure what to do with it. I can only continue my own experiments).

Conclusions:

1) I do not need to add any fancy library to my project in order to convert my (possible dynamically created) image to PNG; in fact, it's best to leave it completely uncompressed and let iTextSharp do all the compression work.

2) I also read material on the Internet about saving iTextSharp images in a specific DPI. I did NOT see this problem ... I used the ScalePercent () method to scale the bitmap to 1% and the file size was the same and there were no β€œlosses” in the bitmap pixels ... this confirms that iTextSharp does simple, pleasant, general lossless compression.

+1
source

It seems that PDF requires the png to be transcoded to something else, jpeg most likely.

see here: http://forums.adobe.com/message/2952201

The only thing I can think of is to first convert the png to the smallest jpeg, including 75% scaling, and then import this file without scaling.

0
source

All Articles