zoukankan      html  css  js  c++  java
  • pdf解析2

    1、使用PDFBox处理PDF文档

    PDF全称Portable Document Format,是Adobe公司开发的电子文件格式。这种文件格式与操作系统平台无关,可以在Windows、Unix或Mac OS等操作系统上通用。

    PDF文件格式将文字、字型、格式、颜色及独立于设备和分辨率的图形图像等封装在一个文件中。如果要抽取其中的文本信息,需要根据它的文件格式来进行解析。幸好目前已经有不少工具能帮助我们做这些事情,其中就有PDFBOX

    PDFBox是Java实现的PDF文档API库,提供PDF文档的一系列操作。例如创建、处理以及文档内容提取等功能,也包含了一些命令行实用工具。

    主要有以下特性:

    • PDF格式的文本抽取
    • 合并PDF文档
    • PDF文档的加密与解密
    • Lucene搜索引擎集成
    • 填充表单数据
    • 创建一个文本文件的PDF
    • 创建PDF页面图象
    • 打印PDF文档

    2、PDFBox的下载

    最常见的一种PDF文本抽取工具就是PDFBox了,访问网址http://sourceforge.net/projects/pdfbox/。读者可以在该网页下载其最新的版本。本书采用的是PDFBox-0.7.3版本。PDFBox是一个开源的Java PDF库,这个库允许你访问PDF文件的各项信息。

    3、在Eclipse中配置

    以下是在Eclipse中创建工程,并导入pdf工具类的过程

    (1)在Eclipse的workspace中创建一个普通的Java工程:pdfprj

    (2)把下载的PDFBox-0.7.3.zip解压。

    (3)进入external目录下,可以看到,这里包括了PDFBox所有用到的外部包。复制下面的Jar包到工程pdfprj的lib目录下(如还未建立lib目录,则先创建一个)。

    1.  bcmail-jdk14-132.jar
    2.  bcprov-jdk14-132.jar
    3.  checkstyle-all-4.2.jar
    4.  FontBox-0.1.0-dev.jar
    5.  lucene-core-2.0.0.jar

    然后再从PDFBox的lib目录下,复制PDFBox-0.7.3.jar到工程的lib目录下。

    (4)在工程上单击右键,在弹出的快捷菜单中选择“Build Path->Config Build Path->Add Jars”命令,把工程lib目录下面的包都加入工程的Build Path。

    4.使用PDFBox解析PDF内容

      抽取pdf文本内容

        

    Java代码  
    1. private PDDocument document = null;  
    2.     public static void main(String[] args) throws IOException {  
    3.         String file = "d:\\pdf\\pdf-type.pdf";  
    4.         PDFBOX parse = new PDFBOX();  
    5.         parse.openPDFFile(file);  
    6.         }  
    7.     public void openPDFFile(String file) throws IOException {  
    8.         InputStream is = null;  
    9.         File f = new File(file);  
    10.         is = new FileInputStream(f);  
    11.         this.document = this.parseDocument(is);  
    12.         //获取页数  
    13.         List pages = this.document.getDocumentCatalog().getAllPages();  
    14.         int pageSize = pages.size();  
    15.         System.out.println("pdf页数:"+pageSize);  
    16.         this.getPdfText();  
    17.   
    18.     }  
    19.     public PDDocument parseDocument(InputStream input) throws IOException {  
    20.         PDDocument document = PDDocument.load(input);  
    21.         if (document.isEncrypted()) {  
    22.             try {  
    23.                 document.decrypt("");  
    24.             } catch (CryptographyException e) {  
    25.                 // TODO Auto-generated catch block  
    26.                 e.printStackTrace();  
    27.             } catch (InvalidPasswordException e) {  
    28.                 // TODO Auto-generated catch block  
    29.                 e.printStackTrace();  
    30.             }  
    31.         }  
    32.         return document;  
    33.     }  
    34.      /* 
    35.       * 抽取pdf文本内容 
    36.       */  
    37.     public void getPdfText() throws IOException {  
    38.         PDFTextStripper stripper = new PDFTextStripper();  
    39.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(  
    40.                 "d:\\pdf-type.txt"));  
    41.         BufferedWriter bw = new BufferedWriter(osw);  
    42.         stripper.setShouldSeparateByBeads(true);  
    43.         stripper.writeText(document, bw);  
    44.         bw.close();  
    45.         document.close();  
    46.     }  

      抽取pdf文档信息:

    Java代码  
    1. public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";     
    2.      
    3. /**   
    4.  * 解析pdf文档信息   
    5.  * @param pdfPath   pdf文档路径   
    6.  * @throws Exception   
    7.  */    
    8. public static void pdfParse( String pdfPath, String imgSavePath ) throws Exception     
    9. {     
    10.     InputStream input = null;     
    11.     File pdfFile = new File( pdfPath );     
    12.     PDDocument document = null;     
    13.     try{     
    14.         input = new FileInputStream( pdfFile );     
    15.         //加载 pdf 文档     
    16.         document = PDDocument.load( input );     
    17.              
    18.         /** 文档属性信息 **/    
    19.         PDDocumentInformation info = document.getDocumentInformation();     
    20.         System.out.println( "标题:" + info.getTitle() );     
    21.         System.out.println( "主题:" + info.getSubject() );     
    22.         System.out.println( "作者:" + info.getAuthor() );     
    23.         System.out.println( "关键字:" + info.getKeywords() );     
    24.              
    25.         System.out.println( "应用程序:" + info.getCreator() );     
    26.         System.out.println( "pdf 制作程序:" + info.getProducer() );     
    27.              
    28.         System.out.println( "作者:" + info.getTrapped() );     
    29.              
    30.         System.out.println( "创建时间:" + dateFormat( info.getCreationDate() ));     
    31.         System.out.println( "修改时间:" + dateFormat( info.getModificationDate()));     
    32.     
    33.         /** 文档页面信息 **/    
    34.         PDDocumentCatalog cata = document.getDocumentCatalog();     
    35.         List pages = cata.getAllPages();     
    36.         int count = 1;     
    37.         for( int i = 0; i < pages.size(); i++ )     
    38.         {     
    39.             PDPage page = ( PDPage ) pages.get( i );     
    40.             if( null != page )     
    41.             {     
    42.                 PDResources res = page.findResources();     
    43.                      
    44.                 //获取页面图片信息     
    45.                 Map imgs = res.getImages();     
    46.                 if( null != imgs )     
    47.                 {     
    48.                     Set keySet = imgs.keySet();     
    49.                     Iterator it = keySet.iterator();     
    50.                     while( it.hasNext() )     
    51.                     {     
    52.                         Object obj =  it.next();     
    53.                         PDXObjectImage img = ( PDXObjectImage ) imgs.get( obj );     
    54.                         img.write2file( imgSavePath + count );     
    55.                         count++;     
    56.                     }     
    57.                 }     
    58.             }     
    59.         }     
    60.     }catch( Exception e)     
    61.     {     
    62.         throw e;     
    63.     }finally{     
    64.         if( null != input )     
    65.             input.close();     
    66.         if( null != document )     
    67.             document.close();     
    68.     }     
    69. }     
    70.      
    71. /**   
    72.  * 获取格式化后的时间信息   
    73.  * @param dar   时间信息   
    74.  * @return   
    75.  * @throws Exception   
    76.  */    
    77. public static String dateFormat( Calendar calendar ) throws Exception     
    78. {     
    79.     if( null == calendar )     
    80.         return null;     
    81.     String date = null;     
    82.     try{     
    83.         String pattern = DATE_FORMAT;     
    84.         SimpleDateFormat format = new SimpleDateFormat( pattern );     
    85.         date = format.format( calendar.getTime() );     
    86.     }catch( Exception e )     
    87.     {     
    88.         throw e;     
    89.     }     
    90.     return date == null ? "" : date;     
    91. }     
  • 相关阅读:
    react之setState面试题
    react之setState异步和同步问题
    react关于setState的使用
    antd框架tree树动态插入,解决新版Antd无法使用TreeNodes问题
    postman测试接口时参数为数组时怎么测试?
    你不知道的react更新状态this.setState细节点
    浅谈md5加密技术
    浅谈纯文本&&富文本&&Markdown区别
    【LeetCode】43. Multiply Strings
    【LeetCode】44. Wildcard Matching (2 solutions)
  • 原文地址:https://www.cnblogs.com/xgbzsc/p/2768451.html
Copyright © 2011-2022 走看看