pdf 转化image(网络下载的文件或者本地文件)

 需要jar包

pdfbox-2.0.12.jar, fontbox-2.0.12.jar

package cn.zgjzd.app.util.zjl;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

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

public class PdfToImage {

	/***
	 * PDF文件转PNG图片,全部页数
	 * 
	 * @param PdfFilePath
	 *            pdf完整路径
	 * @param imgFilePath
	 *            图片存放的文件夹
	 * @param dpi
	 *            dpi越大转换后越清晰,相对转换速度越慢
	 * @return
	 */
	public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) {
		File file = new File(PdfFilePath);
		PDDocument pdDocument;
		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 + imagePDFName;// 获取图片存放的文件夹路径
			} else {
				imgFolderPath = dstImgFolder + File.separator + imagePDFName;
			}

			if (createDirectory(imgFolderPath)) {

				pdDocument = PDDocument.load(file);
				/* dpi越大转换后越清晰,相对转换速度越慢 */
				PDFRenderer renderer = new PDFRenderer(pdDocument);
				int pages = pdDocument.getNumberOfPages();
				StringBuffer imgFilePath = null;
				for (int i = 0; i < pages; i++) {
					String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
					imgFilePath = new StringBuffer();
					imgFilePath.append(imgFilePathPrefix);
					imgFilePath.append("_");
					imgFilePath.append(String.valueOf(i + 1));
					imgFilePath.append(".png");
					File dstFile = new File(imgFilePath.toString());
					BufferedImage image = renderer.renderImageWithDPI(i, dpi);
					ImageIO.write(image, "png", dstFile);
				}
				System.out.println("PDF文档转PNG图片成功!");

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

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

	public static byte[] pdf2ImageGetLastImageByte(String urlStr, String fileName, String y_lx,String n_lx,String savePath, int dpi) {
		File file = new File(savePath + File.separator + fileName+"."+y_lx);
		PDDocument pdDocument = null;
		File dstFile = null;
		byte[]  bytes=null;
		BufferedImage image =null;
		try {
				DownFileFromUrl.downLoadFromUrl(urlStr, file, savePath);
				pdDocument = PDDocument.load(file);
				PDFRenderer renderer = new PDFRenderer(pdDocument);
				int pages = pdDocument.getNumberOfPages();
				if(pages==0){
					return bytes;
				}
				dstFile = new File(savePath + File.separator + fileName+"."+n_lx);
				/* dpi越大转换后越清晰,相对转换速度越慢 */
				image = renderer.renderImageWithDPI(pages-1, dpi);
				ImageIO.write(image, "jpg", dstFile);
				bytes=toByteArray(dstFile);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(pdDocument!=null){
				try {
					pdDocument.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
//			file.delete();
//			dstFile.delete();
		}
		return bytes;
	}

	public static byte[] toByteArray(File f) throws IOException {
		if (!f.exists()) {
			throw new FileNotFoundException();
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(f));
			int buf_size = 1024;
			byte[] buffer = new byte[buf_size];
			int len = 0;
			while (-1 != (len = in.read(buffer, 0, buf_size))) {
				bos.write(buffer, 0, len);
			}
			return bos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			bos.close();
		}
	}

	private static boolean createDirectory(String folder) {
		File dir = new File(folder);
		if (dir.exists()) {
			return true;
		} else {
			return dir.mkdirs();
		}
	}
}
 

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownFileFromUrl {
	/**
	 * 从网络Url中下载文件
	 * 
	 * @param urlStr
	 * @param fileName
	 * @param savePath
	 * @throws IOException
	 */
	public static void downLoadFromUrl(String urlStr, File file, String savePath) {
		// 获取自己数组
		byte[] getData = downLoadFromUrlByte(urlStr);
		// 文件保存位置
		File saveDir = new File(savePath);
		if (!saveDir.exists()) {
			saveDir.mkdir();
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(getData);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static byte[] downLoadFromUrlByte(String urlStr) {
		InputStream inputStream = null;
		byte[] getData = null;
		try {
			URL url = new URL(urlStr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置超时间为3秒
			conn.setConnectTimeout(3 * 1000);
			// 防止屏蔽程序抓取而返回403错误
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			// 得到输入流
			inputStream = conn.getInputStream();
			// 获取自己数组
			getData = readInputStream(inputStream);
		} catch (IOException e1) {
			System.out.println("网络附件不存在 或网络不通 --> "+ urlStr);
			e1.printStackTrace();

		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return getData;
	}

	/**
	 * 从输入流中获取字节数组
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static byte[] readInputStream(InputStream inputStream) throws IOException {
		byte[] buffer = new byte[1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		bos.close();
		return bos.toByteArray();
	}

}
//调用	
byte[] bytes = PdfToImage.pdf2ImageGetLastImageByte("附件的网络路径", "附件名称", "pdf", "jpg",
						"本地存放路径", 300/*数值越大图片越清晰,转化越慢*/);

 

转载于:https://my.oschina.net/u/3715963/blog/2978889

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AP PDF to IMAGE Batch Converter 是一个有用的 pdf工具,该转换器能利用源 PDF 文档格式转换全功能的带文字,图片,图表等图像。它不需要任何软件支持,如 Adobe Acrobat,Acrobat Reader 等。 AP PDF to IMAGE Batch Converter 支持自定义输出结果页面。你可以选择转换几页文档,甚至自由转换文档页面,如 "1,2,3","2-100" 等,要获得更多信息,请参考页面范围设置。它能处理多个文件,只要确认设置好每个文件的输出目录,这样所有处理完的文件都会放置到它们相应的目录中。 PDF to Image 功能 * 迅速转换 PDF 文件到图像格式。 * 支持批量转换 PDF 到图像。 * 支持加密的 PD F文件。 * 支持自定义输出结果页面。 * 支持矢量图形转换到图像文件。 * 支持多个图像格式,如 'bmp','tiff','jpg','gif','png','pcx' 等。 * 支持生成多页面 tiff 文件。 * 支持生成任何分辨率的图像文件。 * 支持转换为 1 位,4 位,8 位,4 位灰度,8 位灰度和 24 位格式图像。 * 支持 tiff 图像的多种压缩,如 LZW,JPEG,PACKBITS,CCITT Group3,CCITT Group4,RLE 等。 * 支持拖放文件转换。 * 支持 Win98/ME/NT/2000/XP/2003/Vista 平台。 * 转换后可以自动显示图像文件。 * 允许你保留源文档的布局。 * 容易使用,单独的应用程序。 * 不需要 Adobe Acrobat 或 Adobe Acrobat Reader 的支持。 * 自动安装和配置。 * 兼容 Adobe Acrobat 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 。 命令行功能 PDF to Image 转换命令行程序也提供一个有正常转换 PDF to Image 功能的控制台。 * 包括 PDF to Image 的所有功能。 * 它可以像一个 DOS 命令行应用程序一样简单使用。 * 支持文件夹。 * 批量转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值