pdf转图片



import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
* Create by tianqing on 2019/3/30.
*/
public class Pdf2ImageUtil {

private static final Logger logger = LoggerFactory.getLogger(Pdf2ImageUtil.class);

public static void main(String[] args) {

buildPng("/Users/zmx/Downloads/2201.pdf", "/Users/zmx/Downloads/220117.png");

}



public static void buildPng(String sourcePdf, String outFilePath) {
buildByPdfbox(sourcePdf,outFilePath);
}

private static void buildByIcepdf(String sourcePdf, String outFilePath) {
/*Pdf2ImageUtil.registerNewFont(PDF_FONT_PATH + "simsun.ttf");
Document document = new Document();
try {
document.setFile(sourcePdf);
} catch (Exception e) {
logger.info("[Pdf2ImageUtil - buildPng] 读取pdf失败", e);
}
float scale = 2.5f;//缩放比例
float rotation = 0f;//旋转角度

int len = document.getNumberOfPages();
int[][] ImageArrays = new int[len][];
BufferedImage[] images = new BufferedImage[len];
int dst_height = 0;
// 生成新图片
try {
for (int i = 0; i < document.getNumberOfPages(); i++) {
images[i] = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = images[i];
int width = images[i].getWidth();
int height = images[i].getHeight();
// 从图片中读取RGB 像素
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
dst_height += height;
images[i].flush();
}
int dst_width = images[0].getWidth();

BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
for (int i = 0; i < images.length; i++) {
ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width);
height_i += images[i].getHeight();
}

File outFile = new File(outFilePath);
ImageIO.write(ImageNew, "png", outFile);// 写图片 ,输出到硬盘
} catch (Exception e) {
e.printStackTrace();
}
document.dispose();*/
}

public static void buildByPdfbox(String pdfPath, String imgPath) {
try {
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
//图像合并使用参数
// 总宽度
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, 105, ImageType.RGB);
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
//计算高度和偏移量
if (i == 0) {
//使用第一张图片宽度;
width = imageWidth;
//保存每页图片的像素值
imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
} else {
// 计算偏移高度
shiftHeight += imageHeight;
}
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
// 写入流中
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
}
pdDocument.close();
// 写图片
ImageIO.write(imageResult, "png", new File(imgPath));
} catch (Exception e) {
logger.error("PDF转图片失败",e);
}
}
}
原文地址:https://www.cnblogs.com/zhimingxin/p/10646724.html