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;

/**
 * 
 * 
 * 普通图片格式转成PDF格式
 * 
* */ 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"); } }


转载于:https://www.cnblogs.com/zhqhlbt/archive/2012/12/18/4190655.html

你可能感兴趣的:(java)