读取PDF文档内容
commit
cbee9c9b67
@ -0,0 +1,41 @@
|
|||||||
|
package devUtil.pdfUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.text.PDFTextStripper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关于PDF的一些工具
|
||||||
|
*/
|
||||||
|
public class PdfUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取PDF中的文字
|
||||||
|
*/
|
||||||
|
public String getPdfCharacter(String filePath, String fileName) {
|
||||||
|
String result = "";
|
||||||
|
try {
|
||||||
|
// 加载PDF文档
|
||||||
|
PDDocument document = PDDocument.load(new File(filePath + File.separator + fileName));
|
||||||
|
|
||||||
|
// 创建一个PDF文本提取器
|
||||||
|
PDFTextStripper pdfStripper = new PDFTextStripper();
|
||||||
|
|
||||||
|
// 提取文本
|
||||||
|
result = pdfStripper.getText(document);
|
||||||
|
|
||||||
|
// 关闭文档
|
||||||
|
document.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue