【问题标题】:Error while retrieving images from pdf using Itext使用 Itext 从 pdf 检索图像时出错
【发布时间】:2015-08-12 10:22:52
【问题描述】:

我有一个现有的PDF,我想从中检索图像

注意:

在文档中,这是 RESULT 变量

public static final String RESULT = "results/part4/chapter15/Img%s.%s";

我不明白为什么需要这张图片?我只想从我的PDF 文件中提取图片

所以现在当我使用MyImageRenderListener listener = new MyImageRenderListener(RESULT);

我收到错误消息:

results\part4\chapter15\Img16.jpg(系统 找不到指定的路径)

这是我的代码。

    package part4.chapter15;

    import java.io.IOException;


    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.parser.PdfReaderContentParser;

    /**
     * Extracts images from a PDF file.
     */
    public class ExtractImages {

    /** The new document to which we've added a border rectangle. */
    public static final String RESOURCE = "resources/pdfs/samplefile.pdf";
    public static final String RESULT = "results/part4/chapter15/Img%s.%s";
    /**
     * Parses a PDF and extracts all the images.
     * @param src the source PDF
     * @param dest the resulting PDF
     */
    public void extractImages(String filename)
        throws IOException, DocumentException {
        PdfReader reader = new PdfReader(filename);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        MyImageRenderListener listener = new MyImageRenderListener(RESULT);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            parser.processContent(i, listener);
        }
        reader.close();
    }

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

【问题讨论】:

标签: java pdf itext pdf-parsing


【解决方案1】:

你有两个问题,第一个问题的答案是第二个问题答案的关键。

问题 1:

你指的是:

public static final String RESULT = "results/part4/chapter15/Img%s.%s";

你问:为什么需要这张图片?

这个问题是错误的,因为Img%s.%s 不是图像的文件名,而是图像文件名的模式。解析时,iText 将检测 PDF 中的图像。这些图像存储在编号的对象中(例如对象 16),这些图像可以以不同的格式导出(例如 jpg、png、...)。

假设图像存储在对象 16 中,并且该图像是 jpg,则该模式将解析为 Img16.jpg

问题 2:

为什么会报错:

results\part4\chapter15\Img16.jpg(系统找不到指定的路径)

在您的 PDF 中,对象 16 中存储了一个 jpg。您要求 iText 使用以下路径存储该图像:results\part4\chapter15\Img16.jpg(如我对 问题 1 的回答中所述)。但是:您的工作目录没有子目录results\part4\chapter15\,因此会抛出IOException(或FileNotFoundException?)。

一般问题是什么?

您复制/粘贴了我为“iText in Action - Second Edition”一书编写的 ExtractImages 示例,但是:

  1. 你没有读过那本书,所以你不知道这段代码应该做什么。
  2. 您并没有告诉 StackOverflow 上的读者此示例依赖于 MyImageRenderer 类,而这正是所有魔法发生的地方。

如何解决您的问题?

选项 1:

像这样更改RESULT

public static final String RESULT = "Img%s.%s";

现在图像将存储在您的工作目录中。

选项 2:

改编MyImageRenderer类,更具体地说是这个方法:

public void renderImage(ImageRenderInfo renderInfo) {
    try {
        String filename;
        FileOutputStream os;
        PdfImageObject image = renderInfo.getImage();
        if (image == null) return;
        filename = String.format(path,
            renderInfo.getRef().getNumber(), image.getFileType());
        os = new FileOutputStream(filename);
        os.write(image.getImageAsBytes());
        os.flush();
        os.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

每当遇到图像时,iText 都会调用此类。它将ImageRenderInfo 传递给此方法,其中包含有关该图像的大量信息。

在这个实现中,我们将图像字节存储为一个文件。这就是我们创建该文件路径的方式:

String.format(path,
     renderInfo.getRef().getNumber(), image.getFileType())

如您所见,RESULT 中存储的模式的使用方式是将第一次出现的 %s 替换为数字,第二次出现的替换为文件扩展名。

您可以轻松调整此方法,以便将图像作为byte[] 存储在List 中,如果这是您想要的。

【讨论】:

  • 嗨 Bruno.. 非常感谢,我完全迷失了,因为我更像是一个 PHP 开发人员,我解决了它
  • 请下载免费电子书The Best iText Questions on StackOverflow,以便将最常见的 iText 问题集中在一处。例如How to get the co-ordinates of an image? 告诉您如何在页面上找到图像的宽度和高度。您需要java.awt.BufferedImage 才能找到以像素为单位的大小。
  • 嗨 Bruno,这是我使用矩阵结果所做的,xPosition = matrix.get(Matrix.I31); yPosition = matrix.get(Matrix.I32);width = matrix.get(Matrix.I11);height = matrix.get(Matrix.I22); 所以我只需将 x-DPI 的宽度/xPosition 和 y-DPI 的高度/yPosition 相除?
  • 不,请使用常识,您会明白xPositionyPosition 与分辨率无关。想想看:移动图像会改变分辨率吗?当然不是!只需按照我的建议并使用BufferedImage 来获取像素数。
  • 我像这样使用了 BufferedImage PdfImageObject image = renderInfo.getImage(); BufferedImage bi = image.getBufferedImage(); 但我收到了这个错误:不支持的图像类型。如何将图像传递给getBufferedImage 函数?
猜你喜欢
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 2011-12-05
  • 2013-12-01
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
相关资源
最近更新 更多