package com.....util;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class PdfUtil {
//经过测试,dpi为96,100,105,120,150,200中,105显示效果较为清晰,体积稳定,dpi越高图片体积越大,一般电脑显示分辨率为96
public static final float DEFAULT_DPI = 105;
//默认转换的图片格式为jpg
public static final String DEFAULT_FORMAT = "jpg";
public static void main(String[] args) throws IOException {
PdfUtil.pdfToImage("D:\\test.pdf", "D:\\test");
}
public static void pdfToImage(String pdfPath, String imgPath) {
try {
//图像合并使用参数
// 总宽度
int width = 0;
// 保存一张图片中的RGB数据
int[] singleImgRGB;
int shiftHeight = 0;
//保存每张图片的像素值
BufferedImage imageResult = null;
//利用PdfBox生成图像
PDDocument pdDocument = PDDocument.load(new File(pdfPath));
PDFRenderer renderer = new PDFRenderer(pdDocument);
//循环每个页码
for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
//计算高度和偏移量
//使用第一张图片宽度;
width = imageWidth;
//保存每页图片的像素值
imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB);
//这里有高度,可以将imageHeight*len,我这里值提取一页所以不需要
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
// 写入流中
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
// 写图片
File f = new File(imgPath, "test_" + i + ".png");
if (!f.exists()) {
f.createNewFile();
}
ImageIO.write(imageResult, DEFAULT_FORMAT, f);
}
pdDocument.close();
} catch (Exception e) {
e.printStackTrace();
}
//OVER
}
public static List<byte[]> pdfToImage(String pdfPath) {
List<byte[]> result = new ArrayList<>();
try {
//图像合并使用参数
// 总宽度
int width = 0;
// 保存一张图片中的RGB数据
int[] singleImgRGB;
int shiftHeight = 0;
//保存每张图片的像素值
BufferedImage imageResult = null;
//利用PdfBox生成图像
PDDocument pdDocument = PDDocument.load(new File(pdfPath));
PDFRenderer renderer = new PDFRenderer(pdDocument);
//循环每个页码
for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB);
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
//计算高度和偏移量
//使用第一张图片宽度;
width = imageWidth;
//保存每页图片的像素值
imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB);
//这里有高度,可以将imageHeight*len,我这里值提取一页所以不需要
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
// 写入流中
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
// 写图片
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(imageResult, DEFAULT_FORMAT, out);
result.add(out.toByteArray());
}
pdDocument.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}