我也是从网上找的,原文没找到,写个文章祭奠下原博主,勿怪勿怪
依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
测试:
public class PDFToImg2 {
public static void PdfToImages(String pdfPath, String ImgFolderPath, int dpi) {
PdfToImageUtil.pdfToImage(pdfPath, ImgFolderPath, dpi);
System.out.println("输出完成=========");
}
public static void main(String[] args) throws Exception{
// String pdfPath="F:\\pythonData\\pdf\\huiming.pdf";
String pdfPath="F:\\pythonData\\pdf\\zhongzhimima.pdf";//pdf文件地址
String imgfloder="F:\\pythonData\\pdf";//输出图片的地址
int dpi=150;
PdfToImages(pdfPath, imgfloder, dpi);
}
}
工具类
import com.itextpdf.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;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class PdfToImageUtil {
/**
* PDF文件转PNG图片,全部页数
*
* @param PdfFilePath pdf完整路径
* @param dstImgFolder 图片存放的文件夹
* @param dpi dpi越大转换后越清晰,相对转换速度越慢
* @return 返回转换后图片集合list
*/
public static List<File> pdfToImage(String PdfFilePath, String dstImgFolder, int dpi) {
UUID uuid = UUID.randomUUID();
String uuId = uuid.toString();
System.out.println(uuId);
File file = new File(PdfFilePath);
//定义集合保存返回图片数据
List<File> fileList = new ArrayList<File>();
@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越大转换后越清晰,相对转换速度越慢 */
PdfReader reader = new PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
//System.out.println("pdf总共多少页-----" + 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+1));
imgFilePath.append(".jpg");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "png", dstFile);
fileList.add(dstFile);
}
System.out.println("PDF文档转PNG图片成功!");
return fileList;
} else {
System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//创建文件夹
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
}