2013-04-16 130 views
-2

我已經收到了來自服務器的pdf文件。我想以只讀方式打開該pdf文件。沒有編輯選項,所以我不能使用文件操作。如何閱讀Java中的PDF文件?

哪個庫最好能讀取pdf文件?

如何在Java中讀取pdf文件?

+0

使用iText或PDFBox。谷歌搜索你的標題導致這個鏈接http://stackoverflow.com/questions/10830583/how-to-read-pdf-file-in-java –

回答

0

使用PDFBOXiText

我用下面的代碼,而使用PDFBOX。

try 
    { 
    document = PDDocument.load(f); 
    document.getClass(); 
if(document.isEncrypted()) 
{ 
try 
{ 
document.decrypt(""); 
} 
catch(InvalidPasswordException e) 
{ 
System.err.println("Error: Document is encrypted with a password."); 
System.exit(1); 
} 
} 

PDFTextStripperByArea stripper = new PDFTextStripperByArea(); 
stripper.setSortByPosition(true); 
PDFTextStripper stripper = new PDFTextStripper(); 
String st = stripper.getText(document); 

System.out.println(st);