减少ITextPDF中的图像大小(字节) [英] Reduce image size (bytes) in ITextPDF

查看:1743
本文介绍了减少ITextPDF中的图像大小(字节)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用itext PDF库在Java中构建一个非常图像密集的PDF文档。每个页面上都有十几张图片。原始源图像的分辨率非常高,我使用 scaleToFit 将图像渲染到我需要的尺寸。

I'm using the itext PDF library to build a very image-intensive PDF document in Java. Each page has a dozen images on it. The original source images are very high resolution, and I'm using scaleToFit to render the image to the size I need.

我遇到的问题是PDF文档仍然很大。我的理解是包含整个原始高分辨率图像,我使用的缩放仅影响实际渲染,而不影响文件中包含的图像的大小。

The problem I have is that the PDF document is still very large. My understanding is that the entire original high resolution image is being included, and the scaling I'm using only affects the actual rendering, not the size of the image that's included in the file.

我通过删除缩放来验证这一点 - 页面是在高分辨率图像彼此重叠和页面边缘的情况下渲染的,而PDF与缩放到位时的大小相同。

I've verified this by removing the scaling — the pages were rendered with the high resolution images overlapping each other and the edge of pages, and the PDF was the same size as when the scaling was in place.

所以,这就是问题 - 如何通过缩小每个图像来减小PDF文件的大小?如果我失去了一点图像质量,那就没关系。手动重新调整源图像将很困难。

So, here's the question — how can I reduce the size of the PDF file by scaling down each image? If I lose a little bit of image quality that's ok. Rescaling the source images manually will be difficult.

推荐答案

所以我找到了一种方法。我现在将图像加载到 BufferedImage 中,然后使用此处的提示进行缩放:如何缩放BufferedImage

So I've found a way to do it. I now load the image into a BufferedImage, and then scale that using the hints found here: how do I scale a BufferedImage.

这给了我一个 BufferedImage - 然后我使用

This gives me a BufferedImage — I then convert this into an iText image using

        Image returnedImage = Image.getInstance ( pcb, bufferedImage, quality );

其中质量目前为0.6。这对我正在做的工作是可以接受的。

Where quality is currently 0.6. That's acceptable for the work I'm doing.

这篇关于减少ITextPDF中的图像大小(字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