您的位置:首页 > 编程语言 > Java开发

Java 使用 pdfbox 读取 PDF文件中的文本

2016-06-21 16:11 609 查看
下面我们直接使用pdf取得PDF文件中的文本,代码如下

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

/**
* 提取PDF文件中的文本
*
* @author Jason.Zhang06
*
*/
public class PdfConverterTxt {

/**
* 取得PDF文本
*
* @param path
* @return
*/
public static String getContent(String path) {
String result = "";
PDDocument document = null;
try {
InputStream is = new FileInputStream(path);
document = PDDocument.load(is);
PDFTextStripper stripper = new PDFTextStripper();
result = stripper.getText(document).trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != document) {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result.toLowerCase();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息