使用PDFBox处理PDF文档

2/10/2017来源:ASP.NET技巧人气:1682

使用PDFBox处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)

阅读:4462 时间:2015-01-26 20:07 分享: 7

项目需要在原有的PDF文件中插入图片、文字,并将最终的PDF文件转换为图片,在网上找了很多Demo,现在开源可以解析处理PDF文件的第三方插件比较多,eg:IText、PDFBox等,现在就PDFBox解析处理PDF文件总结如下:

 

【PDFBox简介】

自从Adobe公司1993年第一次发布公共PDF参考以来,支持各种语言和平台的PDF工具和类库就如雨后春笋般涌现。然而,java应用开发中Adobe技术的支持相对滞后了。这是个奇怪的现象,因为PDF文档是企业信息系统存储和交换信息的大势所趋,而Java技术特别适合这种应用。然而,Java开发人员似乎直到最近才获得成熟可用的PDF支持。   PDFBox(一个BSD许可下的源码开放项目)是一个为开发人员读取和创建PDF文档而准备的纯Java类库。它提供如下特性: 提取文本,包括Unicode字符。和Jakarta Lucene等文本搜索引擎的整合过程十分简单。加密/解密PDF文档。从PDF和XFDF格式中导入或导出表单数据。向已有PDF文档中追加内容。将一个PDF文档切分为多个文档。覆盖PDF文档。   PS:http://baike.baidu.com/link?url=TsYWHJtTPMhlf0UvKzPOk-j3f9KzF7morIa4CqoZ0s4yIDCLB3z8nLVgLHVz-AO4dE6S7ls_3_yuvXP03nLSiq  

【PDFBox下载】

最常见的一种PDF文本抽取工具就是PDFBox了,访问网址http://pdfbox.apache.org/download.cgi,进入如下图所示的下载界面。读者可以在该网页下载其最新的版本。本书采用的是pdfbox-1.8.8版本。PDFBox是一个开源的Java PDF库,这个库允许你访问PDF文件的各项信息。在接下来的例子中,将演示如何使用PDFBox提供的API操作PDF文件。

 

【将刚下载的7个jar包引入到工程当中】

pdfbox-1.8.8-src.zip为pdfbox源代码,里面有很对的例子,在pdfbox-1.8.8\examples目录下存在

 

【以下为Demo正式开始】

1、创建PDF文件

 

 1     public void createHelloPDF() {
 2         PDDocument doc = null;
 3         PDPage page = null;
 4 
 5         try {
 6             doc = new PDDocument();
 7             page = new PDPage();
 8             doc.addPage(page);
 9             PDFont font = PDType1Font.HELVETICA_BOLD;
10             PDPageContentStream content = new PDPageContentStream(doc, page);
11             content.beginText();
12             content.setFont(font, 12);
13             content.moveTextPositionByAmount(100, 700);
14             content.drawString("hello");
15 
16             content.endText();
17             content.close();
18             doc.save("F:\\java56班\\eclipse-SDK-4.2-win32\\pdfwithText.pdf");
19             doc.close();
20         } catch (Exception e) {
21             System.out.PRintln(e);
22         }
23     }

 

2、读取PDF文件:

 1     public void readPDF() {
 2         PDDocument helloDocument = null;
 3         try {
 4             helloDocument = PDDocument.load(new File(
 5                     "F:\\java56班\\eclipse-SDK-4.2-win32\\pdfwithText.pdf"));
 6             PDFTextStripper textStripper = new PDFTextStripper("GBK");
 7             System.out.println(textStripper.getText(helloDocument));
 8 
 9             helloDocument.close();
10         } catch (IOException e) {
11             // TODO Auto-generated catch block
12             e.printStackTrace();
13         }
14     }

