使用pdfbox解析pdf文档信息(属性,内容,图片)

来源:互联网 发布:unity 内存优化 编辑:程序博客网 时间:2024/04/29 04:12
Java代码 
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.InputStream;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Calendar;  
  6. import java.util.Iterator;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9. import java.util.Set;  
  10.   
  11. import org.apache.pdfbox.pdmodel.PDDocument;  
  12. import org.apache.pdfbox.pdmodel.PDDocumentCatalog;  
  13. import org.apache.pdfbox.pdmodel.PDDocumentInformation;  
  14. import org.apache.pdfbox.pdmodel.PDPage;  
  15. import org.apache.pdfbox.pdmodel.PDResources;  
  16. import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;  
  17. import org.apache.pdfbox.util.PDFTextStripper;  
  18.   
  19. /** 
  20.  * 使用 pdfbox 解析pdf 文档信息 
  21.  * @author longhuiping 
  22.  * 
  23.  */  
  24. public class PDFParse {  
  25.   
  26.     public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";  
  27.       
  28.     /** 
  29.      * 解析pdf文档信息 
  30.      * @param pdfPath   pdf文档路径 
  31.      * @throws Exception 
  32.      */  
  33.     public static void pdfParse( String pdfPath, String imgSavePath ) throws Exception  
  34.     {  
  35.         InputStream input = null;  
  36.         File pdfFile = new File( pdfPath );  
  37.         PDDocument document = null;  
  38.         try{  
  39.             input = new FileInputStream( pdfFile );  
  40.             //加载 pdf 文档  
  41.             document = PDDocument.load( input );  
  42.               
  43.             /** 文档属性信息 **/  
  44.             PDDocumentInformation info = document.getDocumentInformation();  
  45.             System.out.println( "标题:" + info.getTitle() );  
  46.             System.out.println( "主题:" + info.getSubject() );  
  47.             System.out.println( "作者:" + info.getAuthor() );  
  48.             System.out.println( "关键字:" + info.getKeywords() );  
  49.               
  50.             System.out.println( "应用程序:" + info.getCreator() );  
  51.             System.out.println( "pdf 制作程序:" + info.getProducer() );  
  52.               
  53.             System.out.println( "作者:" + info.getTrapped() );  
  54.               
  55.             System.out.println( "创建时间:" + dateFormat( info.getCreationDate() ));  
  56.             System.out.println( "修改时间:" + dateFormat( info.getModificationDate()));  
  57.           
  58.               
  59.             //获取内容信息  
  60.             PDFTextStripper pts = new PDFTextStripper();  
  61.             String content = pts.getText( document );  
  62.             System.out.println( "内容:" + content );  
  63.               
  64.               
  65.             /** 文档页面信息 **/  
  66.             PDDocumentCatalog cata = document.getDocumentCatalog();  
  67.             List pages = cata.getAllPages();  
  68.             int count = 1;  
  69.             forint i = 0; i < pages.size(); i++ )  
  70.             {  
  71.                 PDPage page = ( PDPage ) pages.get( i );  
  72.                 ifnull != page )  
  73.                 {  
  74.                     PDResources res = page.findResources();  
  75.                       
  76.                     //获取页面图片信息  
  77.                     Map imgs = res.getImages();  
  78.                     ifnull != imgs )  
  79.                     {  
  80.                         Set keySet = imgs.keySet();  
  81.                         Iterator it = keySet.iterator();  
  82.                         while( it.hasNext() )  
  83.                         {  
  84.                             Object obj =  it.next();  
  85.                             PDXObjectImage img = ( PDXObjectImage ) imgs.get( obj );  
  86.                             img.write2file( imgSavePath + count );  
  87.                             count++;  
  88.                         }  
  89.                     }  
  90.                 }  
  91.             }  
  92.         }catch( Exception e)  
  93.         {  
  94.             throw e;  
  95.         }finally{  
  96.             ifnull != input )  
  97.                 input.close();  
  98.             ifnull != document )  
  99.                 document.close();  
  100.         }  
  101.     }  
  102.       
  103.     /** 
  104.      * 获取格式化后的时间信息 
  105.      * @param dar   时间信息 
  106.      * @return 
  107.      * @throws Exception 
  108.      */  
  109.     public static String dateFormat( Calendar calendar ) throws Exception  
  110.     {  
  111.         ifnull == calendar )  
  112.             return null;  
  113.         String date = null;  
  114.         try{  
  115.             String pattern = DATE_FORMAT;  
  116.             SimpleDateFormat format = new SimpleDateFormat( pattern );  
  117.             date = format.format( calendar.getTime() );  
  118.         }catch( Exception e )  
  119.         {  
  120.             throw e;  
  121.         }  
  122.         return date == null ? "" : date;  
  123.     }  
  124.       
  125.     public static void main( String [] args ) throws Exception{  
  126.         pdfParse("f:/pdf/1.pdf","f:/pdfimg/");  
  127.     }  
  128. }  

原创粉丝点击