使用PDFBox读取pdf文件

来源:互联网 发布:淘宝上出售游戏账号 编辑:程序博客网 时间:2024/04/29 23:08

简介

PDFBox是一个为开发人员读取和创建PDF文档而准备的纯Java类库。

导入相应的包

使用maven<dependency>    <groupId>org.apache.pdfbox</groupId>    <artifactId>pdfbox</artifactId>    <version>2.0.6</version></dependency>

下载网址

导入所需的包

或是直接上网址下载这几个jar包

代码

    private static String getContent(String path) throws Exception {        //创建输入流对象        FileInputStream fileInputStream = new FileInputStream(path);        //创建解析器对象        PDFParser pdfParser = new PDFParser(new RandomAccessBuffer(fileInputStream));        pdfParser.parse();        //pdf文档        PDDocument pdDocument = pdfParser.getPDDocument();        //pdf文本操作对象,使用该对象可以获取所读取pdf的一些信息        PDFTextStripper pdfTextStripper = new PDFTextStripper();        String content = pdfTextStripper.getText(pdDocument);        //PDDocument对象时使用完后必须要关闭        pdDocument.close();        return content;    }