溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫(xiě)此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫(xiě)此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
                  近期在項(xiàng)目中使用到了大量的報(bào)表開(kāi)發(fā),需要將html頁(yè)面中的表格內(nèi)容導(dǎo)出到pdf word excel和圖片,前三者都比較好實(shí)現(xiàn)。唯獨(dú)后者生成圖片使用ImageIo操作時(shí)生成的圖片有點(diǎn)慘不忍睹。經(jīng)過(guò)大量google后發(fā)現(xiàn),pdfbox這個(gè)組件不錯(cuò),可以將pdf文件輕松生成圖片。這不問(wèn)題解決了,但在使用過(guò)程中不然,受到了很多致命性的打擊。pdfbox在處理中文pdf的時(shí)候就會(huì)表現(xiàn)的比較脆弱點(diǎn)。但對(duì)英文版的pdf導(dǎo)出圖片,那是杠杠的。盡管這樣,還是記錄一下,畢竟這方面的資料很少。我?guī)缀跛驯榱苏麄€(gè)google,baidu才搜集到那么一點(diǎn)點(diǎn)資料。這里跟大家分享下。
                  所依賴(lài)的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 雪山飛鵠 閱讀(11387) 評(píng)論(7)  編輯  收藏 所屬分類(lèi): javase

          Feedback

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

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

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2010-07-23 13:31 雪山飛鵠
          @cxh8318
          我說(shuō)過(guò)了,對(duì)于中文pdf目前那是相當(dāng)?shù)拇嗳酰W(wǎng)上搜了,說(shuō)是這是源代碼的bug,期待下一個(gè)版本能夠改進(jìn)過(guò)來(lái),但對(duì)英文版的pdf支持絕對(duì)完美,我在測(cè)試的時(shí)候?qū)⒂⑽陌娴膕pring參考手冊(cè)全部順利生成了jpg圖片,雖然控制臺(tái)有警告但不礙事的。比一般的工具軟件強(qiáng)悍多了。  回復(fù)  更多評(píng)論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2010-07-23 20:19 cxh8318
          恩,對(duì)英文pdf轉(zhuǎn)換確實(shí)很強(qiáng)  回復(fù)  更多評(píng)論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片[未登錄](méi) 2010-07-26 09:21 conjs
          我可以轉(zhuǎn),HTML DOC,XLS,PPT,PDF,JPG 都可以轉(zhuǎn)  回復(fù)  更多評(píng)論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片 2013-05-27 18:28 acmersch
          內(nèi)存溢出是什么問(wèn)題  回復(fù)  更多評(píng)論
            

          # re: 輕松使用apache pdfbox將pdf文件生成圖片[未登錄](méi) 2014-07-24 17:21 h
          姓名,,轉(zhuǎn)出來(lái)變成 姓姓,其他也是這樣。。重復(fù)第一個(gè)字  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 东山县| 山西省| 屏南县| 刚察县| 乐陵市| 云梦县| 青浦区| 宁蒗| 辉南县| 星子县| 松原市| 广丰县| 莲花县| 玉门市| 武穴市| 海晏县| 泉州市| 平凉市| 蓝山县| 孙吴县| 南丹县| 海晏县| 图木舒克市| 白玉县| 南皮县| 德清县| 永善县| 海淀区| 仙游县| 军事| 安塞县| 大城县| 循化| 勃利县| 榆社县| 台东县| 神木县| 怀集县| 青阳县| 迭部县| 昭平县|