3、修改PDF文件(处理中文乱码,我可以搞定的):

 1  /**
 2      * Locate a string in a PDF and replace it with a new string.
 3      *
 4      * @param inputFile The PDF to open.
 5      * @param outputFile The PDF to write to.
 6      * @param strToFind The string to find in the PDF document.
 7      * @param message The message to write in the file.
 8      *
 9      * @throws IOException If there is an error writing the data.
10      * @throws COSVisitorException If there is an error writing the PDF.
11      */
12     public void doIt( String inputFile, String outputFile, String strToFind, String message)
13         throws IOException, COSVisitorException
14     {
15         // the document
16         PDDocument doc = null;
17         try
18         {
19             doc = PDDocument.load( inputFile );
20 //            PDFTextStripper stripper=new PDFTextStripper("ISO-8859-1");
21             List pages = doc.getDocumentCatalog().getAllPages();
22             for( int i=0; i<pages.size(); i++ )
23             {
24                 PDPage page = (PDPage)pages.get( i );
25                 PDStream contents = page.getContents();
26                 PDFStreamParser parser = new PDFStreamParser(contents.getStream() );
27                 parser.parse();
28                 List tokens = parser.getTokens();
29                 for( int j=0; j<tokens.size(); j++ )
30                 {
31                     Object next = tokens.get( j );
32                     if( next instanceof PDFOperator )
33                     {
34                         PDFOperator op = (PDFOperator)next;
35                         //Tj and TJ are the two operators that display
36                         //strings in a PDF
37                         if( op.getOperation().equals( "Tj" ) )
38                         {
39                             //Tj takes one operator and that is the string
40                             //to display so lets update that operator
41                             COSString previous = (COSString)tokens.get( j-1 );
42                             String string = previous.getString();
43                             string = string.replaceFirst( strToFind, message );
44                             System.out.println(string);
45                             System.out.println(string.getBytes("GBK"));
46                             previous.reset();
47                             previous.append( string.getBytes("GBK") );
48                         }
49                         else if( op.getOperation().equals( "TJ" ) )
50                         {
51                             COSArray previous = (COSArray)tokens.get( j-1 );
52                             for( int k=0; k<previous.size(); k++ )
53                             {
54                                 Object arrElement = previous.getObject( k );
55                                 if( arrElement instanceof COSString )
56                                 {
57                                     COSString cosString = (COSString)arrElement;
58                                     String string = cosString.getString();
59                                     string = string.replaceFirst( strToFind, message );
60                                     cosString.reset();
61                                     cosString.append( string.getBytes("GBK") );
62                                 }
63                             }
64                         }
65                     }
66                 }
67                 //now that the tokens are updated we will replace the
68                 //page content stream.
69                 PDStream updatedStream = new PDStream(doc);
70                 OutputStream out = updatedStream.createOutputStream();
71                 ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
72                 tokenWriter.writeTokens( tokens );
73                 page.setContents( updatedStream );
74             }
75             doc.save( outputFile );
76         }
77         finally
78         {
79             if( doc != null )
80             {
81                 doc.close();
82             }
83         }
84     }

4、在PDF中加入图片:

 1 /**
 2      * Add an image to an existing PDF document.
 3      *
 4      * @param inputFile The input PDF to add the image to.
 5      * @param image The filename of the image to put in the PDF.
 6      * @param outputFile The file to write to the pdf to.
 7      *
 8      * @throws IOException If there is an error writing the data.
 9      * @throws COSVisitorException If there is an error writing the PDF.
10      */
11     public void createPDFFromImage( String inputFile, String image, String outputFile ) 
12         throws IOException, COSVisitorException
13     {
14         // the document
15         PDDocument doc = null;
16         try
17         {
18             doc = PDDocument.load( inputFile );
19 
20             //we will add the image to the first page.
21             PDPage page = (PDPage)doc.getDocumentCatalog().getAllPages().get( 0 );
22 
23             PDXObjectImage ximage = null;
24             if( image.toLowerCase().endsWith( ".jpg" ) )
25             {
26                 ximage = new PDJpeg(doc, new FileInputStream( image ) );
27             }
28             else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
29             {
30                 ximage = new PDCcitt(doc, new RandomaccessFile(new File(image),"r"));
31             }
32             else
33             {
34                 BufferedImage awtImage = ImageIO.read( new File( image ) );
35                 ximage = new PDPixelMap(doc, awtImage);
36             }
37             PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
38 
39             //contentStream.drawImage(ximage, 20, 20 );
40             // better method inspired by http://stackoverflow.com/a/22318681/535646
41             float scale = 0.5f; // reduce this value if the image is too large
42             System.out.println(ximage.getHeight());
43             System.out.println(ximage.getWidth());
44 //            ximage.setHeight(ximage.getHeight()/5);
45 //            ximage.setWidth(ximage.getWidth()/5);
46             contentStream.drawXObject(ximage, 20, 200, ximage.getWidth()*scale, ximage.getHeight()*scale);
47 
48             contentStream.close();
49             doc.save( outputFile );
50         }
51         finally
52         {
53             if( doc != null )
54             {
55                 doc.close();
56             }
57         }
58     }

5、PDF文件转换为图片:

 1 public void toImage() {
 2     try {
 3         PDDocument doc = PDDocument
 4                 .load("F:\\java56班\\eclipse-SDK-4.2-win32\\pdfwithText.pdf");
 5         int pageCount = doc.getPageCount();
 6         System.out.println(pageCount);
 7         List pages = doc.getDocumentCatalog().getAllPages();
 8         for (int i = 0; i < pages.size(); i++) {
 9             PDPage page = (PDPage) pages.get(i);
10             BufferedImage image = page.convertToImage();
11             Iterator iter = ImageIO.getImageWritersBySuffix("jpg");
12             ImageWriter writer = (ImageWriter) iter.next();
13             File outFile = new File("F:\\java56班\\eclipse-SDK-4.2-win32\\"
14                     + i + ".jpg");
15             FileOutputStream out = new FileOutputStream(outFile);
16             ImageOutputStream outImage = ImageIO
17                     .createImageOutputStream(out);
18             writer.setOutput(outImage);
19             writer.write(new IIOImage(image, null, null));
20         }
21         doc.close();
22         System.out.println("over");
23     } catch (FileNotFoundException e) {
24         // TODO Auto-generated catch block
25         e.printStackTrace();
26     } catch (IOException e) {
27         // TODO Auto-generated catch block
28         e.printStackTrace();
29     }
30 }

 

