廢話不多說直接上代碼尘喝。不管多少頁(yè)的pdf,都能給你轉(zhuǎn)成圖片倦踢,速度非乘椭停快,主要是穩(wěn)定辱挥,比linux(下面有粘圖的 都被屏蔽掉了犁嗅。什么鬼)
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import com.lowagie.text.pdf.PdfReader;
/**
*@author :liu
* @version 創(chuàng)建時(shí)間:2018年4月19日 下午9:36:51
*? ? ? ? ? Description:
*/
public class PDF2IMAGE {
// 獲取到 pdf 文件路徑
// 獲取到圖片存放路徑
// study-1.jpg
public static void main(String[] args) {
pdf2Image("D:/temp/a222.pdf", "D:/temp/picture", 150);
}
/***
* PDF文件轉(zhuǎn)PNG圖片,全部頁(yè)數(shù)
*
* @param PdfFilePath
*? ? ? ? ? ? pdf完整路徑
* @param imgFilePath
*? ? ? ? ? ? 圖片存放的文件夾
* @param dpi
*? ? ? ? ? ? dpi越大轉(zhuǎn)換后越清晰晤碘,相對(duì)轉(zhuǎn)換速度越慢
* @return
* list 獲取到pdf 的文件路徑 /dsd/d.pdf? 然后拼上絕對(duì)路徑
*/
public static Integer pdf2Image(String PdfFilePath, String dstImgFolder,
int dpi) {
UUID uuid = UUID.randomUUID();
String uuId = uuid.toString();
System.out.println(uuId);
File file = new File(PdfFilePath);
@SuppressWarnings("resource")
PDDocument pdDocument = new PDDocument();
try {
//String imagePDFName = file.getName().substring(0, dot); // 獲取圖片文件名
String imgFolderPath = null;
if (dstImgFolder.equals("")) {
imgFolderPath = dstImgFolder + File.separator + uuId;// 獲取圖片存放的文件夾路徑
} else {
imgFolderPath = dstImgFolder + File.separator + uuId;
}
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
/* dpi越大轉(zhuǎn)換后越清晰褂微,相對(duì)轉(zhuǎn)換速度越慢 */
PdfReader reader = new PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
System.out.println("pdf總共多少頁(yè)-----" + pages);
StringBuffer imgFilePath = null;
for (int i = 0; i < pages; i++) {
String imgFilePathPrefix = imgFolderPath + File.separator
+ "study";
System.out.println("imgFilePathPrefix====="+ imgFilePathPrefix);
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append("-");
imgFilePath.append(String.valueOf(i));
imgFilePath.append(".jpg");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
System.out.println(dstFile);
ImageIO.write(image, "png", dstFile);
}
System.out.println("PDF文檔轉(zhuǎn)PNG圖片成功!");
System.out.println("做保存操作保存數(shù)據(jù)庫(kù)-----");
return 1;
} else {
System.out.println("PDF文檔轉(zhuǎn)PNG圖片失斣耙:" + "創(chuàng)建" + imgFolderPath
+ "失敗");
return -1;
}
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
//刪除文件夾
//param folderPath 文件夾完整絕對(duì)路徑
? ? public static void delFolder(String folderPath) {
? ? try {
? ? ? ? delAllFile(folderPath); //刪除完里面所有內(nèi)容
? ? ? ? String filePath = folderPath;
? ? ? ? filePath = filePath.toString();
? ? ? ? java.io.File myFilePath = new java.io.File(filePath);
? ? ? ? myFilePath.delete(); //刪除空文件夾
? ? } catch (Exception e) {
? ? ? e.printStackTrace();
? ? }
}
//刪除指定文件夾下所有文件
//param path 文件夾完整絕對(duì)路徑
? public static boolean delAllFile(String path) {
? ? ? boolean flag = false;
? ? ? File file = new File(path);
? ? ? if (!file.exists()) {
? ? ? ? return flag;
? ? ? }
? ? ? if (!file.isDirectory()) {
? ? ? ? return flag;
? ? ? }
String[] tempList = file.list();
? ? ? File temp = null;
? ? ? for (int i = 0; i < tempList.length; i++) {
? ? ? ? ? if (path.endsWith(File.separator)) {
? ? ? ? ? ? temp = new File(path + tempList[i]);
? ? ? ? ? } else {
? ? ? ? ? ? ? temp = new File(path + File.separator + tempList[i]);
? ? ? ? ? }
? ? ? ? ? if (temp.isFile()) {
? ? ? ? ? ? temp.delete();
? ? ? ? ? }
? ? ? ? ? if (temp.isDirectory()) {
? ? ? ? ? ? delAllFile(path + "/" + tempList[i]);//先刪除文件夾里面的文件
? ? ? ? ? ? delFolder(path + "/" + tempList[i]);//再刪除空文件夾
? ? ? ? ? ? flag = true;
? ? ? ? ? }
? ? ? }
? ? ? return flag;
? ? }
}
測(cè)試結(jié)果
148張轉(zhuǎn)換要49秒蕊梧。你可以調(diào)整一下分辨率,低的話腮介,速度會(huì)更快
下面在弄個(gè)生成圖片縮略圖的