iText打开PDF,无法读取内容

问题描述 投票:0回答:1
PdfReader reader = new PdfReader("E:\\document\\6imm5562e.pdf");
PdfDocument doc = new PdfDocument(reader);
String textFromPage = PdfTextExtractor.getTextFromPage(doc.getPage(1));
System.out.println(textFromPage);

错误消息如下:

请稍候...

如果此消息最终没有被文档的正确内容替代,则您的PDF查看器可能无法显示此类文档。

您可以通过访问http://www.adobe.com/go/reader_download升级到Windows®,Mac或Linux®的最新版本的Adobe Reader。

有关Adobe Reader的更多帮助,请访问http://www.adobe.com/go/acrreader

Windows是Microsoft Corporation在美国和/或其他国家的注册商标或商标。 Mac是商标Apple Inc.在美国和其他国家/地区注册的公司。 Linux是Linus Torvalds在美国和其他国家/地区的注册商标。

pdf itext
1个回答
0
投票

并不确定要提取什么数据,所以我将提供两种选择:

1]您要从PDF中提取静态文本(此解决方案需要使用pdfXFA来使用flatten method):

public void readXFA() throws IOException, InterruptedException {

    final XFAFlattener xfaFlattener = new XFAFlattener();
    xfaFlattener.flatten(new FileInputStream(INPUT_XFA),
            new FileOutputStream(FLATTENED));

    final PdfDocument doc = new PdfDocument(new PdfReader(FLATTENED));
    String textFromPage = PdfTextExtractor.getTextFromPage(doc.getPage(1));
    System.out.println(textFromPage);
}

此代码将首先展平您的表单,然后在展平的PDF文件上使用您的代码。

2)您要提取在表单上填写的数据(为此,您只需要iText 7 Core和方法getXfaFieldValue):

public void readXFA() throws IOException {
        final String INPUT_XFA = "c:\\temp\\imm5562e.pdf";

        try (PdfDocument pdf = new PdfDocument(new PdfReader(INPUT_XFA))) {
            XfaForm xfaForm = PdfAcroForm.getAcroForm(pdf, false).getXfaForm();
            System.out.println(xfaForm.getXfaFieldValue("TextField2"));
        }
    }

我想从技术上讲,您可以使用第一种方法提取两个数据,但随后您将失去XFA从表单提取数据的功能。

© www.soinside.com 2019 - 2024. All rights reserved.