我的代码不是从所有PDF文件中提取图像

时间:2016-12-29 12:57:42

标签: java maven pdf itext

请帮助我,我的代码从PDF文件中提取图像,不是从所有PDF文件中提取图像。虽然它适用于某些PDF文件。请帮我。 这是我的代码: ExtractImages.java

package pdttotextconvertor;

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 {

    /**
     * PDF to extract images from
     */
    public static final String SOURCE_PDF = "C:/Latest Maven Code/pdttotextconvertor/src/main/resources/LC DPF example 1.pdf";

    /**
     * Parses a PDF and extracts all the images.
     *
     * @param filename the source PDF
     * @param destination the directory to save images
     */
    public void extractImages(String filename, String destination)
            throws IOException, DocumentException {
        System.out.println("Processing PDF at " + filename);
        System.out.println("Saving images to " + destination);

        PdfReader reader = new PdfReader(filename);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        ImageRenderListener listener = new ImageRenderListener(destination + "/Img%s.%s");
        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 {
        String sourcePDF = SOURCE_PDF;
        String destination = "target/images";
        if (args.length > 0) {
            sourcePDF = args[0];
            if (args.length > 1) {
                destination = args[1];
            }
        }

        new ExtractImages().extractImages(sourcePDF, destination);
    }
}

ImageRenderListener.java

package scannedPdfConvertor;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.PdfImageObject;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Saves images to a directory.
 *
 * @author mnguyen
 */
public class ImageRenderListener implements RenderListener {

    /**
     * The directory path to store images.
     */
    protected String path;

    /**
     * Creates a RenderListener that will look for images.
     */
    public ImageRenderListener(String path) {
        this.path = path;
    }

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
     */
    public void beginTextBlock() {
    }

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
     */
    public void endTextBlock() {
    }

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
     * com.itextpdf.text.pdf.parser.ImageRenderInfo)
     */
    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());
            System.out.println("Writing image to file: " + filename);
            os = new FileOutputStream(filename);
            os.write(image.getImageAsBytes());
            os.flush();
            os.close();
        } catch (IOException e) {
            Logger.getLogger(ImageRenderListener.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderText(
     * com.itextpdf.text.pdf.parser.TextRenderInfo)
     */
    public void renderText(TextRenderInfo renderInfo) {

    }
}

无效的Pdf文件链接是http://www.filedropper.com/lcdpfexample23

请提前帮助谢谢。

1 个答案:

答案 0 :(得分:1)

样本文件&#34; LC DPF示例2.3.pdf&#34;由OP共享既不包含位图图像也不包含文本。它只包含矢量图形。

E.g。我们读到的那封信&#34; P&#34;我们信中的第3页......&#34;在第一行实际上使用这些(m)ove,(l)ine,(c)urve,close(h)和(f)生病指令绘制:

71.16 804.07 m
71.16 806.83 l
72.12 806.83 l
72.72 806.83 73.2 806.83 73.56 806.83 c
73.8 806.95 74.04 807.07 74.28 807.19 c
74.52 807.43 74.76 807.67 74.88 807.91 c
75 808.27 75.12 808.63 75.12 809 c
75.12 809.59 75 810.07 74.76 810.43 c
74.4 810.79 74.04 811.03 73.68 811.15 c
73.44 811.15 72.84 811.27 72 811.27 c
69.72 811.27 l
69.72 804.07 l
71.16 804.07 l
h
71.88 810.07 m
72.36 810.07 72.72 809.95 72.84 809.95 c
73.08 809.95 73.32 809.83 73.44 809.71 c
73.56 809.47 73.68 809.23 73.68 809 c
73.68 808.75 73.56 808.63 73.56 808.51 c
73.44 808.27 73.2 808.15 73.08 808.15 c
72.84 808.03 72.48 808.03 71.88 808.03 c
71.16 808.03 l
71.16 810.07 l
71.88 810.07 l
h
f 

因此,OP实现的RenderListener接口不会找到任何东西(因为它只是侦听位图图像和文本)。

他可以实现ExtRenderListener,它还可以监听路径创建和渲染指令。不幸的是,这只会给他相当于上面显示的说明,而不是像#34这样的解释;这条填充的路径看起来就像一封信&#39; P&#39;&#34;。

因此,他要么必须自己实现这样的字符路径识别,要么改为使用OCR。