簡體   English   中英

我正在使用pdfbox-app-2.0.0-RC3但在PDF解析器中使用Rndo​​mAccessFile時仍然出現錯誤

[英]I am using pdfbox-app-2.0.0-RC3 but still I am getting Error while using RndomAccessFile in PDF parser

-您可以通過以下鏈接查看示例: http : //radixcode.com/pdfbox-example-code-how-to-extract-text-from-pdf-file-with-java/

import java.io.IOException;

public class JavaPDFTest {

    public static void main(String[] args) throws IOException {

       PDFManager pdfManager = new PDFManager();
       pdfManger.setFilePath("E:\test.pdf");
       System.out.println(pdfManager.ToText());       
    }    
}

import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class PDFManager {

   private PDFParser parser;
   private PDFTextStripper pdfStripper;
   private PDDocument pdDoc ;
   private COSDocument cosDoc ;

   private String Text ;
   private String filePath;
   private File file;

    public PDFManager() {

    }
   public String ToText() throws IOException
   {
       this.pdfStripper = null;
       this.pdDoc = null;
       this.cosDoc = null;

       file = new File(filePath);
       parser = new PDFParser(new RandomAccessFile(file,"r")); // update for PDFBox V 2.0

       parser.parse();
       cosDoc = parser.getDocument();
       pdfStripper = new PDFTextStripper();
       pdDoc = new PDDocument(cosDoc);
       pdDoc.getNumberOfPages();
       pdfStripper.setStartPage(1);
       pdfStripper.setEndPage(10);

       // reading text from page 1 to 10
       // if you want to get text from full pdf file use this code
       // pdfStripper.setEndPage(pdDoc.getNumberOfPages());

       Text = pdfStripper.getText(pdDoc);
       return Text;
   }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}

錯誤

線程“主”中的異常java.lang.ClassCastException:java.io.RandomAccessFile無法轉換為org.apache.pdfbox.io.RandomAccessRead
在aechaec.PDFManager.ToText(PDFManager.java:43)
在aechaec.AechAEC.main(AechAEC.java:25)
Java結果:1

是由安全特權引起的嗎? 因為我在Mac上使用Netbeans?

周圍有許多過時的示例,它們可能會起作用,也可能不會起作用。 請替換此代碼

file = new File(filePath);
parser = new PDFParser(new RandomAccessFile(file,"r"));
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);

這段代碼:

pdDoc = PDDocument.load(new File(filePath));
pdfStripper = new PDFTextStripper();

並更新到2.0的發行版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM