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

发布于 2024-09-15 18:33:05 字数 633 浏览 1 评论 0原文

我目前正在转换一些旧代码以使用 iTextSharp 创建 PDF 文件。我们正在创建一个较大的 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();

插入图像(20.8 KB png 文件)似乎会使 PDF 文件大小增加近 100 KB。

有没有办法在输入之前压缩图像(记住这需要具有合理的打印质量),或者进一步压缩整个 PDF?在上面的例子中我是否执行了任何压缩?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情绪失控 2024-09-22 18:33:05

答案似乎是您需要设置目标 PDF 规范的适当版本,然后按如下方式设置压缩:

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

writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_5);

writer.CompressionLevel = PdfStream.BEST_COMPRESSION;

这大大减小了我的文件大小。我还发现 PNG 在文档的最终大小方面给了我最好的结果。

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.

笛声青案梦长安 2024-09-22 18:33:05

我今天早上做了一些实验。我的测试图像在保存为 PNG 时为 800x600,文件大小为 100.69K。我将其插入到 PDF 中(使用 iTextSharp 和常用的 GetInstance() 方法),文件大小从 301.71K 增加到 402.63K。然后,我将测试图像重新保存为原始位图,文件大小为 1,440,054。我将其插入到 PDF 中,文件大小下降到 389.81K。有趣的!

我在网上做了一些研究以寻求可能的解释,并且根据我的发现,iTextSharp 似乎不压缩图像,而是通过一些通用压缩来压缩所有内容。换句话说,BMP 实际上并未转换为另一种文件类型,它只是被压缩,就像通过 ZIP 压缩它一样。无论他们在做什么,它一定是好的,因为它的压缩效果比 PNG 压缩的图像更好。我假设 iTextSharp 会尝试压缩 PNG,但会压缩到 0%,因为它已经被压缩了。 (不过,这与原作者的观察不一致......Paddy说他的PDF大小增加了比PNG大小多得多......不知道该怎么做。我只能继续我自己的实验)。

结论:

1)我不需要在我的项目中添加一些花哨的库来将我的(最终动态创建的)图像转换为 PNG;实际上,让它完全不压缩并让 iTextSharp 完成所有压缩工作会更好。

2)我还在网上阅读了有关 iTextSharp 以特定 DPI 保存图像的内容。我没有看到这个问题...我使用 ScalePercent() 方法将位图缩放到 1% 并且文件大小相同,并且位图中的位图像素没有“丢失”...这证实了 iTextSharp正在做一个简单、漂亮、通用的无损压缩。

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.

小…红帽 2024-09-22 18:33:05

似乎 PDF 需要将 png 转码为其他内容,很可能是 jpeg。

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

可以想到的是先将png转换为最小的jpeg,包括缩小75%,然后导入该文件而不进行缩放。

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

see here: 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.

逆蝶 2024-09-22 18:33:05

使用:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文