通过java的第三方iText包(iText-2.1.6.jar下载)读取PDF格式二进制字符文件为对应的Document文件,再jai(jai_core.jar、jai_codec.jar及jai_imageio-1.1-alpha.jar等下载)的TIFF格式化工具进行图像转换输入出tiff格式的图像文件,具体代码如下所示
import java.awt.Graphics;@b@import java.awt.Transparency;@b@import java.awt.image.BufferedImage;@b@import java.awt.image.DataBuffer;@b@import java.awt.image.IndexColorModel;@b@import java.awt.image.RenderedImage;@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.IOException;@b@@b@import javax.imageio.IIOImage;@b@import javax.imageio.ImageIO;@b@import javax.imageio.ImageWriteParam;@b@import javax.imageio.ImageWriter;@b@import javax.imageio.spi.IIORegistry;@b@import javax.imageio.stream.ImageOutputStream;@b@@b@import org.icepdf.core.exceptions.PDFException;@b@import org.icepdf.core.exceptions.PDFSecurityException;@b@import org.icepdf.core.pobjects.Document;@b@import org.icepdf.core.pobjects.PDimension;@b@import org.icepdf.core.pobjects.Page;@b@import org.icepdf.core.util.GraphicsRenderingHints;@b@@b@import sun.misc.BASE64Encoder;@b@import com.sun.media.imageioimpl.plugins.tiff.TIFFImageReaderSpi;@b@import com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriterSpi;@b@import com.sun.media.jai.codec.ImageCodec;@b@import com.sun.media.jai.codec.ImageEncoder;@b@import com.sun.media.jai.codec.TIFFEncodeParam;@b@import com.sun.media.jai.codec.TIFFField;@b@import com.sun.media.jai.codecimpl.TIFFImageDecoder;@b@@b@public class PdfConvertTiffUtil {@b@ @b@ public static final double PRINTER_RESOLUTION = 300.0;@b@ public static final String COMPRESSION_TYPE = "CCITT T.6";@b@ public static final long[] dpiData = {300l, 300l};@b@ @b@ static {@b@ IIORegistry registry = IIORegistry.getDefaultInstance();@b@ registry.registerServiceProvider(new TIFFImageWriterSpi());@b@ registry.registerServiceProvider(new TIFFImageReaderSpi());@b@ }@b@ @b@ public static ByteArrayOutputStream[] pdftoGif(String pdfPath) throws Exception {@b@ ByteArrayOutputStream bos = new ByteArrayOutputStream();@b@ FileInputStream fis = new FileInputStream(new File(pdfPath));@b@ byte[] tempbuff = new byte[1024];@b@ while(fis.read(tempbuff)!=-1){@b@ bos.write(tempbuff);@b@ }@b@ fis.close();@b@ ByteArrayOutputStream[] bytess = PDF2TIFF( bos.toByteArray());@b@ return bytess;@b@ }@b@@b@ public static ByteArrayOutputStream[] PDF2TIFF(byte[] pdfBytes) {@b@ Document document = new Document();@b@ try {@b@ document.setByteArray(pdfBytes, 0, pdfBytes.length, null);@b@ } catch (PDFException ex) {@b@ System.out.println("Error parsing PDF document " + ex);@b@ } catch (PDFSecurityException ex) {@b@ System.out.println("Error encryption not supported " + ex);@b@ } catch (IOException ex) {@b@ System.out.println("Error handling PDF document " + ex);@b@ }@b@ ByteArrayOutputStream[] tiffStreams = new ByteArrayOutputStream[document.getNumberOfPages()];@b@ try {@b@ for (int i = 0; i < document.getNumberOfPages(); i++) {@b@ ByteArrayOutputStream bout = new ByteArrayOutputStream();@b@ ImageOutputStream ios = ImageIO.createImageOutputStream(bout);@b@ ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();@b@ writer.setOutput(ios);@b@ final double targetDPI = PRINTER_RESOLUTION;@b@ float scale = 1.0f;@b@ float rotation = 0f;@b@ PDimension size = document.getPageDimension(i, rotation, scale);@b@ double dpi = Math.sqrt((size.getWidth() * size.getWidth())@b@ + (size.getHeight() * size.getHeight()))@b@ / Math.sqrt((8.5 * 8.5) + (11 * 11));@b@ if (dpi < (targetDPI - 0.1)) {@b@ scale = (float) (targetDPI / dpi);@b@ size = document.getPageDimension(i, rotation, scale);@b@ }@b@ int pageWidth = (int) size.getWidth();@b@ int pageHeight = (int) size.getHeight();@b@ int[] cmap = new int[] { 0xFF000000, 0xFFFFFFFF };@b@ IndexColorModel cm = new IndexColorModel(1, cmap.length, cmap,@b@ 0, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);@b@ BufferedImage image = new BufferedImage(pageWidth, pageHeight,@b@ BufferedImage.TYPE_BYTE_BINARY, cm);@b@ Graphics g = image.createGraphics();@b@ document.paintPage(i, g, GraphicsRenderingHints.PRINT,@b@ Page.BOUNDARY_CROPBOX, rotation, scale);@b@ g.dispose();@b@ IIOImage img = new IIOImage(image, null, null);@b@ ImageWriteParam param = writer.getDefaultWriteParam();@b@ param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);@b@ param.setCompressionType(COMPRESSION_TYPE);@b@ writer.write(null, img, param);@b@ image.flush();@b@ ios.flush();@b@ ios.close();@b@ writer.dispose();@b@ tiffStreams[i] = changeResolution(bout.toByteArray());//转化TIF分辨率@b@ bout.close();@b@@b@ }@b@ } catch (IOException e) {@b@ System.out.println("Error saving file " + e);@b@ e.printStackTrace();@b@ }@b@ document.dispose();@b@ @b@ return tiffStreams;@b@ }@b@ @b@ /**@b@ * 修改tiff的像素值@b@ * @param tiffpath@b@ * @throws IOException @b@ */@b@ public static ByteArrayOutputStream changeResolution (byte[] bout) {@b@ @b@ try {@b@ RenderedImage renderedImage = ImageIO.read(new ByteArrayInputStream(bout));@b@ TIFFEncodeParam param = new TIFFEncodeParam();@b@ param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);@b@ if(dpiData[0]>0 || dpiData[1] >0){@b@ int fieldCount = 1;@b@ if(dpiData[0]>0 && dpiData[1] >0){@b@ fieldCount = 2;@b@ }@b@ TIFFField[] tiffFields = new TIFFField[fieldCount];@b@ int index =0;@b@ if(dpiData[0]>0){@b@ long xdpiArray[][] = new long[1][2];@b@ xdpiArray[0] = new long[] { dpiData[0], 1 };@b@ TIFFField xRes = new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,@b@ TIFFField.TIFF_RATIONAL, 1, xdpiArray);@b@ tiffFields[index++] = xRes;@b@ }@b@ if(dpiData[1]>0){@b@ long ydpiArray[][] = new long[1][2];@b@ ydpiArray[0] = new long[] { dpiData[1], 1 };@b@ TIFFField yRes = new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,@b@ TIFFField.TIFF_RATIONAL, 1, ydpiArray);@b@ tiffFields[index] = yRes;@b@ }@b@ param.setExtraFields(tiffFields); @b@ }@b@ ByteArrayOutputStream out = new ByteArrayOutputStream(); // 输出到文件流 @b@ ImageEncoder enc = ImageCodec.createImageEncoder("TIFF", out, param);@b@ enc.encode(renderedImage);@b@ out.close();@b@ return out;@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ return null;@b@ }@b@ @b@ public static void deleteFile(File file){ @b@ if(file.exists()){ @b@ if(file.isFile()){ @b@ file.delete(); @b@ }else if(file.isDirectory()){ @b@ File files[] = file.listFiles(); @b@ for(int i=0;i<files.length;i++){ @b@ files[i].delete(); @b@// this.deleteFile(files[i]); @b@ } @b@ file.delete(); @b@ } @b@ }else{ @b@ System.out.println("所删除的文件不存在!"+'\n'); @b@ } @b@ }@b@ @b@ public static String image2Str(ByteArrayOutputStream data){@b@ if (data == null)@b@ return null;@b@ BASE64Encoder encoder = new BASE64Encoder();@b@ return encoder.encode(data.toByteArray());@b@ }@b@ @b@ public static ByteArrayOutputStream readImage(String filePath) throws Exception{@b@ ByteArrayOutputStream bos = new ByteArrayOutputStream();@b@ FileInputStream fis = new FileInputStream(filePath);@b@ byte[] array = new byte[10240];@b@ int idx = -1;@b@ while ((idx = fis.read(array)) > 0) {@b@ bos.write(array,0,idx);@b@ }@b@ fis.close();@b@ bos.close();@b@ return bos;@b@ }@b@}