需要jar包支持
fontbox-1.8.11.jars
pdfbox-1.8.11.jar
pdfbox-2.0.0-RC3.jar
--------------------------------------代码部分----------------------------------------
//解析pdf,入库
public Map analysisPdf(String path) {
Map map = new HashMap();
map.put("state", "success");
// 是否排序
boolean sort = false;
// 开始提取页数
int startPage = 16;
// 结束提取页数
int endPage = 25;
// 文件输入流,生成文本文件
// 内存中存储的PDF Document
PDDocument document = null;
try {
// 如果作为URL装载得到异常则从文件系统装载
// document = PDDocument.load("E:\\JCI医院.pdf");
document = PDDocument.load(path);
//判断pdf文件是否加密了
if (document.isEncrypted()) {
System.out.println("该文件是加密文件,不能打开......");
map.put("errorInfo", "pdf加密了,无法解析");
return map;
}
// PDFTextStripper来提取文本
PDFTextStripper stripper = new PDFTextStripper();
//取每页的数据
int pageNum = 16;
endPage= document.getPageCount();
for(int i=startPage;i<=endPage;i++){
// 设置是否排序
stripper.setSortByPosition(sort);
// 设置起始页
stripper.setStartPage(i);
// 设置结束页
stripper.setEndPage(i);
// 设置每页末尾字符串
// stripper.setPageEnd("\r\n======================"+pageNum+"========================\r\n");
//把每页的字符内容插入到数据库
String strText = stripper.getText(document);
// System.out.println(strText);
PdfContent upload = new PdfContent();
upload.setPageNum(pageNum);
upload.setContent(strText);
uploadPdfDao.save(upload);
pageNum++;
}
}catch (IOException e){
}
if (document != null) {
// 关闭PDF Document
try {
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return map;
}
解析pdf文档
最新推荐文章于 2024-07-22 20:26:49 发布