java - 如何在不调整图像大小的情况下压缩PDF?

标签 java pdf itext

我正在使用 iText 来压缩现有的 pdf。 我正在使用 iText in Action book(第二版)中的示例:

package part4.chapter16;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.parser.PdfImageObject;

public class ResizeImage {

    /** The resulting PDF file. */
    public static String RESULT = "results/part4/chapter16/resized_image.pdf";
    /** The multiplication factor for the image. */
    public static float FACTOR = 0.5f;

    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException 
     */
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfName key = new PdfName("ITXT_SpecialId");
        PdfName value = new PdfName("123456789");
        // Read the file
        PdfReader reader = new PdfReader(SpecialId.RESULT);
        int n = reader.getXrefSize();
        PdfObject object;
        PRStream stream;
        // Look for image and manipulate image stream
        for (int i = 0; i < n; i++) {
            object = reader.getPdfObject(i);
            if (object == null || !object.isStream())
                continue;
            stream = (PRStream)object;
            if (value.equals(stream.get(key))) {
                PdfImageObject image = new PdfImageObject(stream);
                BufferedImage bi = image.getBufferedImage();
                if (bi == null) continue;
                int width = (int)(bi.getWidth() * FACTOR);
                int height = (int)(bi.getHeight() * FACTOR);
                BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                AffineTransform at = AffineTransform.getScaleInstance(FACTOR, FACTOR);
                Graphics2D g = img.createGraphics();
                g.drawRenderedImage(bi, at);
                ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
                ImageIO.write(img, "JPG", imgBytes);
                stream.clear();
                stream.setData(imgBytes.toByteArray(), false, PRStream.NO_COMPRESSION);
                stream.put(PdfName.TYPE, PdfName.XOBJECT);
                stream.put(PdfName.SUBTYPE, PdfName.IMAGE);
                stream.put(key, value);
                stream.put(PdfName.FILTER, PdfName.DCTDECODE);
                stream.put(PdfName.WIDTH, new PdfNumber(width));
                stream.put(PdfName.HEIGHT, new PdfNumber(height));
                stream.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
                stream.put(PdfName.COLORSPACE, PdfName.DEVICERGB);
            }
        }
        // Save altered PDF
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.close();
        reader.close();
    }

    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, DocumentException {

        new ResizeImage().manipulatePdf(src, dest);
    }
}

以上代码根据FACTOR减小图像的大小。我不想减小尺寸,而是更改 DPI 以减小图像尺寸。 任何帮助将不胜感激。我是java新手。 还有其他一些可用于压缩 PDF 的开源工具吗?

最佳答案

您从我的书中复制/粘贴了一个示例,但您似乎没有读过这本书,也没有真正尝试过该示例。你说“我不想缩小尺寸,而是改变DPI。”

嗯...这正是我书中的示例所做的!在 SpecialID例如,我创建了一个 PDF,其页面大小是使用以下矩形定义的:new Rectangle(400, 300)。这意味着我们有一个尺寸为 400 x 300 点的页面(请参见下面屏幕截图中的蓝点)。在此页面中,我添加了一张 500 x 332 像素的 JPG(请参见红点)。使用以下方法将该图像缩放为 400 x 300 点:

img.scaleAbsolute(400, 300);

此图像需要 55332 字节(绿点)。

enter image description here

请注意,我们可以轻松计算出 DPI:图像的宽度为 400 点;即 5.555 英寸。图像的高度为300点;那是 4.166 英寸。宽度的像素数为 500,因此 X 方向的 DPI 为 500 x 72/400 或 90 DPI。高度的像素数为 332,因此 DPI 为 332 x 72/300 或 79.68 DPI。

您想要通过降低分辨率来减少 JPG 的字节数。但是:您希望图像的大小保持不变:它仍然必须覆盖 400 x 300 点。

正是 ResizeImage 中所做的事情例如,您复制/粘贴到您的问题中。让我们看看里面:

enter image description here

图像的尺寸仍然为 400 x 300 点(这就是您想要的,不是吗?),但分辨率急剧下降:图像现在为 250 x 166 像素,而不是 500 x 332 像素,而不更改其尺寸页面!

现在让我们计算新的 DPI:在 X 方向,我们有 250 x 72/400。即 45 DPI。在 Y 方向,我们有 166 x 72/300。即 39.84 DPI。 这正是我们之前 DPI 的一半!这是巧合吗?当然不是!这就是我们使用的FACTOR。 (这都是一个简单的数学问题。)

由于分辨率降低,图像现在只需要 5343 字节(而不是原来的 55332 字节)。您已成功压缩图像。

简而言之:您误解了您使用的示例。你说它没有达到你想要的效果。我可以证明它确实有效;-)

从您对此答案发布的评论来看,您似乎混淆了图像分辨率、图像压缩等概念。为了消除这种混淆,您应该阅读以下问题和答案:

关于java - 如何在不调整图像大小的情况下压缩PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28367722/

相关文章:

java - 无法使用 iText 显示 unicode 0x0332

java - Spring Boot Actuator 应用程序无法在 Ubuntu VPS 上启动

java - 随机生成器始终生成数字 0

java - 如何扩展现有 PDF 的页面大小以在 iText 中添加页脚

java - 如何获取 iText7 上表单字段的字体大小?

c# - 如何使用 C# 将多个 A4 PDF 合并为 A3 PDF

java - 证书链不包含在签名的 pdf 中

java - 编译我的项目时是否编译依赖项本身?

java - 未能延迟初始化角色自定义 UserDetailsS​​ervice 的集合

java - iText:在 Java 中将 html 和 css 转换为 PDF