java如何將pdf轉(zhuǎn)換成image
本文實例為大家分享了java將pdf轉(zhuǎn)換image的具體代碼,供大家參考,具體內(nèi)容如下
首先使用了使用了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"); } }
但是其問題在于問題:
當(dāng)PDF文檔為180M大小時直接報解析異常
當(dāng)PDF頁數(shù)為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是提高生成的圖片質(zhì)量 encoder.setJPEGEncodeParam(param2); encoder.encode(tag); // JPEG編碼 out.close(); System.out.println("image in the page -->"+(i+1)); } } }
但是其問題在于問題: 當(dāng)pdf的版本不為1.4時,直接報錯:Expected 'xref' at start of table
pdfbox與pdfrenderer相比較來說,轉(zhuǎn)換的效率要低得多。200頁左右的pdf花費(fèi)的時間是后者的6倍左右。同時,對于中文字體的支持存在些問題。
但是對于卻不存在pdf版本不同無法轉(zhuǎn)換的問題。
pdfrenderer 不能轉(zhuǎn)換1.4以上版本,查找了解決辦法但是沒有找到。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf
- java利用jacob將word轉(zhuǎn)pdf
- java使用jacob實現(xiàn)word轉(zhuǎn)pdf
- java實現(xiàn)在pdf模板的指定位置插入圖片
- java實現(xiàn)PDF轉(zhuǎn)圖片的方法
- java實現(xiàn)PPT轉(zhuǎn)化為PDF
- Java將圖片組合成PDF文件的方法
- java實現(xiàn)Img與PDF相互轉(zhuǎn)換
- Java利用openoffice將doc、docx轉(zhuǎn)為pdf實例代碼
- Java實現(xiàn)PDF打印的解決方案
相關(guān)文章
Java根據(jù)開始時間和結(jié)束時間及周幾計算日期的示例代碼
在Java 7中,java.time包不存在,所以我們需要使用java.util.Calendar和java.util.Date類來實現(xiàn)類似的功能,這篇文章主要介紹了Java根據(jù)開始時間和結(jié)束時間及周幾計算出日期的示例代碼,需要的朋友可以參考下2024-06-06mybatis的mapper特殊字符轉(zhuǎn)移及動態(tài)SQL條件查詢小結(jié)
mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會報錯的需要轉(zhuǎn)移一下,本文給大家介紹了常見的條件查詢操作,對mybatis的mapper特殊字符及動態(tài)SQL條件查詢相關(guān)知識感興趣的朋友一起看看吧2021-09-09深入淺析ArrayList 和 LinkedList的執(zhí)行效率比較
這篇文章主要介紹了ArrayList 和 LinkedList的執(zhí)行效率比較的相關(guān)資料,需要的朋友可以參考下2017-08-08解決微服務(wù)下Mybatis?xml無效綁定問題及分析Invalid?bound?statement
這篇文章主要介紹了解決微服務(wù)下Mybatis?xml無效綁定問題及分析Invalid?bound?statement,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11Spring循環(huán)依賴之問題復(fù)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Spring的循環(huán)依賴什么時候會出現(xiàn)以及如何解決循環(huán)依賴,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-07-07