java實(shí)現(xiàn)截取PDF指定頁(yè)并進(jìn)行圖片格式轉(zhuǎn)換功能
1、引入依賴(lài)
<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、實(shí)現(xiàn)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格式的圖片結(jié)尾 String targetfile="D:\\data\\out11.png"; pdfFileToImage(file,targetfile); } }
總結(jié)
以上所述是小編給大家介紹的java實(shí)現(xiàn)截取PDF指定頁(yè)并進(jìn)行圖片格式轉(zhuǎn)換功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Java通過(guò)正則表達(dá)式獲取字符串中數(shù)字的方法示例
最近工作中遇到了一個(gè)需求,需要利用java獲取字符串中的數(shù)字,嘗試幾種方法后發(fā)現(xiàn)利用正則表達(dá)式實(shí)現(xiàn)最為方法,下面這篇文章就主要介紹了Java通過(guò)正則表達(dá)式獲取字符串中數(shù)字的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考下。2017-03-03Maven一鍵部署Springboot到Docker倉(cāng)庫(kù)為自動(dòng)化做準(zhǔn)備(推薦)
這篇文章主要介紹了Maven一鍵部署Springboot到Docker倉(cāng)庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析
這篇文章主要介紹了springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09spring cloud config分布式配置中心的高可用問(wèn)題
本文給大家介紹spring cloud config分布式配置中心的高可用問(wèn)題,通過(guò)整合Eureka來(lái)實(shí)現(xiàn)配置中心的高可用,需要的朋友參考下本文2018-01-01