提示:有些時(shí)候我們需要在項(xiàng)目中展示PDF,所以我們可以將PDF轉(zhuǎn)為圖片,然后已圖片的方式展示,效果很好。Java使用各種技術(shù)將pdf轉(zhuǎn)換成圖片格式,并且內(nèi)容不失幀。清晰可見,該工具類也是開發(fā)中常用到的工具類。其中包括單頁(yè)pdf轉(zhuǎn)換成一張圖片、多頁(yè)pdf轉(zhuǎn)換成多頁(yè)圖片、。 并且本人親自使用過(guò)的。
前言
提示:生成圖片以后需要將文件流關(guān)閉,不然刪除文件會(huì)刪除失敗
很多人不知道怎么將pdf的文件轉(zhuǎn)換成圖片格式的,而且網(wǎng)上有很例子是跑不通的,同是也是方便自己在用到該需求的時(shí)候能夠快速度地寫出來(lái),所以整理了幾種pdf轉(zhuǎn)換成圖片的方法工具類,
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、使用開源庫(kù)Apache PDFBox將PDF轉(zhuǎn)換為圖片
1、引入依賴庫(kù)
<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)換圖片工具類(多頁(yè)pdf會(huì)生成多頁(yè)的圖片,后綴會(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", "測(cè)試", "png");
}
}
使用Apache PDFBox將PDF轉(zhuǎn)換為圖片成功
3、按照固定頁(yè)數(shù)來(lái)將pdf轉(zhuǎn)換成圖片的工具類(自由選擇pdf轉(zhuǎn)換圖片的頁(yè)數(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 {
/**
* 自由確定起始頁(yè)和終止頁(yè)
* @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test
* @param filename PDF文件名不帶后綴名
* @param indexOfStart 開始頁(yè) 開始轉(zhuǎn)換的頁(yè)碼,從0開始
* @param indexOfEnd 結(jié)束頁(yè) 停止轉(zhuǎn)換的頁(yè)碼,-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", "思泰得流式檢測(cè)報(bào)告-00420299-任蛆小-RA202302100117",2,3, "png");
}
}
自由頁(yè)數(shù)轉(zhuǎn)換成功
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-459728.html
二、使用PDF Box將多頁(yè)的pdf轉(zhuǎn)換一張長(zhǎng)圖片的方法
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、編寫將多頁(yè)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圖片,全部頁(yè)數(shù)
* @param pdfFilePath pdf完整路徑:C:\\Users\\user\\Desktop\\test\\1234.pdf
* @param dpi dpi越大轉(zhuǎn)換后越清晰,相對(duì)轉(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)換后越清晰,相對(duì)轉(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)換后越清晰,相對(duì)轉(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ì)提示文件正在使用,無(wú)法刪除的情況
pdDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
public static void main(String[] args) throws FileNotFoundException {
pdfToImage("C:\\Users\\user\\Desktop\\test", "測(cè)試", "png");
}
}
總結(jié)
以上好幾種pdf轉(zhuǎn)圖片的方法,該方法可直接拿過(guò)去用,或者按照自己的邏輯進(jìn)行修改使用。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面就來(lái)一起學(xué)習(xí)學(xué)習(xí)吧?。。?span toymoban-style="hidden">文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-459728.html
到了這里,關(guān)于Java實(shí)現(xiàn)pdf轉(zhuǎn)圖片的工具類(三種方法實(shí)現(xiàn)PDF轉(zhuǎn)圖片的案例)【親測(cè)可用】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!