자바 PDF 파일 을 JPG 파일 로 변환

6908 단어 Java
package com.gg.zzt.app.help;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.icepdf.core.pobjects.Document;
import org.icepdf.core.util.GraphicsRenderingHints;

import sun.misc.BASE64Decoder;

public class pdf2JpgHelper {

	static BASE64Decoder decoder = new sun.misc.BASE64Decoder();

	/**
	 * pdf   jpg
	 *
	 * @param pdffilepath
	 */
	public static void pdf2jpg(String pdffilepath, String jpgfilepath) {
		Document document = new Document();
		document.setFile(pdffilepath);
		float scale = 2.5f;//     
		float rotation = 0f;//     
		for (int i = 0; i < document.getNumberOfPages(); i++) {
			BufferedImage image = (BufferedImage) document.getPageImage(i,
					GraphicsRenderingHints.SCREEN,
					org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation,
					scale);
			RenderedImage rendImage = image;
			try {
				File file = new File(jpgfilepath);
				ImageIO.write(rendImage, "jpg", file);
			} catch (IOException e) {
				e.printStackTrace();
			}
			image.flush();
		}
		document.dispose();
	}

	/**
	 * base64 pdf
	 * @param base64sString
	 * @param pdffilepath
	 */
	public static void base64StringToPDF(String base64sString,String pdffilepath) {
		BufferedInputStream bin = null;
		FileOutputStream fout = null;
		BufferedOutputStream bout = null;
		try {
			//  base64             
			byte[] bytes = decoder.decodeBuffer(base64sString);
			// apache   API
			// byte[] bytes = Base64.decodeBase64(base64sString);
			//      bytes       ByteArrayInputStream  
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			//                      
			bin = new BufferedInputStream(bais);
			//        http://m.nvzi91.cn/nxby/29355.html
			//File file = new File("/home/amarsoft/test.pdf");
			File file = new File(pdffilepath);
			//            
			fout = new FileOutputStream(file);
			//                
			bout = new BufferedOutputStream(fout);

			byte[] buffers = new byte[1024];
			int len = bin.read(buffers);
			while (len != -1) {
				bout.write(buffers, 0, len);
				len = bin.read(buffers);
			}
			//                     ,      ,        
			bout.flush();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bin.close();
				fout.close();
				bout.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	//    
	public static boolean deleteFile(String fileName)
	{
		File file = new File(fileName);
		if(file.exists()&& file.isFile())
		{
			if(file.delete())
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String pdffilepath = "D:/55.pdf";
		String jpgfilepath = "D:/55.jpg";
//		pdf2jpg(pdffilepath, jpgfilepath);

		pdf2jpg(pdffilepath,jpgfilepath);
		//String base64 = "JVBERi0xLjQKJeLjz9MKNCAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDg0Nj4+c3RyZWFtCnictZjPaxNBFMe3hyjsRaTgIRfHS61g252d2d2kBcGfhRIWNhtti7GVtEnAJkVLpZQURfAQjaEVJNZDD4I9eOu5B8E/wEPFQ0HQkxdB8BQRwTgzG62ifVu7LyxMJ81OPvt9837N3tDPZHRmE0Y5yUzr5zO6p5tkRP6XEkNccmSWQTJlfeACJVTMCnrvicw1ee/OLQaZKv++yDaJSXk/c4KFJknKdYb6dq6o915651/MZOcWP7fi8rcMUvzH712+Iv5Oq+eBSQZAcoeKg7WNek/+Q/EoBowmAZgfG3+TG0NDJQAUKsgBQHk7t44GskHj+Vv1hdlY5XE1VU35X1CAFgQUOEQUD0WheTw1IY/fnjnlbtZ7kGRBYRyg8GSBgSxYk1rOwdsxKLwmuhZfYTCgyHq4tbrWyubGj3tPWtl5DJqV3J3WOHdfw2BAbl4+XeL/sTcL4qaR9vL08M/vuEXMRL9FyjpPGMG0pPt7WiluZ2aw1LHa87/W7qaNUgYXrWz20OpafsDdXvp2673bfTPb632qvYxoUSo0QibN+f51lPQrQSwENEtW/MggZoQrKg4W76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MDgnMDAnKS9DcmVhdG9yKEphc3BlclJlcG9ydHMgXChyZXBvcnQzMjIzXCkpL0NyZWF0aW9uRGF0ZShEOjIwMTcwNzI3MTgzMzU3KzA4JzAwJykvUHJvZHVjZXIoaVRleHQgMi4xLjcgYnkgMVQzWFQpPj4KZW5kb2JqCnhyZWYKMCAxMwowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDA5MjggMDAwMDAgbiAKMDAwMDAwMTIwNiAwMDAwMCBuIAowMDAwMDAxNjY5IDAwMDAwIG4gCjAwMDAwMDAwMTUgMDAwMDAgbiAKMDAwMDAwMTc5MyAwMDAwMCBuIAowMDAwMDAxMTcxIDAwMDAwIG4gCjAwMDAwMDEyOTQgMDAwMDAgbiAKMDAwMDAwMTQ4NSAwMDAwMCBuIAowMDAwMDAxODU2IDAwMDAwIG4gCjAwMDAwMDE5MTAgMDAwMDAgbiAKMDAwMDAwMTk0MyAwMDAwMCBuIAowMDAwMDAyMDQ4IDAwMDAwIG4gCnRyYWlsZXIKPDwvSW5mbyAxMiAwIFIvSUQgWzw2Mjg4ZWJmNDM3MjBhODgyYWU5MDFlOTgwMTdlOTczZT48NDY5MGIwYTRhMGMwNzgyOWUyNzQ1MTM4MDkxNDNkN2U+XS9Sb290IDExIDAgUi9TaXplIDEzPj4Kc3RhcnR4cmVmCjIyMDkKJSVFT0YK";
		//String ReqDate  = mTimer.getTime(2);
		//String imagePath = "D:\\\\"+"HD\\\\"+ ReqDate + "\\\\" + "123.pdf";
		//base64StringToPDF(base64,imagePath);
	}

}

비고: 이 코드 는 워 터 마크 가 없 는 icepdf 패 키 지 를 사용 해 야 실행 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기