java使用Aspose.pdf实现pdf转图片

主要步骤

  1. 使用Aspose进行文档转换,首先引入相应的jar包到系统环境
  2. 项目resource下导入license.xml文件
  3. 使用Aspose时,需要调用设置License方法,设置完成第一次转换比较慢,再次就不需要设置License方法,效率会更高。

注意:使用Aspose时,每一个模块(words,pdf,cells)都有相同的类,如License类,SaveOptions类,SaveFormat类,功能各不相同。

获取license示例代码:

package com.sinoif.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;

/**
 * @description:
 * @author: guanlj
 * @date: 2020/7/24 11:10
 */
@Slf4j
public class AsposeLicenseUtil {

	/**
	 * 获取License的输入流
	 *
	 * @return
	 */
	private static InputStream getLicenseInput() {

		InputStream inputStream = null;
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		try {
			inputStream = contextClassLoader.getResourceAsStream("license.xml");
		} catch (Exception e) {
			log.error("license not found!"+ e);
		}
		return inputStream;
	}

	/**
	 * 设置Aspose PDF的license
	 * @return true表示设置成功,false表示设置失败
	 */
	public static boolean setPdfLicense() {
		InputStream licenseInput = getLicenseInput();
		if (licenseInput != null) {
			try {
				com.aspose.pdf.License asposeLicense = new com.aspose.pdf.License();
				asposeLicense.setLicense(licenseInput);
				return true;
			} catch (Exception e) {
				log.error("set pdf license error!", e);
			}
		}
		return false;
	}
}

pdf转图片工具类:

package com.sinoif.common.utils;

import com.aspose.pdf.Document;
import com.aspose.pdf.devices.JpegDevice;
import com.aspose.pdf.devices.Resolution;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: guanlj
 * @date: 2020/8/13 15:16
 */
@Slf4j
public class PdfToImageUtil {


	/**
	 * 将pdf转图片
	 * @param inputStream pdf源文件流
	 * @param imgFilePath 转成一张图片文件全路径 例如 "D:\\home\\qq.png"
	 */
	public static void pdfToImage(InputStream inputStream, String imgFilePath) {
		try {
			log.info("convert pdf2jpg begin");
			long old = System.currentTimeMillis();
			if (!AsposeLicenseUtil.setPdfLicense()){
				return;
			}
			Document pdfDocument = new Document(inputStream);
			//分辨率
			Resolution resolution = new Resolution(130);
			JpegDevice jpegDevice = new JpegDevice(resolution);
			List imageList = new ArrayList();
			List fileList = new ArrayList<>();
			for (int index = 1; index <= pdfDocument.getPages().size(); index++) {
				File file = File.createTempFile("tempFile", "png");
				FileOutputStream fileOS = new FileOutputStream(file);
				jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOS);
				fileOS.close();
				imageList.add(ImageIO.read(file));
				fileList.add(file);
			}
			//临时文件删除
			BufferedImage mergeImage = mergeImage(false, imageList);
			ImageIO.write(mergeImage, "png", new File(imgFilePath));
			long now = System.currentTimeMillis();
			log.info("convert pdf2jpg completed, elapsed :" + ((now - old) / 1000.0) + "秒");
			//删除临时文件
			for (File f : fileList) {
				if (f.exists()){
					f.delete();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("convert pdf2jpg error:"+e);
		}

	}

	/**
	 * 合并任数量的图片成一张图片
	 *
	 * @param isHorizontal true代表水平合并,fasle代表垂直合并
	 * @param imgs         待合并的图片数组
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage mergeImage(boolean isHorizontal, List imgs) throws IOException {
		// 生成新图片
		BufferedImage destImage = null;
		// 计算新图片的长和高
		int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
		// 获取总长、总宽、最长、最宽
		for (int i = 0; i < imgs.size(); i++) {
			BufferedImage img = imgs.get(i);
			allw += img.getWidth();

			if (imgs.size() != i + 1) {
				allh += img.getHeight() + 5;
			} else {
				allh += img.getHeight();
			}


			if (img.getWidth() > allwMax) {
				allwMax = img.getWidth();
			}
			if (img.getHeight() > allhMax) {
				allhMax = img.getHeight();
			}
		}
		// 创建新图片
		if (isHorizontal) {
			destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
		} else {
			destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
		}
		Graphics2D g2 = (Graphics2D) destImage.getGraphics();
		g2.setBackground(Color.LIGHT_GRAY);
		g2.clearRect(0, 0, allw, allh);
		g2.setPaint(Color.RED);

		// 合并所有子图片到新图片
		int wx = 0, wy = 0;
		for (int i = 0; i < imgs.size(); i++) {
			BufferedImage img = imgs.get(i);
			int w1 = img.getWidth();
			int h1 = img.getHeight();
			// 从图片中读取RGB
			int[] ImageArrayOne = new int[w1 * h1];
			// 逐行扫描图像中各个像素的RGB到数组中
			ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
			if (isHorizontal) {
				// 水平方向合并
				// 设置上半部分或左半部分的RGB
				destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1);
			} else {
				// 垂直方向合并
				// 设置上半部分或左半部分的RGB
				destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1);
			}
			wx += w1;
			wy += h1 + 5;
		}


		return destImage;
	}

	/**
	 * pfd转图片
	 * @param pdf 源文件全路径
	 * @param outPath 转后的文件夹路径
	 */
	public static void pdfToImage(String pdf, String outPath){
		// 验证License
		if (!AsposeLicenseUtil.setPdfLicense()) {
			return;
		}

		try {
			long old = System.currentTimeMillis();
			log.info("convert pdf2jpg begin");
			Document pdfDocument = new Document(pdf);
			//图片宽度:800
			//图片高度:100
			// 分辨率 960
			//Quality [0-100] 最大100
			//例: new JpegDevice(800, 1000, resolution, 90);
			Resolution resolution = new Resolution(960);
			JpegDevice jpegDevice = new JpegDevice(resolution);
			for (int index=1;index<=pdfDocument.getPages().size();index++) {
				// 输出路径
				File file = new File(outPath + "/"+index+".jpg");
				FileOutputStream fileOs = new FileOutputStream(file);
				jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOs);
				fileOs.close();
			}

			long now = System.currentTimeMillis();
			log.info("convert pdf2jpg completed, elapsed :" + ((now - old) / 1000.0) + "秒");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("convert pdf2jpg error:"+e);
		}


	}

}

注:在转图片的时候注意图片的属性,长,宽,像素;像素越高转换越慢;

所需jar包和license.xml文件地址:

https://download.csdn.net/download/glj6287/12710511

你可能感兴趣的:(Java,Aspose,java)