用pdfbox 读取pdf文档
By:Roy.LiuLast updated:2017-08-03
pdfbox 是apache下一个开源的小项目,用来做与PDF相关的操作, 测试了一下简单的读取PDF文档,也有点意思,但对于生成PDF来说,没有itext强大。
maven的依赖
简单读取未加密的PDF文档代码
如果需要查看更多pdfbox的例子,可以参考这里:
https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/
maven的依赖
org.apache.pdfbox pdfbox 2.0.6
简单读取未加密的PDF文档代码
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import java.io.File;
import java.io.IOException;
public class ReadPdf {
public static void main(String[] args) throws IOException {
try (PDDocument document = PDDocument.load(new File("/path-to/abc.pdf"))) {
document.getClass();
if (!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
//System.out.println("Text:" + st);
// split by whitespace
String lines[] = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
System.out.println(line);
}
}
}
}
}
如果需要查看更多pdfbox的例子,可以参考这里:
https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/
From:一号门
Previous:java中用换行符分割字符串-兼容各种操作系统

COMMENTS