相关文章推荐
深情的小刀  ·  GitHub ...·  10 月前    · 
大气的创口贴  ·  sql server get table ...·  1 年前    · 

PDFBox是Java实现的PDF文档协作类库,提供PDF文档的创建、处理以及文档内容提取功能,也包含了一些命令行实用工具。其主要特性包括:
1、提取PDF文件的Unicode文本
2、将PDF切分成多个PDF文件或合并多个PDF文件
3、从PDF表格中提取数据或填写PDF表格
4、验证PDF文件是否符合PDF/A-1b标准
5、使用标准的java API打印PDF文件
6、将PDF文件保存为图像文件,如PNG、JPEG
7、创建一个PDF文件,包含嵌入的字体和图像
8、PDF文件进行数字签名,即对PDF 文档进行加密与解密

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.util.PDFTextStripper;
import static readPDFContent.PDFParse.dateFormat;
 * @author Angela
public class PDFReader {
     * 获取格式化后的时间信息 
     * @param calendar   时间信息 
     * @return 
    public static String dateFormat( Calendar calendar ){  
        if( null == calendar )  
            return null;  
        String date = null;    
        String pattern = "yyyy-MM-dd HH:mm:ss";  
        SimpleDateFormat format = new SimpleDateFormat( pattern );  
        date = format.format( calendar.getTime() );  
        return date == null ? "" : date;  
        /**打印纲要**/
    public static void getPDFOutline(String file){
        try {  
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //加载 pdf 文档,获取PDDocument文档对象
            PDDocument document=PDDocument.load(fis);
            //获取PDDocumentCatalog文档目录对象
            PDDocumentCatalog catalog=document.getDocumentCatalog();
            //获取PDDocumentOutline文档纲要对象
            PDDocumentOutline outline=catalog.getDocumentOutline();
            //获取第一个纲要条目(标题1)
            PDOutlineItem item=outline.getFirstChild();
            if(outline!=null){
                //遍历每一个标题1
                while(item!=null){
                    //打印标题1的文本
                    System.out.println("Item:"+item.getTitle());
                    //获取标题1下的第一个子标题(标题2)
                    PDOutlineItem child=item.getFirstChild(); 
                    //遍历每一个标题2
                    while(child!=null){
                        //打印标题2的文本
                        System.out.println("    Child:"+child.getTitle());
                        //指向下一个标题2
                        child=child.getNextSibling();
                    //指向下一个标题1
                    item=item.getNextSibling();
            //关闭输入流
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
    /**打印一级目录**/
    public static void getPDFCatalog(String file){
        try {  
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //加载 pdf 文档,获取PDDocument文档对象
            PDDocument document=PDDocument.load(fis);
            //获取PDDocumentCatalog文档目录对象
            PDDocumentCatalog catalog=document.getDocumentCatalog();
            //获取PDDocumentOutline文档纲要对象
            PDDocumentOutline outline=catalog.getDocumentOutline();
            //获取第一个纲要条目(标题1)
            if(outline!=null){
                PDOutlineItem item=outline.getFirstChild();
                //遍历每一个标题1
                while(item!=null){
                    //打印标题1的文本
                    System.out.println("Item:"+item.getTitle());               
                    //指向下一个标题1
                    item=item.getNextSibling();
            //关闭输入流
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFBOXReader.class.getName()).log(Level.SEVERE, null, ex);
    /**获取PDF文档元数据**/
    public static void getPDFInformation(String file){
        try {  
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //加载 pdf 文档,获取PDDocument文档对象
            PDDocument document=PDDocument.load(fis);
            /** 文档属性信息 **/  
            PDDocumentInformation info = document.getDocumentInformation(); 
            System.out.println("页数:"+document.getNumberOfPages());
            System.out.println( "标题:" + info.getTitle() );  
            System.out.println( "主题:" + info.getSubject() );  
            System.out.println( "作者:" + info.getAuthor() );  
            System.out.println( "关键字:" + info.getKeywords() );             
            System.out.println( "应用程序:" + info.getCreator() );  
            System.out.println( "pdf 制作程序:" + info.getProducer() );  
            System.out.println( "Trapped:" + info.getTrapped() );  
            System.out.println( "创建时间:" + dateFormat( info.getCreationDate() ));  
            System.out.println( "修改时间:" + dateFormat( info.getModificationDate()));  
            //关闭输入流
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
    /**提取pdf文本**/
    public static void extractTXT(String file){
        try{
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //实例化一个PDF解析器
            PDFParser parser = new PDFParser(fis);
            //解析pdf文档
            parser.parse();
            //获取PDDocument文档对象
            PDDocument document=parser.getPDDocument();
            //获取一个PDFTextStripper文本剥离对象           
            PDFTextStripper stripper = new PDFTextStripper();
            //获取文本内容
            String content = stripper.getText(document); 
            //打印内容
            System.out.println( "内容:" + content );   
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
     * 提取部分页面文本
     * @param file pdf文档路径
     * @param startPage 开始页数
     * @param endPage 结束页数
    public static void extractTXT(String file,int startPage,int endPage){
        try{
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //实例化一个PDF解析器
            PDFParser parser = new PDFParser(fis);
            //解析pdf文档
            parser.parse();
            //获取PDDocument文档对象
            PDDocument document=parser.getPDDocument();
            //获取一个PDFTextStripper文本剥离对象           
            PDFTextStripper stripper = new PDFTextStripper();
            // 设置起始页
            stripper.setStartPage(startPage);
            // 设置结束页
            stripper.setEndPage(endPage);
            //获取文本内容
            String content = stripper.getText(document); 
            //打印内容
            System.out.println( "内容:" + content );   
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
     * 提取图片并保存
     * @param file PDF文档路径
     * @param imgSavePath 图片保存路径
    public static void extractImage(String file,String imgSavePath){
        try{
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //加载 pdf 文档,获取PDDocument文档对象
            PDDocument document=PDDocument.load(fis);           
            /** 文档页面信息 **/  
            //获取PDDocumentCatalog文档目录对象
            PDDocumentCatalog catalog = document.getDocumentCatalog();
            //获取文档页面PDPage列表
            List pages = catalog.getAllPages();  
            int count = 1;  
            int pageNum=pages.size();   //文档页数
            //遍历每一页
            for( int i = 0; i < pageNum; i++ ){  
                //取得第i页
                PDPage page = ( PDPage ) pages.get( i ); 
                if( null != page ){  
                    PDResources resource = page.findResources();                      
                    //获取页面图片信息 
                    Map<String,PDXObjectImage> imgs = resource.getImages();                    
                    for(Map.Entry<String,PDXObjectImage> me: imgs.entrySet()){
                        //System.out.println(me.getKey());
                        PDXObjectImage img = me.getValue();  
                        //保存图片,会自动添加图片后缀类型
                        img.write2file( imgSavePath + count );  
                        count++;  
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
     * 提取文本并保存
     * @param file PDF文档路径
     * @param savePath 文本保存路径
    public static void extractTXT(String file,String savePath){
        try{
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //实例化一个PDF解析器
            PDFParser parser = new PDFParser(fis);
            //解析pdf文档
            parser.parse();
            //获取PDDocument文档对象
            PDDocument document=parser.getPDDocument();
            //获取一个PDFTextStripper文本剥离对象           
            PDFTextStripper stripper = new PDFTextStripper();
            //创建一个输出流
            Writer writer=new OutputStreamWriter(new FileOutputStream(savePath));
            //保存文本内容
            stripper.writeText(document, writer);             
            //关闭输出流
            writer.close();
            //关闭输入流
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
     * 提取部分页面文本并保存
     * @param file PDF文档路径
     * @param startPage 开始页数
     * @param endPage 结束页数
     * @param savePath 文本保存路径
    public static void extractTXT(String file,int startPage,
            int endPage,String savePath){
        try{
            //打开pdf文件流
            FileInputStream fis = new   FileInputStream(file);
            //实例化一个PDF解析器
            PDFParser parser = new PDFParser(fis);
            //解析pdf文档
            parser.parse();
            //获取PDDocument文档对象
            PDDocument document=parser.getPDDocument();
            //获取一个PDFTextStripper文本剥离对象           
            PDFTextStripper stripper = new PDFTextStripper();
            //创建一个输出流
            Writer writer=new OutputStreamWriter(new FileOutputStream(savePath));
            // 设置起始页
            stripper.setStartPage(startPage);
            // 设置结束页
            stripper.setEndPage(endPage);
            //保存文本内容
            stripper.writeText(document, writer);             
            //关闭输出流
            writer.close();
            //关闭输入流
            document.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PDFReader.class.getName()).log(Level.SEVERE, null, ex);
    public static void main(String args[]){
        String file="F:\\pdf\\2013\\000608_阳光股份_2013年年度报告(更新后)_1.pdf";
        String savePath="E:\\result1.txt";
        long startTime=System.currentTimeMillis();
        extractTXT(file,savePath);
        long endTime=System.currentTimeMillis();
        System.out.println("读写所用时间为:"+(endTime-startTime)+"ms");

注意:加密的pdf文档上面的程序读不了,无法解析的pdf文档会报异常,印刷版和图片式的pdf文档无法提取出文本,无法解析。

PDFBox是一个用于创建和处理PDF文档的Java库。它可以使用Java代码创建、读取、修改和提取PDF文档中的内容。PDFBox的功能:Extract Text - 使用PDFBox,您可以从PDF文件中提取Unicode文本。Split & Merge - 使用PDFBox,您可以将单个PDF文件分成多个文件,并将它们合并为一个文件。Fill Forms - 使用PDFBox,您可以在文档中填写表单数据。Print - 使用PDFBox,您可以使用标准Java打印API打印PDF文件。 摘要:最近需要将一批PDF文件中的某些数据整理到Excel中,因为文件数量接近20w+,手动更新几乎不现实,于是就提取关键词和内容动手写了个Python小工具,以实现自动完成上述目标。 作者:yooongchun 微信公众号: yooongchun小屋 读取PDF文件找到特定关键字,然后读取其对应的数值提取出来 在Excel中查找对应关键字,然后在对应位置把上面提取出来的内容填进去 基本实现过程: 遍历文件夹,按照特定的要求找出指定类型的PDF文件 解析PDF文件 提取指定内容和对应值 更新数据到Excel 所需工具: 解析PDF文件的模块:pdfminer 操作Excel的模块:xlwt、xlrd、xlutils 注意:要在一个已经存在的Excel中写入数据需要配合xlutils使用,即先copy一个Excel对象,在该对象中进行写入,最后删除原对象而保存copy出来的对象 (5)、5执行效果,将多页的pdf文件进行一页一页分割。如上的示例代码,依次按照顺序执行main方法示例。(2)执行2效果:创建新文件且插入文字。(4)、4执行效果,合并pdf会出现2页。(3)、3执行效果,插入图片成功。在代码的如下位置引入该字体文件。 I am newbie in Apache PDFbox. I want to extract all bookmarks in PDF file using PDFBox library in Java. Any idea how to extract them?解决方案From the PrintBookmarks example in the source code downloadPDDo... PDF解析入门案例介绍注意点案例创建项目引入依赖读取PDF文本内容读取所有页,所有文本按页读取文本按坐标读取 Apache PDFBox是一个开源Java库,支持PDF文档的开发和转换。 使用此库,您可以开发用于创建,转换和操作PDF文档的Java程序。 PDFbox这个PDF处理类库,我使用过程中,能够满足我在一些场景中的需求,达成了我想要的效果,最后在此做一个使用demo的介绍,希望能够给大家带来帮助! Apache-PDFbox PDFvox-快速指南-WIKI PDF内容是按坐标进行定 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.OutputStreamWriter; Apache PDFBox库是一个用于处理PDF文档的开源Java工具。该项目允许创建新的PDF文档,操作现有PDF文档,并从PDF文档中提取内容。Apache PDFBox还包括几个命令行实用程序。 因为在apache官网上下载PDFBOX1.2jar包时,数据文件有丢失,未成功下载。所以用旧版本PDFBox-0.7.3的。 下载PDFBox-0.7.3后,可以找到需要的两个jar包,在不同的文件夹里。 1、准备两个jar包 PDFBox-0.7.3.jar,FontBox-0.1.0-dev.jar import org.pdfbox.pdmodel.PDDocument;im...