pdfbox pdf转图片

本文介绍了一种使用Java将PDF文件转换为PNG图片的方法。通过加载PDF文档,并利用PDFRenderer进行渲染,可以逐页将PDF内容转换为图像。此过程支持自定义DPI并允许合并多页到一个图像中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void pdf2Image(File sourceFile, File destFile) {
		if (sourceFile.exists()) {
			try {
				PDDocument doc = PDDocument.load(sourceFile);
				PDFRenderer renderer = new PDFRenderer(doc);
				int pageCount = doc.getNumberOfPages();
				BufferedImage imageNew = null;
				for (int i = 0; i < pageCount; i++) {
					BufferedImage image = renderer.renderImageWithDPI(i, 284);
					int width = image.getWidth();
					int height = image.getHeight();
					if (imageNew == null) {
						imageNew = new BufferedImage(width, (height + SEPARATE_DISTANCE) * pageCount,
								BufferedImage.TYPE_INT_RGB);
					}
					int[] imageRgbArray = new int[width * height];
					imageRgbArray = image.getRGB(0, 0, width, height, imageRgbArray, 0, width);
					imageNew.setRGB(0, (height + SEPARATE_DISTANCE) * i, width, height, imageRgbArray, 0, width);// SEPARATE_DISTANCE表示两张图片的间隔距离
				}
				ImageIO.write(imageNew, "PNG", destFile);
		  
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

  

转载于:https://www.cnblogs.com/moonlignt/p/9233763.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值