java pdf转图片


//import com.spire.pdf.PdfDocument;
/**
*这种方法如果想等比例的转图片得经过抽图这一步骤,也可能是有方法,但是我不知道
*/
public String pdfToImage(String PdfFilePath,int width,int height) throws IOException {
		// 加载PDF文件

		if(width == 0) {
			width = 1043;
		}
		if(height == 0) {
			height = 1501;
		}
		
//		System.out.println("开始读pdf文件");
		File file = new File(PdfFilePath);

		String path = "";

		int dot = file.getName().lastIndexOf('.');
//		System.out.println("dot:" + dot);
		String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
//		System.out.println("imagePDFName:" + imagePDFName);

		PdfDocument doc = new PdfDocument();
		doc.loadFromFile(PdfFilePath);

		// 保存PDF的每一页到图片
		BufferedImage image;
		for (int i = 0; i < doc.getPages().getCount(); i++) {

			String imgFilePathPrefix = historyDataPlugin.pdfPicPath + File.separator + "temp"  + File.separator + imagePDFName + ".png";

			image = doc.saveAsImage(i);
			File fileImg=new File(imgFilePathPrefix); 
			ImageIO.write(image, "PNG", fileImg);
			
//			path = imgFilePathPrefix;
			
			
			String name=fileImg.getName();
			
			try{
		        BufferedImage input = ImageIO.read(fileImg);
		        Image big = input.getScaledInstance(Integer.valueOf(width), Integer.valueOf(height),Image.SCALE_DEFAULT);
		        BufferedImage inputbig = new BufferedImage(Integer.valueOf(width), Integer.valueOf(height),BufferedImage.TYPE_INT_BGR);
		        inputbig.getGraphics().drawImage(input, 0, 0, Integer.valueOf(width), Integer.valueOf(height), null); //画图


		            File file2 =new File(historyDataPlugin.pdfPicPath);            //此目录保存缩小后的关键图
		            if(file2.exists()){
//		                 System.out.println("多级目录已经存在不需要创建!!");
		            }else{
		              //如果要创建的多级目录不存在才需要创建。
		               file2.mkdirs();
		             }
		            ImageIO.write(inputbig, "PNG", new File(historyDataPlugin.pdfPicPath+ File.separator+name));   //将其保存在C:/imageSort/targetPIC/下
		            
		            path = historyDataPlugin.pdfPicPath+ File.separator+name;
		            
		        } catch (Exception ex) {
		        	ex.printStackTrace();
		        }
			
		}

		doc.close();
		return path;
	}

	/***
	 * PDF文件转PNG图片,全部页数
	 * 这种方法对于部分pdf来说,会转图片失败
	 * @param PdfFilePath
	 *            pdf完整路径
	 * @param imgFilePath
	 *            图片存放的文件夹
	 * @param dpi
	 *            dpi越大转换后越清晰,相对转换速度越慢
	 * @return
	 * @throws IOException
	 */
	public String pdf2Image(String PdfFilePath/* , int dpi */) throws IOException {
		System.out.println("开始读pdf文件");
		File file = new File(PdfFilePath);

		String path = "";

		PDDocument pdDocument;
		String imgPDFPath = file.getParent();
		int dot = file.getName().lastIndexOf('.');
		System.out.println("dot:" + dot);
		String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
		System.out.println("imagePDFName:" + imagePDFName);
		// String imgFolderPath = null;
		// if (dstImgFolder.equals("")) {
		// imgFolderPath = pdfPath + File.separator + imagePDFName;// 获取图片存放的文件夹路径
		// } else {
		// imgFolderPath = dstImgFolder + File.separator + imagePDFName;
		// }
		System.out.println("imgFolderPath:" + historyDataPlugin.pdfPicPath);
		// 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 = historyDataPlugin.pdfPicPath + File.separator + imagePDFName;
			imgFilePath = new StringBuffer();
			imgFilePath.append(imgFilePathPrefix);
			imgFilePath.append(".png");
			File dstFile = new File(imgFilePath.toString());
			path = imgFilePath.toString();
			// 根据pdf大小等比例转图片,此处第二个参数设置为1就是等比例抽取
			BufferedImage image = renderer.renderImage(i, 1F);
			// BufferedImage image = renderer.renderImageWithDPI(i, dpi);
			ImageIO.write(image, "png", dstFile);
		}
		System.out.println("PDF文档转PNG图片成功!");

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

使用的jar包:
fontbox-2.0.1.jar
pdfbox-2.0.1.jar
spire.pdf-2.2.0.jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小凯小凯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值