package com.icepdf;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import org.icepdf.core.util.GraphicsRenderingHints;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;
public class PdfToTiff {
static {
System.setProperty("com.sun.media.jai.disableMediaLib", "true");
}
public String changePdfToTiff(String srcPath,String outPath) throws Exception {
File fileIn = new File(srcPath);
if(!fileIn.exists()){
throw new RuntimeException("源文件不存在!");
}
File fileOut = new File(outPath);
OutputStream os = new FileOutputStream(fileOut);
if (!srcPath.endsWith(".pdf")) {
throw new RuntimeException("请检查参数pdfName文件名是否以.pdf结尾!");
}
org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
document.setFile(srcPath);
float scale = 2f;//比例
float rotation = 0f;//旋转
List<BufferedImage> bufferImgList = new ArrayList<BufferedImage>(document.getNumberOfPages() - 1);
for (int i = 0 + 1; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN,
org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
bufferImgList.add(image);
}
TIFFEncodeParam param = new TIFFEncodeParam();// 创建tiff编码参数类
param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);// 压缩参数
param.setExtraImages(bufferImgList.iterator());// 设置图片的迭代器
BufferedImage fimg = (BufferedImage) document.getPageImage(0, GraphicsRenderingHints.SCREEN,
org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
PlanarImage fpi = JAI.create("mosaic", fimg); // 通过JAI的create()方法实例化jai的图片对象
ImageEncoder enc = ImageCodec.createImageEncoder("tiff", os, param);
enc.encode(fpi);// 指定第一个进行编码的jai图片对象,并将输出写入到与此
document.dispose();
return "";
}
public static void main(String[] args) {
try {
PdfToTiff pdfToTiff = new PdfToTiff();
System.out.println("start");
System.out.println(System.currentTimeMillis());
String srcPath="E:\\正式文件.pdf";
String outPath="E:\\aaa\\正式文件.tiff";
pdfToTiff.changePdfToTiff(srcPath,outPath);
System.out.println(System.currentTimeMillis());
System.out.println("end");
} catch (Exception e) {
e.printStackTrace();
}
}
}