1.需引入jar依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.11</version>
</dependency>
2.将PDF转成一页长图
public static BufferedImage pdfChangeImage(InputStream inputStream) {
//图像合并使用参数
int width = 0; // 总宽度
int[] singleImgRGB; // 保存一张图片中的RGB数据
int shiftHeight = 0;
BufferedImage imageResult = null;//保存每张图片的像素值
try {
//利用PdfBox生成图像
PDDocument pdDocument = PDDocument.load(inputStream);
PDFTextStripper text = new PDFTextStripper();
String text1 = text.getText(pdDocument);
logger.info("读取PDF文字:" + text1);
PDFRenderer renderer = new PDFRenderer(pdDocument);
//循环每个页码
for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
//dpi参数越大,越清晰
BufferedImage image = renderer.renderImageWithDPI(i, 300, 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();
} catch (Exception e) {
logger.error(" 从PDF转换JPG格式异常!", e);
}
return imageResult;
}
public static void main(String[] args) {
InputStream inputStream = null;
BufferedImage bufferedImage = null;
String url = "";
try {
//pdf转img
bufferedImage = PDFUtils.pdfChangeImage(new URL(url));
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", os);
inputStream = new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
得到流后就可以下载到本地或者转存其他空间