简体   繁体   English

iTextsharp - 插入图像后的 PDF 文件大小

[英]iTextsharp - PDF file size after inserting image

I'm currently converting some legacy code to create PDF files using iTextSharp.我目前正在转换一些遗留代码以使用 iTextSharp 创建 PDF 文件。 We're creating a largish PDF file that contains a number of images, which I'm inserting like so:我们正在创建一个较大的 PDF 文件,其中包含许多图像,我像这样插入:

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.插入图像(20.8 KB png 文件)似乎会使 PDF 文件大小增加近 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?有没有办法在输入之前压缩图像(记住这需要具有合理的打印质量),或者进一步压缩整个 PDF? Am I even performing any compression in the above example?我什至在上面的例子中执行了任何压缩吗?

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:答案似乎是您需要将 PDF 规范的适当版本设置为目标,然后按如下方式设置压缩:

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.我还发现 PNG 在文档的最终大小方面给了我最好的结果。

I did some experiments this morning.我今天早上做了一些实验。 My test image was 800x600 with a file size of 100.69K when saved as a PNG.我的测试图像为 800x600,保存为 PNG 时文件大小为 100.69K。 I inserted this into a PDF (using iTextSharp and the usual GetInstance() method) and the file size increased from 301.71K to 402.63K.我将它插入到 PDF 中(使用 iTextSharp 和常用的 GetInstance() 方法),文件大小从 301.71K 增加到 402.63K。 I then re-saved my test image as a raw bitmap with file size of 1,440,054.然后我将我的测试图像重新保存为文件大小为 1,440,054 的原始位图。 I inserted this into the PDF and the file size went DOWN to 389.81K.我将它插入到 PDF 中,文件大小下降到 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.我在网上做了一些研究以寻求可能的解释,并且根据我的发现,iTextSharp 似乎不压缩图像,而是使用一些通用压缩来压缩所有内容。 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.因此,换句话说,BMP 实际上并未转换为另一种文件类型,它只是像您通过 ZIP 压缩它一样进行压缩。 Whatever they're doing, it must be good, for it compressed better than the image with PNG compression.无论他们在做什么,它都必须是好的,因为它比使用 PNG 压缩的图像压缩得更好。 I assume iTextSharp woudld try to compress the PNG but would compress at 0% since it already is compressed.我假设 iTextSharp 会尝试压缩 PNG,但会以 0% 压缩,因为它已经被压缩了。 (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). (这与原作者的观察不一致,不过……Paddy 说他的 PDF 大小增加的比 PNG 大得多……不知道该怎么做。我只能继续我自己的实验)。

Conclusions:结论:

1) I don't need to add some fancy library to my project to convert my (eventual dynamically-created) image to PNG; 1)我不需要在我的项目中添加一些花哨的库来将我的(最终动态创建的)图像转换为 PNG; it actually does better to leave it totally uncompressed and let iTextSharp do all the compression work.实际上最好让它完全未压缩并让 iTextSharp 完成所有压缩工作。

2) I also read stuff on the web about iTextSharp saving images at a certain DPI. 2)我还在网上阅读了有关 iTextSharp 以特定 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.我没有看到这个问题...我使用 ScalePercent() 方法将位图缩放到 1% 并且文件大小相同并且位图中的位图像素没有“丢失”...这证实了 iTextSharp正在做一个简单、漂亮、通用的无损压缩。

It seems that PDF requires the png to be transcoded to something else, jpeg, most probably.似乎 PDF 需要将 png 转码为其他内容,很可能是 jpeg。

see here: http://forums.adobe.com/message/2952201请参阅此处: http : //forums.adobe.com/message/2952201

The only thing I can think of is to convert png to smallest jpeg first, including scaling down 75%, then importing that file without scaling.我唯一能想到的就是先将 png 转换为最小的 jpeg,包括缩小 75%,然后在不缩放的情况下导入该文件。

use:用:

var image = iTextSharp.text.Image.GetInstance(srcImage, ImageFormat.Jpeg);

image.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
//image.ScalePercent(75f);
image.SetAbsolutePosition(0, 0);
document.Add(image);
document.NewPage();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM