非常好用的PDF转图片工具

package com.aukey.supply.chain.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

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

import com.lowagie.text.pdf.PdfReader;

public class PDFToImageUtil {

	public static void main(String[] args) {

		pdf2Image("D:/test.pdf", "D:/contract","contract", 300);

	}

	/***
	 * PDF文件转PNG图片,全部页数
	 * 
	 * @param PdfFilePath
	 *            pdf完整路径
	 * @param imgFilePath
	 *            图片存放的文件夹
	 * @param packageName
	 * 			打包子包名
	 * @param dpi
	 *            dpi越大转换后越清晰,相对转换速度越慢
	 * @return
	 */

	public static List<String> pdf2Image(String PdfFilePath, String dstImgFolder, String packageName,int dpi) {

		File file = new File(PdfFilePath);

		PDDocument pdDocument;
		
		//存储转换后的图片地址
		List<String> imageFileUrlList=new ArrayList<String>();
		
		try {

			String imgPDFPath = file.getParent();

			/*int dot = file.getName().lastIndexOf('.');

			String imagePDFName = file.getName().substring(0, dot);*/ // 获取图片文件名

			String imgFolderPath = null;

			if (dstImgFolder.equals("")) {

				imgFolderPath = imgPDFPath + File.separator + packageName;// 获取图片存放的文件夹路径

			} else {

				imgFolderPath = dstImgFolder + File.separator + packageName;

			}

			if (createDirectory(imgFolderPath)) {

				pdDocument = PDDocument.load(file);

				PDFRenderer renderer = new PDFRenderer(pdDocument);

				/* dpi越大转换后越清晰,相对转换速度越慢 */

				PdfReader reader = new PdfReader(PdfFilePath);

				int pages = reader.getNumberOfPages();

				StringBuffer imgFilePath = null;

				for (int i = 0; i < pages; i++) {

					String imgFilePathPrefix = imgFolderPath + File.separator;

					imgFilePath = new StringBuffer();

					imgFilePath.append(imgFilePathPrefix);

					/*imgFilePath.append("_");*/

					imgFilePath.append(String.valueOf(i + 1));

					imgFilePath.append(".jpg");

					File dstFile = new File(imgFilePath.toString());

					BufferedImage image = renderer.renderImageWithDPI(i, dpi);

					
					ImageIO.write(image, "jpg", dstFile);
					
					//存储文件地址名 然后打包发送
					imageFileUrlList.add(imgFilePath.toString());
				}

				System.out.println("PDF文档转jpg图片成功!");
				
				pdDocument.close();

			} else {

				System.out.println("PDF文档转jpg图片失败:" + "创建" + imgFolderPath + "失败");

			}

		} catch (IOException e) {

			e.printStackTrace();

		}
		return imageFileUrlList;
	}
	//专门的转换PDF转换图片
	public static List<String> pdfToImage(String PdfFilePath, String dstImgFolder, String packageName,int dpi) {

		File file = new File(PdfFilePath);

		PDDocument pdDocument;
		
		//存储转换后的图片地址
		List<String> imageFileUrlList=new ArrayList<String>();
		
		try {

			String imgPDFPath = file.getParent();

			/*int dot = file.getName().lastIndexOf('.');

			String imagePDFName = file.getName().substring(0, dot);*/ // 获取图片文件名

			String imgFolderPath = null;

			if (dstImgFolder.equals("")) {

				imgFolderPath = imgPDFPath + File.separator + packageName;// 获取图片存放的文件夹路径

			} else {

				imgFolderPath = dstImgFolder + File.separator + packageName;

			}

			if (createDirectory(imgFolderPath)) {

				pdDocument = PDDocument.load(file);

				PDFRenderer renderer = new PDFRenderer(pdDocument);

				/* dpi越大转换后越清晰,相对转换速度越慢 */

				PdfReader reader = new PdfReader(PdfFilePath);

				int pages = reader.getNumberOfPages();

				StringBuffer imgFilePath = null;

				for (int i = 0; i < pages; i++) {

					String imgFilePathPrefix = imgFolderPath + File.separator;

					imgFilePath = new StringBuffer();

					imgFilePath.append(imgFilePathPrefix);

					/*imgFilePath.append("_");*/

					imgFilePath.append(String.valueOf(i + 1));

					imgFilePath.append(".jpg");

					File dstFile = new File(imgFilePath.toString());

					BufferedImage image = renderer.renderImageWithDPI(i, dpi);

					
					ImageIO.write(image, "jpg", dstFile);
					
					//存储文件地址名 然后打包发送
					imageFileUrlList.add(imgFilePath.toString());
				}

				System.out.println("PDF文档转jpg图片成功!");
				pdDocument.close();
			} else {

				System.out.println("PDF文档转jpg图片失败:" + "创建" + imgFolderPath + "失败");

			}

		} catch (IOException e) {

			e.printStackTrace();

		}
		return imageFileUrlList;
	}
	
	
	
	private static boolean createDirectory(String folder) {

		File dir = new File(folder);

		if (dir.exists()) {

			return true;

		} else {

			return dir.mkdirs();

		}

	}

}

附带jar包下载地址:https://download.csdn.net/download/qq_24818689/10893420

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值