1、首先需要在项目中引入maven <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fontbox</artifactId> <version>2.0.15</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.8</version> </dependency> <dependency> <groupId>org.eclipse.birt.runtime.3_7_1</groupId> <artifactId>com.lowagie.text</artifactId> <version>2.1.7</version> </dependency>
2、具体的工具类 package com.xxx.xxx.util; 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; /** * @Author: Robert·Ran * @CreateTime: 2019/5/28 17:04 * @Description: 文件工具类 */ public class FileTools { /** * @param PdfFilePath pdf完整路径 * @param dpi dpi越大转换后越清晰,相对转换速度越慢 * @Author: Robert·Ran * @CreateTime: 2019/5/17 11:59 * @Description: PDF文件转PNG图片 */ public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) throws IOException { File file = new File(PdfFilePath); PDDocument pdDocument = null; try { // String imgPDFPath = file.getParent(); int dot = file.getName().lastIndexOf('.'); String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名 //以下被注释代码是用于创建新文件夹存放转换的图片,被注释后转换文件夹与原PDF文件在同一文件夹内 // String imgFolderPath = null; // if (dstImgFolder.equals("")) { // imgFolderPath = imgPDFPath + File.separator + imagePDFName;// 获取图片存放的文件夹路径 // } else { // imgFolderPath = dstImgFolder + File.separator + imagePDFName; // } // if (createDirectory(imgFolderPath)) { pdDocument = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(pdDocument); /* dpi越大转换后越清晰,相对转换速度越慢 */ PdfReader reader = new PdfReader(PdfFilePath); int pages = reader.getNumberOfPages(); StringBuffer imgFilePath = null; for (int i = 0; i < pages; i++) { String imgFilePathPrefix = dstImgFolder + File.separator + imagePDFName; imgFilePath = new StringBuffer(); imgFilePath.append(imgFilePathPrefix); // imgFilePath.append("_");//为避免命名重复在后面加追加字符 // imgFilePath.append(String.valueOf(i + 1)); imgFilePath.append(".png"); File dstFile = new File(imgFilePath.toString()); BufferedImage image = renderer.renderImageWithDPI(i, dpi); ImageIO.write(image, "png", dstFile); } // } else { // System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败"); // } } catch (IOException e) { e.printStackTrace(); } finally { pdDocument.close(); } } /*创建文件*/ private static boolean createDirectory(String folder) { File dir = new File(folder); if (dir.exists()) { return true; } else { return dir.mkdirs(); } } /*删除指定路径下的文件或文件夹*/ 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; } /** * 删除文件夹 * * @param folderPath 文件夹完整绝对路径 */ public static void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); //删除空文件夹 } catch (Exception e) { e.printStackTrace(); } } }
鸣谢https://blog.csdn.net/thc1987/article/details/76457474/提供学习参考