简体   繁体   English

减少ITextPDF中的图像大小(字节)

[英]Reduce image size (bytes) in ITextPDF

I'm using the itext PDF library to build a very image-intensive PDF document in Java. 我正在使用itext PDF库在Java中构建一个非常图像密集的PDF文档。 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. 原始源图像的分辨率非常高,我使用scaleToFit将图像渲染到我需要的大小。

The problem I have is that the PDF document is still very large. 我遇到的问题是PDF文档仍然很大。 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. 我的理解是包含整个原始高分辨率图像,我使用的缩放仅影响实际渲染,而不影响文件中包含的图像的大小。

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? 那么,这就是问题 - 如何通过缩小每个图像来减小PDF文件的大小? If I lose a little bit of image quality that's ok. 如果我失去了一点图像质量,那就没关系。 Rescaling the source images manually will be difficult. 手动重新调整源图像将很困难。

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 ,然后使用此处的提示进行缩放如何缩放BufferedImage

This gives me a BufferedImage — I then convert this into an iText image using 这给了我一个BufferedImage - 我然后使用它将其转换为iText图像

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

Where quality is currently 0.6. quality目前为0.6。 That's acceptable for the work I'm doing. 这对我正在做的工作是可以接受的。

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

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