溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
                  近期在項目中使用到了大量的報表開發,需要將html頁面中的表格內容導出到pdf word excel和圖片,前三者都比較好實現。唯獨后者生成圖片使用ImageIo操作時生成的圖片有點慘不忍睹。經過大量google后發現,pdfbox這個組件不錯,可以將pdf文件輕松生成圖片。這不問題解決了,但在使用過程中不然,受到了很多致命性的打擊。pdfbox在處理中文pdf的時候就會表現的比較脆弱點。但對英文版的pdf導出圖片,那是杠杠的。盡管這樣,還是記錄一下,畢竟這方面的資料很少。我幾乎搜遍了整個google,baidu才搜集到那么一點點資料。這里跟大家分享下。
                  所依賴的JAR:
                  commons-logging-1.1.1.jar
                  fontbox-1.2.1.jar
                  pdfbox-1.2.1.jar
                  示例代碼:
          /*
           * Licensed to the Apache Software Foundation (ASF) under one or more
           * contributor license agreements.  See the NOTICE file distributed with
           * this work for additional information regarding copyright ownership.
           * The ASF licenses this file to You under the Apache License, Version 2.0
           * (the "License"); you may not use this file except in compliance with
           * the License.  You may obtain a copy of the License at
           *
           *      
          http://www.apache.org/licenses/LICENSE-2.0
           *
           * Unless required by applicable law or agreed to in writing, software
           * distributed under the License is distributed on an "AS IS" BASIS,
           * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           * See the License for the specific language governing permissions and
           * limitations under the License.
           
          */

          package com.future.pdfbox.image;

          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.util.Iterator;
          import java.util.List;

          import javax.imageio.IIOImage;
          import javax.imageio.ImageIO;
          import javax.imageio.ImageWriter;
          import javax.imageio.stream.ImageOutputStream;

          import org.apache.pdfbox.pdmodel.PDDocument;
          import org.apache.pdfbox.pdmodel.PDPage;

          public class ExtractImages 
          {
              
          public static void main(String[] args) throws IOException 
                  PDDocument doc 
          = PDDocument.load("F:\\1.pdf");
                  
          int pageCount = doc.getPageCount(); 
                  System.out.println(pageCount); 
                  List pages 
          = doc.getDocumentCatalog().getAllPages(); 
                  
          for(int i=0;i<pages.size();i++){
                      PDPage page 
          = (PDPage)pages.get(i); 
                      BufferedImage image 
          = page.convertToImage(); 
                      Iterator iter 
          = ImageIO.getImageWritersBySuffix("jpg"); 
                      ImageWriter writer 
          = (ImageWriter)iter.next(); 
                      File outFile 
          = new File("C:/"+i+".jpg"); 
                      FileOutputStream out 
          = new FileOutputStream(outFile); 
                      ImageOutputStream outImage 
          = ImageIO.createImageOutputStream(out); 
                      writer.setOutput(outImage); 
                      writer.write(
          new IIOImage(image,null,null)); 
                  }

                  doc.close(); 
                  System.out.println(
          "over"); 
              }


          }


                  
          posted on 2010-07-23 08:46 雪山飛鵠 閱讀(11372) 評論(7)  編輯  收藏 所屬分類: javase

          Feedback

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2010-07-23 09:25 fengzl
          html轉pdf word excel和圖片不是那么容易的吧  回復  更多評論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2010-07-23 13:19 cxh8318
          對于中文的pdf支持是脆弱點嗎?我看壓根就不支持嘛  回復  更多評論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2010-07-23 13:31 雪山飛鵠
          @cxh8318
          我說過了,對于中文pdf目前那是相當的脆弱,網上搜了,說是這是源代碼的bug,期待下一個版本能夠改進過來,但對英文版的pdf支持絕對完美,我在測試的時候將英文版的spring參考手冊全部順利生成了jpg圖片,雖然控制臺有警告但不礙事的。比一般的工具軟件強悍多了。  回復  更多評論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2010-07-23 20:19 cxh8318
          恩,對英文pdf轉換確實很強  回復  更多評論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片[未登錄] 2010-07-26 09:21 conjs
          我可以轉,HTML DOC,XLS,PPT,PDF,JPG 都可以轉  回復  更多評論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2013-05-27 18:28 acmersch
          內存溢出是什么問題  回復  更多評論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片[未登錄] 2014-07-24 17:21 h
          姓名,,轉出來變成 姓姓,其他也是這樣。。重復第一個字  回復  更多評論
            

          主站蜘蛛池模板: 天祝| 射洪县| 布拖县| 贞丰县| 玛纳斯县| 会宁县| 通河县| 石河子市| 石狮市| 横山县| 郑州市| 阿勒泰市| 乐平市| 青铜峡市| 西华县| 竹北市| 泰顺县| 海丰县| 九龙县| 金湖县| 昌乐县| 安阳市| 海口市| 陕西省| 洞口县| 志丹县| 崇信县| 习水县| 重庆市| 绥宁县| 柳河县| 万全县| 井研县| 分宜县| 三江| 七台河市| 东乡| 霍山县| 鲁甸县| 咸宁市| 肃南|