java pdf 多页转换为png格式的图片

java pdf 多页转换为png格式的图片  适用于 无论pdf有多少页均可

 废话不说,直接上代码


import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

/**
 * 
 * @author wdg pdf 转换为pdf
 *
 */
public class PdfToPng {
	
	
	/***
	 * 
	 * 
	 *   使用方式
	 *   
	 *   maven 项目中需要引入:
	 *   <dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>2.0.21</version>
		</dependency>
	 *   
	 *   
	 *   
	 * 方法是将多张pdf 转换为png 格式的图片
	 * @param pdffile  pdf 文件路径
	 * @param targetPath  png 文件存储路径
	 * @param height_offset  多张图片之间合成间隔距离
	 */


	public static void pdfFileToImage(File pdffile, String targetPath,int height_offset) {
		try {
			FileInputStream instream = new FileInputStream(pdffile);
			InputStream byteInputStream = null;
			try {
				PDDocument doc = PDDocument.load(instream);
				PDFRenderer renderer = new PDFRenderer(doc);
				int pageCount = doc.getNumberOfPages();
				List<BufferedImage> list = new ArrayList<BufferedImage>();
				if (pageCount > 0) {

					int totalHeight = 0;
					int width = 0;

					for (int i = 0; i < pageCount; i++) {
						BufferedImage image = renderer.renderImage(i, 1.25f);
						list.add(image);
						totalHeight += image.getHeight();
						if (width < image.getWidth()) {
							width = image.getWidth();
						}
						image.flush();
					}

					BufferedImage tag = new BufferedImage(width, totalHeight, BufferedImage.TYPE_INT_RGB);

					tag.getGraphics();

					Graphics g = tag.createGraphics();

					int startHeight = 0;
					for (BufferedImage image : list) {
						g.drawImage(image, 0, startHeight, width, image.getHeight(), null);
						g.drawImage(image, 0, startHeight, width, image.getHeight(), null);
						startHeight += image.getHeight() + height_offset;
					}
					g.dispose();

					ByteArrayOutputStream bs = new ByteArrayOutputStream();
					ImageOutputStream imOut;
					imOut = ImageIO.createImageOutputStream(bs);
					ImageIO.write(tag, "png", imOut);
					byteInputStream = new ByteArrayInputStream(bs.toByteArray());
					byteInputStream.close();

				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			File uploadFile = new File(targetPath);
			FileOutputStream fops;
			fops = new FileOutputStream(uploadFile);
			fops.write(readInputStream(byteInputStream));
			fops.flush();
			fops.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		inStream.close();
		return outStream.toByteArray();
	}

	public static void main(String[] args) {
		File file = new File("D:\\pdfpath\\ceshi2.pdf");
		// 上传的是png格式的图片结尾
		String targetfile = "D:\\pdfpath\\wdg3.png";
		
		pdfFileToImage(file, targetfile,10);
	}

}

希望对你有所帮助

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要将多页PDF的base64转换图片base64,可以按照以下步骤进行操作: 1. 将PDF的base64编码字符串转换为byte数组。 2. 使用JavaPDFBox库将byte数组转换PDF文档对象。 3. 遍历PDF文档的每一页,将每一页转换为BufferedImage对象。 4. 将每一页的BufferedImage对象转换为对应的图片base64编码字符串。 5. 将所有图片base64编码字符串拼接一个字符串返回。 下面是一个示例代码,可以将多页PDF的base64转换图片base64: ``` import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Base64; public class PdfToImageConverter { public static String convert(String pdfBase64) throws IOException { byte[] pdfBytes = Base64.getDecoder().decode(pdfBase64); PDDocument document = PDDocument.load(pdfBytes); PDFRenderer pdfRenderer = new PDFRenderer(document); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); for (int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = pdfRenderer.renderImageWithDPI(i, 300); ImageIO.write(image, "png", outputStream); outputStream.flush(); } byte[] imageBytes = outputStream.toByteArray(); String imageBase64 = Base64.getEncoder().encodeToString(imageBytes); outputStream.close(); document.close(); return imageBase64; } } ``` 相关问题: 1. 如何将单页PDF的base64转换图片base64? 2. 如何将图片base64转换为文件并保存到指定路径下? 3. 如何将多张图片合并为一张PDF文件并转换为base64编码字符串?
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值