首页>代码>spring mvc简单的图片与PDF文件互转>/com.pdf/src/main/java/com/pdf/util/ImagesPDFUtil.java
package com.pdf.util;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

@SuppressWarnings("restriction")
public class ImagesPDFUtil {

	/**
	 * 图片转换为PDF
	 * @param imagePath
	 * @param mOutputPdfFileName
	 * @return
	 */
	public File jpg2Pdf(String imagePath, String mOutputPdfFileName) {
		Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
		try {
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(
					mOutputPdfFileName));
			doc.open();
			doc.newPage();
			com.lowagie.text.Image png1 = com.lowagie.text.Image
					.getInstance(imagePath);
			float heigth = png1.getHeight();
			float width = png1.getWidth();
			int percent = this.getPercent2(heigth, width);
			png1.setAlignment(com.lowagie.text.Image.MIDDLE);
			png1.setAlignment(com.lowagie.text.Image.TEXTWRAP);
			png1.scalePercent(percent + 3);
			doc.add(png1);
			doc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		File mOutputPdfFile = new File(mOutputPdfFileName);
		if (!mOutputPdfFile.exists()) {
			mOutputPdfFile.deleteOnExit();
			return null;
		}
		System.out.println(mOutputPdfFile);
		return mOutputPdfFile;
	}
	
	/**
	 * 图片处理
	 * @param h
	 * @param w
	 * @return
	 */
	private int getPercent2(float h, float w) {
		int p = 0;
		float p2 = 0.0f;
		p2 = 530 / w * 100;
		p = Math.round(p2);
		return p;
	}

	
	/**
	 * pdf文件转换为jpg文件
	 * 
	 * @param pdfFile
	 * @param jpgFile
	 * @throws IOException
	 */
	public void pdfjpg(String pdfFile, String jpgFile){
		try {
			
			File file = new File(pdfFile);
			RandomAccessFile raf = new RandomAccessFile(file, "r");
			System.out.println(raf);
			FileChannel channel = raf.getChannel();
			ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
					channel.size());
			PDFFile pdffile = new PDFFile(buf);

			System.out.println("页数: " + pdffile.getNumPages());
			int with = 0;
			int height = 0;
			for (int i = 1; i <= pdffile.getNumPages(); i++) {
				// draw the first page to an image
				PDFPage page = pdffile.getPage(i);
				with = (int) page.getBBox().getWidth();
				height += (int) page.getBBox().getHeight();
			}
			Rectangle rect = new Rectangle(0, 0, with, height);
			BufferedImage tag = new BufferedImage(rect.width, rect.height,
					BufferedImage.TYPE_INT_RGB);
			for (int j = 1; j <= pdffile.getNumPages(); j++) {
				PDFPage page = pdffile.getPage(j);
				Rectangle rect1 = new Rectangle(0, 0, (int) page.getBBox()
						.getWidth(), (int) page.getBBox().getHeight());
				Image img = page.getImage(rect1.width, rect1.height, // width &
						// height
						rect1, // clip rect
						null, // null for the ImageObserver
						true, // fill background with white
						true // block until drawing is done
						);

				tag.getGraphics().drawImage(img, 0, rect1.height * (j - 1),
						rect1.width, rect1.height, null);
			}
			FileOutputStream out = new FileOutputStream(jpgFile); // 输出到文件流
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(tag); // JPEG编码
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImageFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) {
		
//		new ImagesPDFUtil().jpg2Pdf("E:\\verifies\\4jyb.jpg", "E:\\verifies\\4jyb.pdf");
		
		
//		new ImagesPDFUtil().pdfjpg("E:\\verifies\\4jyb.pdf","E:\\verifies\\111.jpg");

	}
	
}
最近下载更多
Sisphyus  LV1 2022年2月17日
我的最代码  LV20 2022年1月14日
xuexizhuanyong23  LV16 2021年9月24日
xmjying  LV13 2020年3月19日
lsongssl  LV2 2019年6月14日
DU-ZB  LV13 2019年3月27日
麻辣小龙虾  LV11 2019年3月12日
zhangshu6682  LV7 2019年1月6日
tanyunqing  LV8 2018年7月11日
gggbbbb  LV1 2018年6月12日
最近浏览更多
ssy552  LV10 2023年2月23日
xytthy  LV3 2022年4月25日
Chen2618kk 2022年3月19日
暂无贡献等级
Sisphyus  LV1 2022年2月17日
我的最代码  LV20 2022年1月14日
lvhaowen  LV20 2021年11月6日
xuexizhuanyong23  LV16 2021年9月24日
xsp990218  LV5 2021年8月16日
13043860zj  LV16 2021年5月24日
Cat. 2021年5月16日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友