ZIP包解压后将PDF文件转为JPG

zip包解压后将pdf文件转为jpg

需要导入两个iar包
apache-ant-1.7.0.jar
icepdf-core-4.3.3.jar

上传规则

1.必须将pdf文件进行打包,不能对文件夹进行打包

package com.pdf.service.impl;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.imageio.ImageIO;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

//import com.github.junrar.Archive;
//import com.github.junrar.rarfile.FileHeader;


public class NewsServiceImpl {
	
	public static void main(String[] args) throws Exception {
		
		NewsServiceImpl n = new NewsServiceImpl();
		boolean unZipFiles = n.unZipFiles("C:/Users/Administrator/Desktop/_ao#ZHONG你/20191126/uigd.zip", "C:/Users/Administrator/Desktop/_ao#zhong/20191126");
		
		if(unZipFiles==false){
			
			System.out.println("请输入正确的.zip文件");
			
		}
	}

	
	
	/**
	 * 解压zip格式的压缩文件到指定位置
	 * 
	 * @param zipFileName
	 *            压缩文件
	 * @param extPlace
	 *            解压目录
	 *           
	 * @throws Exception
	 * ocean
	 * 
	 * 20191013
	 */
	public boolean unZipFiles(String zipFileName, String extPlace) throws Exception {
		//取出文件夹中的类型文件
		 File tempFiles =new File(zipFileName.trim());
		 String name = tempFiles.getName();
		//文件类型校验
		if(name.contains(".zip")){
		
		//解决java将文件压缩到zip里面中文的文件名称乱码
		System.setProperty("sun.zip.encoding", "utf-8");
		System.getProperty("sun.jnu.encoding", "utf-8");
		try {
			//根据所传参数创建文件夹
			(new File(extPlace)).mkdirs();
			File f = new File(zipFileName);
			ZipFile zipFile = new ZipFile(zipFileName, "gbk"); // 处理中文文件名乱码的问题
			if ((!f.exists()) && (f.length() <= 0)) {
				throw new Exception("要解压的文件不存在!");
			}
			String strPath, gbkPath, strtemp;
			File tempFile = new File(extPlace);
			//获取文件的绝对路径
			strPath = tempFile.getAbsolutePath();
			//开始解压
			Enumeration e = zipFile.getEntries();
			while (e.hasMoreElements()) {
				ZipEntry zipEnt = (ZipEntry) e.nextElement();
				gbkPath = zipEnt.getName();
				if (zipEnt.isDirectory()) {
					strtemp = strPath + File.separator + gbkPath;
					File dir = new File(strtemp);
					dir.mkdirs();
					continue;
				} else {
					// 读写文件
					InputStream is = zipFile.getInputStream(zipEnt);
					BufferedInputStream bis = new BufferedInputStream(is);
					gbkPath = zipEnt.getName();
					strtemp = strPath + File.separator + gbkPath;

					// 建目录
					String strsubdir = gbkPath;
					for (int i = 0; i < strsubdir.length(); i++) {
						if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
							String temp = strPath + File.separator + strsubdir.substring(0, i);
							File subdir = new File(temp);
							if (!subdir.exists())
								subdir.mkdir();
						}
					}
					//文件解压结束
					FileOutputStream fos = new FileOutputStream(strtemp);
					BufferedOutputStream bos = new BufferedOutputStream(fos);
					int c;
					while ((c = bis.read()) != -1) {
						bos.write((byte) c);
					}
					bos.close();
					fos.close();
					//
					File strtempdir = new File(strtemp);
					if (strtempdir.exists()) {
						//处理pdf文件
						pdf2Pic(strtemp, extPlace);
					}
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
		return false;
}
	
	
	public void pdf2Pic(String pdfPath, String path)
			throws PDFException, PDFSecurityException, IOException {
		Document document = new Document();
		document.setFile(pdfPath);
		float scale = 10.0f;// 缩放比例
		float rotation = 0f;// 旋转角度

		for (int i = 0; i < document.getNumberOfPages(); i++) {
			//对pdf进行格式转化★
			/*
			 * i 页码 
			 * GraphicsRenderingHints.SCREEN 制图编译器提示的屏幕显示  默认值为1
			 * org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX 边界区域大小 默认值为2
			 * rotation 旋转角度 正值为逆时针方向
			 * scale 缩放比例  基础文件1266k 1:183k 2:573k 2.5:820k 2.6:866 2.7:921 2.8:960 2.9:1026 3:1068k 3.1:1122k 3.2:1180 3.3:1230 3.4:1284 4:1623    缩小
			 *                        685k 1:252k 2:896k 2.5:1294k 2.6:1371 2.7:1454 2.8:1542 2.9:1627 3:1710k 3.1:1803k 3.2:1889  3.3:1976 3.4:2069 4:2612 放大
			 */                     
			
			BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
					5, rotation, scale);
			RenderedImage rendImage = image;
			try {
				String jpgPath = pdfPath.replace(".pdf", ".jpg");
				File file = new File(jpgPath);
				ImageIO.write(rendImage, "jpg", file);
				

			} catch (IOException e) {
				e.printStackTrace();
			}
			image.flush();
		}
		document.dispose();
	}

	
}


你可能感兴趣的:(程序人生)