Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的三種方法
前言
提示:生成圖片以后需要將文件流關(guān)閉,不然刪除文件會(huì)刪除失敗
很多人不知道怎么將pdf的文件轉(zhuǎn)換成圖片格式的,而且網(wǎng)上有很例子是跑不通的,同是也是方便自己在用到該需求的時(shí)候能夠快速度地寫出來,所以整理了幾種pdf轉(zhuǎn)換成圖片的方法工具類。
一、使用開源庫Apache PDFBox將PDF轉(zhuǎn)換為圖片
1、引入依賴庫
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fontbox</artifactId> <version>2.0.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox --> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.9</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
2、實(shí)現(xiàn)pdf轉(zhuǎn)換圖片工具類(多頁pdf會(huì)生成多頁的圖片,后綴會(huì)生成圖片的位置序號(hào))
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class Pdf2Png { /** * 使用pdfbox將整個(gè)pdf轉(zhuǎn)換成圖片 * * @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test * @param filename PDF文件名不帶后綴名 * @param type 圖片類型 png 和jpg */ public static void pdf2png(String fileAddress, String filename, String type) { long startTime = System.currentTimeMillis(); // 將文件地址和文件名拼接成路徑 注意:線上環(huán)境不能使用\\拼接 File file = new File(fileAddress + "\\" + filename + ".pdf"); try { // 寫入文件 PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for (int i = 0; i < pageCount; i++) { // dpi為144,越高越清晰,轉(zhuǎn)換越慢 BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI // 將圖片寫出到該路徑下 ImageIO.write(image, type, new File(fileAddress + "\\" + filename + "_" + (i + 1) + "." + type)); } long endTime = System.currentTimeMillis(); System.out.println("共耗時(shí):" + ((endTime - startTime) / 1000.0) + "秒"); //轉(zhuǎn)化用時(shí) } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { pdf2png("C:\\Users\\user\\Desktop\\test", "測試", "png"); } }
使用Apache PDFBox將PDF轉(zhuǎn)換為圖片成功
3、按照固定頁數(shù)來將pdf轉(zhuǎn)換成圖片的工具類(自由選擇pdf轉(zhuǎn)換圖片的頁數(shù))
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class Pdf2Png { /** * 自由確定起始頁和終止頁 * @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test * @param filename PDF文件名不帶后綴名 * @param indexOfStart 開始頁 開始轉(zhuǎn)換的頁碼,從0開始 * @param indexOfEnd 結(jié)束頁 停止轉(zhuǎn)換的頁碼,-1為全部 * @param type 圖片類型 png 和jpg */ public static void pdf2png(String fileAddress,String filename,int indexOfStart,int indexOfEnd,String type) { long startTime = System.currentTimeMillis(); // 將文件地址和文件名拼接成路徑 注意:線上環(huán)境不能使用\\拼接 File file = new File(fileAddress+"\\"+filename+".pdf"); try { PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for (int i = indexOfStart; i < indexOfEnd; i++) { // dpi為144,越高越清晰,轉(zhuǎn)換越慢 BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI // 將圖片寫出到該路徑下 ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type)); } long endTime = System.currentTimeMillis(); System.out.println("共耗時(shí):" + ((endTime - startTime) / 1000.0) + "秒"); // 轉(zhuǎn)換用時(shí) } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { pdf2png("C:\\Users\\user\\Desktop\\test", "思泰得流式檢測報(bào)告-00420299-任蛆小-RA202302100117",2,3, "png"); } }
自由頁數(shù)轉(zhuǎn)換成功
二、使用PDF Box將多頁的pdf轉(zhuǎn)換一張長圖片的方法
1、引入PDF Box需要的依賴
<dependency> <groupId>net.sf.cssbox</groupId> <artifactId>pdf2dom</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.12</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-tools</artifactId> <version>2.0.12</version> </dependency>
2、編寫將多頁P(yáng)DF轉(zhuǎn)換多張圖片的工具類
import com.lowagie.text.pdf.PdfReader; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class Pdf2Png { /*** * PDF文件轉(zhuǎn)PNG圖片,全部頁數(shù) * @param pdfFilePath pdf完整路徑:C:\\Users\\user\\Desktop\\test\\1234.pdf * @param dpi dpi越大轉(zhuǎn)換后越清晰,相對轉(zhuǎn)換速度越慢 */ public static void pdf2Image(String pdfFilePath, int dpi) { long startTime = System.currentTimeMillis(); File file = new File(pdfFilePath); PDDocument pdDocument; try { String imgPdfPath = file.getParent(); int dot = file.getName().lastIndexOf('.'); // 獲取圖片文件名 String imagePdfName = file.getName().substring(0, dot); pdDocument = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(pdDocument); /* dpi越大轉(zhuǎn)換后越清晰,相對轉(zhuǎn)換速度越慢 */ PdfReader reader = new PdfReader(pdfFilePath); int pages = reader.getNumberOfPages(); StringBuffer imgFilePath; for (int i = 0; i < pages; i++) { String imgFilePathPrefix = imgPdfPath + File.separator + imagePdfName; imgFilePath = new StringBuffer(); imgFilePath.append(imgFilePathPrefix); imgFilePath.append("_"); imgFilePath.append((i + 1)); imgFilePath.append(".png"); File dstFile = new File(imgFilePath.toString()); BufferedImage image = renderer.renderImageWithDPI(i, dpi); ImageIO.write(image, "png", dstFile); } long endTime = System.currentTimeMillis(); System.out.println("共耗時(shí):" + ((endTime - startTime) / 1000.0) + "秒"); //轉(zhuǎn)化用時(shí) } catch (IOException e) { e.printStackTrace(); } } }
三、使用文件流整個(gè)pdf轉(zhuǎn)換成圖片 (生成圖片,并將生成的圖片路徑返回)
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.util.*; public class Pdf2Png { /** * 使用文件流整個(gè)pdf轉(zhuǎn)換成圖片 * @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test * @param filename PDF文件名不帶后綴名 * @param type 圖片類型 png 、jpg */ public static List<Map<String, String>> pdfToImage(String fileAddress, String filename, String type) { long startTime = System.currentTimeMillis(); List<Map<String, String>> list = new ArrayList<>(); Map<String, String> resultMap = null; PDDocument pdDocument = null; String fileName = null; String imgPath = null; try { // 將文件地址和文件名拼接成路徑 注意:線上環(huán)境不能使用\\拼接 File FilePath = new File(fileAddress + "\\" + filename + ".pdf"); // 文件流 FileInputStream inputStream = new FileInputStream(FilePath); int dpi = 296; pdDocument = PDDocument.load(inputStream); PDFRenderer renderer = new PDFRenderer(pdDocument); int pageCount = pdDocument.getNumberOfPages(); /* dpi越大轉(zhuǎn)換后越清晰,相對轉(zhuǎn)換速度越慢 */ for (int i = 0; i < pageCount; i++) { resultMap = new HashMap<>(); fileName = filename + "_" + (i + 1) + "." + type; imgPath = fileAddress + "\\" + fileName; BufferedImage image = renderer.renderImageWithDPI(i, dpi); ImageIO.write(image, type, new File(imgPath)); resultMap.put("fileName", fileName); resultMap.put("filePath", imgPath); // 圖片路徑 list.add(resultMap); } long endTime = System.currentTimeMillis(); System.out.println("共耗時(shí):" + ((endTime - startTime) / 1000.0) + "秒"); //轉(zhuǎn)化用時(shí) } catch (Exception e) { e.printStackTrace(); } finally { try { // 這里需要關(guān)閉PDDocument,不然如果想要?jiǎng)h除pdf文件時(shí)會(huì)提示文件正在使用,無法刪除的情況 pdDocument.close(); } catch (IOException e) { e.printStackTrace(); } } return list; } public static void main(String[] args) throws FileNotFoundException { pdfToImage("C:\\Users\\user\\Desktop\\test", "測試", "png"); } }
總結(jié)
以上好幾種pdf轉(zhuǎn)圖片的方法,該方法可直接拿過去用,或者按照自己的邏輯進(jìn)行修改使用。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面就來一起學(xué)習(xí)學(xué)習(xí)吧!
以上就是Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的三種方法的詳細(xì)內(nèi)容,更多關(guān)于Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot項(xiàng)目設(shè)置斷點(diǎn)debug調(diào)試無效忽略web.xml問題的解決
這篇文章主要介紹了SpringBoot項(xiàng)目設(shè)置斷點(diǎn)debug調(diào)試無效忽略web.xml問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08詳解在spring boot中配置多個(gè)DispatcherServlet
本篇文章主要介紹了詳解在spring boot中配置多個(gè)DispatcherServlet,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03Java開發(fā)常見錯(cuò)誤之?dāng)?shù)值計(jì)算精度和舍入問題詳析
除了使用Double保存浮點(diǎn)數(shù)可能帶來精度問題外,更匪夷所思的是這種精度問題,下面這篇文章主要給大家介紹了關(guān)于Java開發(fā)常見錯(cuò)誤之?dāng)?shù)值計(jì)算精度和舍入問題的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11Java使用jdbc連接實(shí)現(xiàn)對MySQL增刪改查操作的全過程
JDBC的全稱是Java?Database?Connectivity,即Java數(shù)據(jù)庫連接,它是一種可以執(zhí)行SQL語句的Java?API,下面這篇文章主要給大家介紹了關(guān)于Java使用jdbc連接實(shí)現(xiàn)對MySQL增刪改查操作的相關(guān)資料,需要的朋友可以參考下2023-03-03Springcloud+Mybatis使用多數(shù)據(jù)源的四種方式(小結(jié))
這篇文章主要介紹了Springcloud+Mybatis使用多數(shù)據(jù)源的四種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Spring Boot插件spring tool suite安裝及使用詳解
這篇文章主要介紹了Spring Boot插件spring tool suite安裝及使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08