0

尽管导入了所有必需的库,但在使用 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();
                } 
             }
    }
4

2 回答 2

0

谢谢你的哈迪克。正如你所说,我显然没有加载正确的 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;
于 2013-10-25T00:16:25.357 回答
0

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

您可以在此处搜索并解决您的问题,请参阅此获取 java.lang.NoClassDefFoundError: org/pdfbox/pdfparser/

于 2013-10-24T06:26:35.383 回答