【问题标题】:Unknown error with PDDocument declaration : PDFBoxPDDocument 声明的未知错误:PDFBox
【发布时间】:2013-10-24 06:11:36
【问题描述】:

尽管导入了所有必需的库,但在使用 PDDocument 和 PDFTextStripper 方法时出现错误。

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.edit.*;
import org.apache.pdfbox.pdmodel.graphics.xobject.*;
import java.io.*;

public class importingPDF {

    public static void main(String[] args) {
        PDDocument pd;
         BufferedWriter wr;
         try {
                 File input = new File("C:\\Invoice.pdf");  // The PDF file from where you would like to extract
                 File output = new File("C:\\SampleText.txt"); // The text file where you are going to store the extracted data
                 pd = PDDocument.load(input);
                 System.out.println(pd.getNumberOfPages());
                 System.out.println(pd.isEncrypted());
                 pd.save("CopyOfInvoice.pdf"); // Creates a copy called "CopyOfInvoice.pdf"
                 PDFTextStripper stripper = new PDFTextStripper();
                 wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
                 stripper.writeText(pd, wr);
                 if (pd != null) {
                     pd.close();
                 }
                // I use close() to flush the stream.
                wr.close();
         } catch (Exception e){
                 e.printStackTrace();
                } 
             }
    }

【问题讨论】:

    标签: java pdfbox


    【解决方案1】:

    这似乎是与不在类路径上的 jar 相关的问题。

    您可以在此处搜索相同类型的问题并解决您的问题,请参阅此Getting java.lang.NoClassDefFoundError: org/pdfbox/pdfparser/

    【讨论】:

      【解决方案2】:

      感谢哈迪克。正如你所说,我显然没有加载正确的 jar 文件。以下是必要的jar文件!

      import org.pdfbox.cos.COSDocument;
      import org.pdfbox.exceptions.COSVisitorException;
      import org.pdfbox.pdfparser.PDFParser;
      import org.pdfbox.pdmodel.PDDocument;
      import org.pdfbox.pdmodel.PDDocumentInformation;
      import org.pdfbox.util.PDFTextStripper;
      

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 2012-07-28
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        相关资源
        最近更新 更多