使用java实现pdf文件转换为jpg或者png(可以批量操作)
使用java代码实现将pdf转换为图片格式。支持归类,支持pdf多页分页面转换存放。
需求背景:有几百个文件夹,每个文件夹里有两个pdf文件,我需要把这两个pdf转换为jpg,pdf内容有很多页,分页转换并友好命名,最后归类在一个文件夹里。
解决过程:在文件夹下以pdf文件的名称创建文件夹,将pdf分页转换,以"pdf名称_1.jpg"命名,归类在目录下。
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-core</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
package org.example;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import sun.misc.BASE64Decoder;
public class pdfToJpgUtil {
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
/**
* pdf文件转jpg
*
* @param pdffilepath
*/
public static void pdf2jpg(String pdffilepath, String jpgfilepath) throws Exception {
Document document = new Document();
document.setFile(pdffilepath);
float scale = 2.5f;// 缩放比例
float rotation = 0f;// 旋转角度
for (int i = 0; i < document.getNumberOfPages(); i++) {
String exportJpgFilePath = "";
BufferedImage image = (BufferedImage) document.getPageImage(i,
GraphicsRenderingHints.SCREEN,
org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation,
scale);
RenderedImage rendImage = image;
try {
exportJpgFilePath = jpgfilepath + "_" + (i + 1) + ".jpg";
File file = new File(exportJpgFilePath);
ImageIO.write(rendImage, "jpg", file);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}
document.dispose();
}
/**
* 递归删除文件
*
* @param path
*/
public static void deleteFile(String path) {
File file = FileUtils.getFile(path);
if (file.isDirectory()) {
String[] list = file.list();
if (null != list) {
for (int i = 0; i < list.length; i++) {
deleteFile(path + File.separator + list[i]);
}
}
}
boolean delete = file.delete();
if (!delete) {
System.out.println("error:File delete failed!");
}
}
public static List<String> getFiles(String path) {
List<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
int dirNum = tempList.length;
System.out.println("文件夹总数:" + dirNum + " 个。");
System.out.println("开始处理所有文件夹。");
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("***********************************************************");
System.out.println("****警告:主目录下有文件,请查看。文件名:" + tempList[i].toString());
System.out.println("***********************************************************");
}
//如果是文件夹,需要以这个文件夹目录为基准,逐个进行转换
if (tempList[i].isDirectory()) {
System.out.println("*******************开始处理:" + tempList[i].toString() + "*******************");
String pdf = tempList[i].getPath();
File pdfFile = new File(pdf);
File[] pdfFileList = pdfFile.listFiles();
for (int j = 0; j < pdfFileList.length; j++) {
if (pdfFileList[j].isFile()) {
//文件名称
String fileName = pdfFileList[j].getName();
//文件的全量路劲
String filePath = pdfFileList[j].getPath();
if (!fileName.contains(".pdf")) {
System.out.println("error:该目录存在非pdf文件,请查看。文件路径:" + pdfFileList[j].getPath());
continue;
}
//截取名称创建文件夹
String subFileName = fileName.substring(0, fileName.indexOf(".pdf"));
String exportDir = pdf + "\\" + subFileName;
File createDir = new File(exportDir);
if (createDir.exists()) {
deleteFile(exportDir);
}
createDir.mkdir();
//开始转换
try {
pdf2jpg(filePath, exportDir + "\\" + subFileName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.out.println("*******************处理完成:" + tempList[i].toString() + "*******************");
}
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
getFiles("E:\\bzh3");
System.out.println("E:\\bzh3 完成");
Thread.sleep(5000);
getFiles("E:\\bzh6");
System.out.println("E:\\bzh6 完成");
}
}
具体实现过程可以看代码,下面附效果图: