TIF 文档转换为 PDF文档


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import org.apache.log4j.Logger;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Utilities;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;

/**
 * 
 * <pre>
 * 普通图片格式转成PDF格式
 * </pre>
 * 
 */
public class Image2Pdf {
	private static Logger logger = Logger.getLogger(Image2Pdf.class);

	public static boolean convert(String oldPath, String newPath) {
		logger.info("由文件[" + oldPath + "]转为[" + newPath + "]---开始!");
		// 创建一个文档对象
		Document doc = new Document();
		try {
			// 定义输出文件的位置
			PdfWriter.getInstance(doc, new FileOutputStream(newPath));
			// 开启文档
			doc.open();

			if (!oldPath.endsWith(".tif")) {
				Image jpg = Image.getInstance(oldPath); // 原来的图片的路径
				// 获得图片的高度
				float heigth = jpg.getHeight();
				float width = jpg.getWidth();
				// 合理压缩,h>w,按w压缩,否则按w压缩
				int percent = getPercent(heigth, width);

				// 设置图片居中显示
				jpg.setAlignment(Image.MIDDLE);
				// 按百分比显示图片的比例
				if (width > 1024 || heigth > 786) {
					jpg.scalePercent(percent);
				}
				doc.add(jpg);
			} else
			// tiff多页
			{
				Object localObject1 = null;
				Object localObject2 = null;
				Image localImage1 = null;
				URL paramURL = Utilities.toURL(oldPath);
				try {
					if (paramURL.getProtocol().equals("file")) {
						localObject2 = paramURL.getFile();
						localObject2 = Utilities
								.unEscapeURL((String) localObject2);
						localObject1 = new RandomAccessFileOrArray(
								(String) localObject2);
					} else {
						localObject1 = new RandomAccessFileOrArray(paramURL);
					}

					int pageNums = TiffImage
							.getNumberOfPages((RandomAccessFileOrArray) localObject1);
					if (pageNums > 0) {
						for (int i = 1; i <= pageNums; i++) {
							localObject2 = TiffImage.getTiffImage(
									(RandomAccessFileOrArray) localObject1, i);
							Image jpg = (Image) localObject2;
							// 获得图片的高度
							float heigth = jpg.getHeight();
							float width = jpg.getWidth();
							// 合理压缩,h>w,按w压缩,否则按w压缩
							int percent = getPercent(heigth, width);

							// 设置图片居中显示
							jpg.setAlignment(Image.MIDDLE);
							// 按百分比显示图片的比例
							if (width > 1024 || heigth > 786) {
								jpg.scalePercent(percent);
							}
							doc.add(jpg);
						}
					}
					if (localObject1 != null)
						((RandomAccessFileOrArray) localObject1).close();

				} finally {
					if (localObject1 != null)
						((RandomAccessFileOrArray) localObject1).close();
				}
			}
		} catch (FileNotFoundException e) {
			logger.error("由文件[" + oldPath + "]转为[" + newPath + "]失败!,原因:" + e.getMessage());
			e.printStackTrace();
		} catch (DocumentException e) {
			logger.error("由文件[" + oldPath + "]转为[" + newPath + "]失败!,原因:" + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			logger.error("由文件[" + oldPath + "]转为[" + newPath + "]失败!,原因:" + e.getMessage());
			e.printStackTrace();
		} finally {
			if (doc != null) {
				doc.close();
			}
		}
		logger.info("由文件[" + oldPath + "]转为[" + newPath + "]---结束!");
		return true;
	}

	/**
	 * 第一种解决方案 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
	 * 
	 * @param h
	 * @param w
	 * @return
	 */

	public static int getPercent(float h, float w) {
		int p = 0;
		float p2 = 0.0f;
		if (h > w) {
			p2 = 210 / h * 279;
		} else {
			p2 = 210 / w * 279;
		}
		p = Math.round(p2);
		return p;
	}

	/**
	 * 第二种解决方案,统一按照宽度压缩 这样来的效果是,所有图片的宽度是相等的
	 * 
	 * @param args
	 */
	public static int getPercent2(float h, float w) {
		int p = 0;
		float p2 = 0.0f;
		p2 = 530 / w * 100;
		p = Math.round(p2);
		return p;
	}

	public static void main(String[] args) {
		convert("D:\\bak\\20121207\\document4.tif", "D:\\bak\\20121207\\document4.pdf");
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值