java 將pdf轉換image

來源:互聯網
上載者:User

首先使用了使用了apache的PDFBox組件1.8.4版本

package pdf;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Date;import java.util.List;import javax.imageio.ImageIO;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;public class PDFBox {@SuppressWarnings("rawtypes")public static void main(String[] args) throws IOException { String p=System.getProperty("user.dir") + "/"+"zk.pdf";            PDDocument doc = PDDocument.load(p);        int pageCount = doc.getNumberOfPages();        System.out.println(pageCount);        Date start = new Date();        try {        List pages = doc.getDocumentCatalog().getAllPages();            for(int i=0;i<pages.size();i++){                PDPage page = (PDPage) pages.get(i);                @SuppressWarnings("unused")int width = new Float(page.getTrimBox().getWidth()).intValue();                @SuppressWarnings("unused")int height = new Float(page.getTrimBox().getHeight()).intValue();                BufferedImage image = page.convertToImage();ImageIO.write(image, "jpg", new File("img" + File.separator + (i + 1) + ".jpg"));System.out.println("image in the page -->"+(i+1));            }} catch (Exception e) {e.printStackTrace();}finally{if(doc != null){doc.close();}}        Date end = new Date();        System.out.println(end.getTime()-start.getTime());        System.out.println("over");    }}


但是其問題在於 問題: 

當PDF文檔為180M大小時直接報解析異常 

當PDF頁數為500多頁時處理非常慢  

其後嘗試使用了pdf-renderer 1.0.5 版本 

package pdf;import java.awt.Image;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;import com.sun.pdfview.PDFFile;import com.sun.pdfview.PDFPage;public class PDFRenderer {public static void main(String[] args) throws IOException{String pdfRealePath=System.getProperty("user.dir") + "/"+"zk.pdf";File file = new File(pdfRealePath);RandomAccessFile raf = new RandomAccessFile(file, "r");FileChannel channel = raf.getChannel();MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,0, channel.size());PDFFile pdffile = new PDFFile(buf);for (int i = 1; i <= pdffile.getNumPages(); i++) {PDFPage page = pdffile.getPage(i);Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox().getWidth()), ((int) page.getBBox().getHeight()));Image img = page.getImage(rect.width, rect.height, rect, null,true,true);BufferedImage tag = new BufferedImage(rect.width, rect.height,BufferedImage.TYPE_INT_RGB);tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,null);FileOutputStream out = new FileOutputStream("img" + File.separator + (i + 1) + ".jpg"); // 輸出到檔案流JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag);param2.setQuality(1f, false);// 1f是提高產生的圖片品質encoder.setJPEGEncodeParam(param2);encoder.encode(tag); // JPEG編碼out.close();System.out.println("image in the page -->"+(i+1));}}}
但是其問題在於 問題: 當pdf的版本不為1.4時,直接報錯:Expected 'xref' at start of table


pdfbox與pdfrenderer相比較來說,轉換的效率要低得多。200頁左右的pdf花費的時間是後者的6倍左右。同時,對於中文字型的支援存在些問題。

但是對於卻不存在pdf版本不同無法轉換的問題。

pdfrenderer 不能轉換1.4以上版本,尋找瞭解決辦法但是沒有找到

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.