6、图片转换为PDF文件(支持多张图片转换为PDF文件):

 1 /**
 2      * create the second sample document from the PDF file format specification.
 3      * 
 4      * @param file
 5      *            The file to write the PDF to.
 6      * @param image
 7      *            The filename of the image to put in the PDF.
 8      * 
 9      * @throws IOException
10      *             If there is an error writing the data.
11      * @throws COSVisitorException
12      *             If there is an error writing the PDF.
13      */
14     public void createPDFFromImage(String file, String image)throws IOException, COSVisitorException {
15         // 多张图片转换为PDF文件
16         PDDocument doc = null;
17         doc = new PDDocument();
18         PDPage page = null;
19         PDXObjectImage ximage = null;
20         PDPageContentStream contentStream = null;
21 
22         File files = new File(image);
23         String[] a = files.list();
24         for (String string : a) {
25             if (string.toLowerCase().endsWith(".jpg")) {
26                 String temp = image + "\\" + string;
27                 ximage = new PDJpeg(doc, new FileInputStream(temp));
28                 page = new PDPage();
29                 doc.addPage(page);
30                 contentStream = new PDPageContentStream(doc, page);
31                 float scale = 0.5f;
32                 contentStream.drawXObject(ximage, 20, 400, ximage.getWidth()
33                         * scale, ximage.getHeight() * scale);
34                 
35                 PDFont font = PDType1Font.HELVETICA_BOLD;
36                 contentStream.beginText();
37                 contentStream.setFont(font, 12);
38                 contentStream.moveTextPositionByAmount(100, 700);
39                 contentStream.drawString("Hello");
40                 contentStream.endText();
41                 
42                 contentStream.close();
43             }
44         }
45         doc.save(file);
46         doc.close();
47     }

7、替换PDF文件中的某个字符串:

 1  /**
 2      * Locate a string in a PDF and replace it with a new string.
 3      *
 4      * @param inputFile The PDF to open.
 5      * @param outputFile The PDF to write to.
 6      * @param strToFind The string to find in the PDF document.
 7      * @param message The message to write in the file.
 8      *
 9      * @throws IOException If there is an error writing the data.
10      * @throws COSVisitorException If there is an error writing the PDF.
11      */
12     public void doIt( String inputFile, String outputFile, String strToFind, String message)
13         throws IOException, COSVisitorException
14     {
15         // the document
16         PDDocument doc = null;
17         try
18         {
19             doc = PDDocument.load( inputFile );
20 //            PDFTextStripper stripper=new PDFTextStripper("ISO-8859-1");
21             List pages = doc.getDocumentCatalog().getAllPages();
22             for( int i=0; i<pages.size(); i++ )
23             {
24                 PDPage page = (PDPage)pages.get( i );
25                 PDStream contents = page.getContents();
26                 PDFStreamParser parser = new PDFStreamParser(contents.getStream() );
27                 parser.parse();
28                 List tokens = parser.getTokens();
29                 for( int j=0; j<tokens.size(); j++ )
30                 {
31                     Object next = tokens.get( j );
32                     if( next instanceof PDFOperator )
33                     {
34                         PDFOperator op = (PDFOperator)next;
35                         //Tj and TJ are the two operators that display
36                         //strings in a PDF
37                         if( op.getOperation().equals( "Tj" ) )
38                         {
39                             //Tj takes one operator and that is the string
40                             //to display so lets update that operator
41                             COSString previous = (COSString)tokens.get( j-1 );
42                             String string = previous.getString();
43                             string = string.replaceFirst( strToFind, message );
44                             System.out.println(string);
45                             System.out.println(string.getBytes("GBK"));
46                             previous.reset();
47                             previous.append( string.getBytes("GBK") );
48                         }
49                         else if( op.getOperation().equals( "TJ" ) )
50                         {
51                             COSArray previous = (COSArray)tokens.get( j-1 );
52                             for( int k=0; k<previous.size(); k++ )
53                             {
54                                 Object arrElement = previous.getObject( k );
55                                 if( arrElement instanceof COSString )
56                                 {
57                                     COSString cosString = (COSString)arrElement;
58                                     String string = cosString.getString();
59                                     string = string.replaceFirst( strToFind, message );
60                                     cosString.reset();
61                                     cosString.append( string.getBytes("GBK") );
62                                 }
63                             }
64                         }
65                     }
66                 }
67                 //now that the tokens are updated we will replace the
68                 //page content stream.
69                 PDStream updatedStream = new PDStream(doc);
70                 OutputStream out = updatedStream.createOutputStream();
71                 ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
72                 tokenWriter.writeTokens( tokens );
73                 page.setContents( updatedStream );
74             }
75             doc.save( outputFile );
76         }
77         finally
78         {
79             if( doc != null )
80             {
81                 doc.close();
82             }
83         }
84     }

 

上述描述的只是PDFBox的部分功能,在原始资源包中有很对例子,大家可以学习,PDFBox API路径:http://pdfbox.apache.org/docs/1.8.8/javadocs/

转载自:爱编程w2bc.com