如何在Java中阅读PDF文件?

时间:2013-04-16 11:30:08

标签: java pdf

我收到了来自服务器的pdf文件。我想以只读方式打开该pdf文件。没有编辑选项。所以我不能使用文件操作。

哪个库最好阅读pdf文件?

如何在java中阅读pdf文件?

1 个答案:

答案 0 :(得分: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);