packagecn.ljl.javaweb.demo.pdf2tiff;import staticcn.ljl.javaweb.demo.pdf2tiff.Pdf2TiffConstant.DPI;import staticcn.ljl.javaweb.demo.pdf2tiff.Pdf2TiffConstant.IMG_FORMAT;importjava.awt.image.BufferedImage;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.ArrayList;importjava.util.List;importjavax.media.jai.JAI;importjavax.media.jai.PlanarImage;importorg.apache.pdfbox.pdmodel.PDDocument;importorg.apache.pdfbox.rendering.ImageType;importorg.apache.pdfbox.rendering.PDFRenderer;importcom.sun.media.jai.codec.ImageCodec;importcom.sun.media.jai.codec.ImageEncoder;importcom.sun.media.jai.codec.TIFFEncodeParam;public classPdf2TiffUtil {/*** 从输入流读取pdf,转化为tiff后写入输出流.
* 参考列表:
*
*
* "http://www.coderanch.com/t/497492/java/java/Convert-PDF-files-Tiff-files"
* >Convert PDF files to Tiff files
*
* "http://www.oracle.com/technetwork/cn/java/javaee/downloads/readme-1-1-2-137176.html"
* >Java(TM) Advanced Imaging API README
*
*
*@paramis
* 输入流,提供pfg内容.
*@paramos
* 输出流.*/
public static voidpdf2Tiff(InputStream is, OutputStream os) {
PDDocument doc= null;try{
doc=PDDocument.load(is);int pageCount =doc.getNumberOfPages();
PDFRenderer renderer= new PDFRenderer(doc); //根据PDDocument对象创建pdf渲染器
List piList = new ArrayList(pageCount - 1);for (int i = 0 + 1; i < pageCount; i++) {
BufferedImage image=renderer.renderImageWithDPI(i, DPI,
ImageType.RGB);
PlanarImage pimg= JAI.create("mosaic", image);
piList.add(pimg);
}
TIFFEncodeParam param= new TIFFEncodeParam();//创建tiff编码参数类
param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);//压缩参数
param.setExtraImages(piList.iterator());//设置图片的迭代器
BufferedImage fimg= renderer.renderImageWithDPI(0, DPI,
ImageType.RGB);
PlanarImage fpi= JAI.create("mosaic", fimg); //通过JAI的create()方法实例化jai的图片对象
ImageEncoder enc=ImageCodec.createImageEncoder(IMG_FORMAT, os,
param);
enc.encode(fpi);//指定第一个进行编码的jai图片对象,并将输出写入到与此
} catch(IOException e) {
e.printStackTrace();
}finally{try{if (doc != null)
doc.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}