【问题标题】:Unable to read content from PDF file using Aspose PDF in Java无法在 Java 中使用 Aspose PDF 从 PDF 文件中读取内容
【发布时间】:2018-05-04 21:06:07
【问题描述】:

我正在尝试使用 AsposePdf 在 PDF 文件中搜索字符串。

这就是我正在做的:

String path = "C:/Windows/Fonts";
List list = Document.getLocalFontPaths();
list.add(path);
Document.setLocalFontPaths(list);
Document pdfDocument = new Document("myFile.pdf");
PageCollection pages = pdfDocument.getPages();
TextAbsorber textAbsorber = new TextAbsorber
  (new TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Raw));  

for(int i = 1; i <= pages.size(); i++){
    Page currentPage = pdfDocument.getPages().get_Item(i);
    currentPage.accept(textAbsorber);
    String abText = textAbsorber.getText();
    String[] abArray = abText.trim().split("\n");
    for (String txtArray : abArray) {
         if (txtArray.contains("SomeText")) {
                //do something
              }
        }
 }

NullPointerException at:currentPage.accept(textAbsorber);

错误堆栈跟踪:

java.lang.NullPointerException
    at com.aspose.pdf.internal.p51.z11.m2(Unknown Source)
    at com.aspose.pdf.internal.p51.z11.m7(Unknown Source)
    at com.aspose.pdf.internal.p51.z13.m1(Unknown Source)
    at com.aspose.pdf.internal.p51.z13.m1(Unknown Source)
    at com.aspose.pdf.internal.p51.z13.m6(Unknown Source)
    at com.aspose.pdf.internal.p51.z13.<init>(Unknown Source)
    at com.aspose.pdf.internal.p51.z13.<init>(Unknown Source)
    at com.aspose.pdf.TextAbsorber.visit(Unknown Source)
    at com.aspose.pdf.Page.accept(Unknown Source)

可能是什么原因?

【问题讨论】:

    标签: java aspose aspose.pdf


    【解决方案1】:

    您无需从 PDF 文件中拆分或修剪字符串即可提取任何文本。 Aspose.PDF API 支持高效的文本提取。请尝试使用以下代码 sn-p 从 PDF 文档中提取文本。

    // Open document
    Document pdfDocument = new Document("input.pdf");
    
    // Create TextAbsorber object to find all instances of the input search phrase
    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("SEARCH STRING");
    
    // Accept the absorber for first page of document
    pdfDocument.getPages().accept(textFragmentAbsorber);
    
    // Get the extracted text fragments into collection
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
    
    // Loop through the Text fragments
    for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
        // Iterate through text segments
        for (TextSegment textSegment : (Iterable<TextSegment>) textFragment.getSegments()) {
            System.out.println("Text :- " + textSegment.getText());
        }
    }
    

    有关文本提取的更多信息,您可以访问Search and Get Text from Pages of a PDF Document。如果您遇到任何问题,请与我们分享源 PDF 文件,同时提及您要提取的文本。

    PS: 我与 Aspose 合作,担任开发人员宣传员。

    【讨论】:

    • 相同错误:com.aspose.pdf.internal.p51.z11.m2(未知来源)的 com.aspose.pdf.internal.p51 的线程“main”java.lang.NullPointerException 中的异常.z11.m7(Unknown Source) at com.aspose.pdf.internal.p51.z13.m1(Unknown Source) at com.aspose.pdf.internal.p51.z13.m1(Unknown Source) at com.aspose.pdf .internal.p51.z13.m6(Unknown Source) at com.aspose.pdf.internal.p51.z13.(Unknown Source) at com.aspose.pdf.internal.p51.z13.(Unknown来源)在 com.aspose.pdf.TextFragmentAbsorber.visit(未知来源)在 com.aspose.pdf.Page.accept(未知来源)
    • 这个过程和我提到的过程适用于大多数文件,但对于少数文件,Absorber 存在问题。是否与编码/字体问题有关?
    • 它看起来像一个文件特定的问题。请分享重现此异常的源文件,方法是将其上传到 Google Drive、Dropbox 等。然后我们将调查并与您分享我们的调查结果。此外,在共享请求的数据之前,请确保在您的环境中使用 Aspose.PDF for Java 18.4。
    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2011-04-21
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多