您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關java如何實現截取PDF指定頁并進行圖片格式轉換功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、引入依賴
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.16</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fontbox</artifactId> <version>2.0.16</version> </dependency>
jar包下載地址:
https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox
2、實現DEMO
package com.dddpeter.app; import org.apache.pdfbox.multipdf.Splitter; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.image.BufferedImage; import java.io.*; import java.util.List; import java.util.ListIterator; public class PDFUtils { public static String splitPdf(int pageNum, String source, String dest) { File indexFile = new File(source); File outFile = new File(dest); PDDocument document = null; try { document = PDDocument.load(indexFile); // document.getNumberOfPages(); Splitter splitter = new Splitter(); splitter.setStartPage(pageNum); splitter.setEndPage(pageNum); List<PDDocument> pages = splitter.split(document); ListIterator<PDDocument> iterator = pages.listIterator(); while (iterator.hasNext()) { PDDocument pd = iterator.next(); if (outFile.exists()) { outFile.delete(); } pd.save(outFile); pd.close(); if (outFile.exists()) { return outFile.getPath(); } } document.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } public static void pdfFileToImage(File pdffile,String targetPath){ try { FileInputStream instream = new FileInputStream(pdffile); InputStream byteInputStream=null; try { PDDocument doc = PDDocument.load(instream); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); if (pageCount > 0) { BufferedImage image = renderer.renderImage(0, 4.0f); image.flush(); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut; imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(image, "png", imOut); byteInputStream = new ByteArrayInputStream(bs.toByteArray()); byteInputStream.close(); } doc.close(); } catch (IOException e) { e.printStackTrace(); } File uploadFile = new File(targetPath); FileOutputStream fops; fops = new FileOutputStream(uploadFile); fops.write(readInputStream(byteInputStream)); fops.flush(); fops.close(); } catch (Exception e) { e.printStackTrace(); } } public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } public static void main(String[] args) { String path = splitPdf(4,"D:\\data\\11.pdf","D:\\data\\out11.pdf"); File file =new File(path); //上傳的是png格式的圖片結尾 String targetfile="D:\\data\\out11.png"; pdfFileToImage(file,targetfile); } }
感謝各位的閱讀!關于“java如何實現截取PDF指定頁并進行圖片格式轉換功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。