最新版访问:https://blog.csdn.net/wmf_helloWorld/article/details/107105353
1、根据文件路径获取文件,并将PDF文件的每一页转换为一个图片。其中要将图片转为base64格式的。
/**
* <p>Description PDF文件转成图片</p>
* @author wumf
* @date 2020年1月20日 上午11:00:47
* @param PdfFilePath PDF文件路径
* @param fileImageList 存放图片文件
* @param dpi 越大越清晰,转换也越慢
* @return 文件页数
* @throws CodeException
*/
public int pdfTurnImage(String pdfFilePath, List<String> fileImageList, int dpi) throws CodeException {
File file = new File(pdfFilePath);
PDDocument pdDocument;
int pages = 0;
try {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
PdfReader reader = new PdfReader(pdfFilePath);
pages = reader.getNumberOfPages();
LOGGER.info(LogType.INFO, "PDF文件一共有"+pages+"页");
for (int i = 0; i < pages; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
fileImageList.add(base64Image(image));
}
} catch (Exception e) {
LOGGER.error(LogType.ERROR, e);
throw new CodeException(XYRFileServerConstants.ERROR_CODE_2017, XYRFileServerConstants.ERROR_MESSAGE_2017, e);
}
return pages;
}
2、base64Image,其中的Base64.encode(bytes)方法为自定义的方法,可以去网上百度其他方式
/**
* <p>Description image转成base64</p>
* @author wumf
* @date 2020年1月20日 上午11:26:24
* @param image
* @return
* @throws CodeException
*/
private String base64Image(BufferedImage image) throws CodeException {
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(image, "png", baos);//写入流中
byte[] bytes = baos.toByteArray();//转换成字节
return new String(Base64.encode(bytes),"UTF-8");
} catch (Exception e) {
LOGGER.error(LogType.ERROR, e);
throw new CodeException(XYRFileServerConstants.ERROR_CODE_2017, XYRFileServerConstants.ERROR_MESSAGE_2017, e);
}
}
3、此时文件图片已经在集合中,可以解析查看
List<String> list = response.getFileImageList();
for (int i = 0; i < list.size(); i++) {
File file = new File("/20200120\\image"+i+".jpg");
String image = list.get(i);
try (FileOutputStream fos = new FileOutputStream(file)){
fos.write(Base64.decode(image));
} catch (Exception e) {
throw new CodeException(XYRFileServerConstants.ERROR_CODE_2018, XYRFileServerConstants.ERROR_MESSAGE_2018);
}
}
4、需要引用的包
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Component;
import com.itextpdf.text.pdf.PdfReader;
import cfca.sadk.util.Base64;
5、maven依赖
<!-- PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
</dependency>
<dependency>
<groupId>cpcn.common.3rd</groupId>
<artifactId>itext-xtra</artifactId>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.9</version>
</dependency>
5、测试demo
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.itextpdf.text.pdf.PdfReader;
public class LocalTest {
// 获取到 pdf 文件路径
// 获取到图片存放路径
// study-1.jpg
public static void main(String[] args) {
pdf2Image("D:\\xyrFileRepository\\PolicyDeclare\\20200120\\202001200944427972857793199.pdf", "D:/temp/picture", 150);
}
/***
*
* PDF文件转PNG图片,全部页数
* @param PdfFilePath pdf完整路径
* @param imgFilePath 图片存放的文件夹
* @param dpi dpi越大转换后越清晰,相对转换速度越慢
* @return list 获取到pdf 的文件路径 /dsd/d.pdf 然后拼上绝对路径
*
*/
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越大转换后越清晰,相对转换速度越慢 */
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));
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文档转PNG图片成功!");
System.out.println("做保存操作保存数据库-----");
return 1;
} else {
System.out.println("PDF文档转PNG图片失败:" + "创建" + 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 文件夹完整绝对路径
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();
}
}
// 删除指定文件夹下所有文件
// param path 文件夹完整绝对路径
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;
}
}