工作中遇到需要把PDF文件转成图片使用,在此记录下,其中用到了三方JAR
package com.xxx.xxx.utils;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @author qingyu
* @version 1.0
* @date 2022/2/17 17:33
*/
public class PdfConvertImg {
public static void pdf2multiImage(String pdfFile, String outpath)
throws PDFException, PDFSecurityException, IOException {
InputStream is = new FileInputStream(pdfFile);
List<BufferedImage> piclist = new ArrayList<BufferedImage>();
Document document = new Document();
document.setFile(pdfFile);
float scale = 2.0f; // 缩放比例
float rotation = 0f; // 旋转角度
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
Page.BOUNDARY_CROPBOX, rotation, scale);
piclist.add(image);
}
document.dispose();
yPic(piclist, outpath);
is.close();
}
public static void yPic(List<BufferedImage> piclist, String outPath) throws IOException {// 纵向处理图片
int height = 0, // 总高度
_width = 0, // 临时宽度
maxWidth = 0, // 最大宽度
_height = 0, // 临时的高度 , 或保存偏移高度
__height = 0, // 临时的高度,主要保存每个高度
picNum = piclist.size();// 图片的数量
int[] heightArray = new int[picNum]; // 保存每个文件的高度
int[] widthArray = new int[picNum]; // 保存每个文件的宽度
BufferedImage buffer = null; // 保存图片流
List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的RGB
int[] _imgRGB; // 保存一张图片中的RGB数据
for (int i = 0; i < picNum; i++) {
buffer = piclist.get(i);
if (buffer.getWidth() > maxWidth) {
// 获取最大宽度
maxWidth = buffer.getWidth();
}
}
for (int i = 0; i < picNum; i++) {
buffer = piclist.get(i);
heightArray[i] = _height = buffer.getHeight();// 图片高度
widthArray[i] = _width = buffer.getWidth();// 图片宽度
height += _height; // 获取总高度
_imgRGB = new int[_width * _height];// 从图片中读取RGB
_imgRGB = buffer.getRGB(0, 0, _width, _height, _imgRGB, 0, _width);
imgRGB.add(_imgRGB);
}
_height = 0; // 设置偏移高度为0
// 生成新图片
BufferedImage imageResult = new BufferedImage(_width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < picNum; i++) {
__height = heightArray[i];
_width = widthArray[i];
// if (i != 0) _height += heightArray[i-1]; // 计算偏移高度 ,若高度一致,则把此行打开
if (i != 0)
_height += heightArray[i - 1]; // 计算偏移高度 ,因高度不一致,所以i-1
// imageResult.setRGB(0, _height, widthArray[i], __height, imgRGB.get(i), 0, widthArray[i]); // 写入流中
// 居中,前两个参数为起始的宽度、高度
imageResult.setRGB((maxWidth - _width) / 2, _height, _width, __height, imgRGB.get(i), 0, _width); // 写入流中
}
File outFile = new File(outPath);
ImageIO.write(imageResult, "jpg", outFile);// 写图片
}
public static void main(String[] args) throws PDFSecurityException, PDFException, IOException {
PdfConvertImg.pdf2multiImage("D:/home/user1/POSPPIC/AK1434726700369334272/creditInvestigation.pdf",
"D:/home/user1/POSPPIC/AK1434726700369334272/creditInvestigation.jpg");
}
}
这个工具类的yPic方法为多找PDF文件转成一张长图片,一般PDF会有多页这个方法输入的为一张图片。网站限制下载必须要积分,已设置成最低1分方便大家使用!
JAR下载地址