最近碰到需要解析pdf文件,将pdf文件中的文字,图片解析出来,例如文字解析放入word文档或者txt文本,图片单独提取出来,网上查询资料,较好的解决方案是使用PDFBox这个工具,也能较好的解决中文问题,不过有些pdf还是不能完全正确解析,和具体pdf文件有关,具体看以下代码,使用的PDFBox版本为0.7.3
1.解析文本:
2.使用PDFToImage.main(String[] args)方法提取图片,内容如果有中文会抛出异常
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
源码中的main方法注释
private static void usage()
{
System.err.println( "Usage: java org.pdfbox.ExtractImages [OPTIONS] <PDF file>/n" +
" -password <password> Password to decrypt document/n" +
" -prefix <image-prefix> Image prefix(default to pdf name)/n" +
" <PDF file> The PDF document to use/n"
);
System.exit( 1 );
}
3.将pdf转换为图片,例如jpg格式图片
源码中的main方法注释
private static void usage()
{
System.err.println( "Usage: java org.pdfbox.PDFToImage [OPTIONS] <PDF file>/n" +
" -password <password> Password to decrypt document/n" +
" -imageType <image type> (" + getImageFormats() + ")/n" +
" -outputPrefix <output prefix> Filename prefix for image files/n" +
" -startPage <number> The first page to start extraction(1 based)/n" +
" -endPage <number> The last page to extract(inclusive)/n" +
" <PDF file> The PDF document to use/n"
);
System.exit( 1 );
}