PDFToClassF.java
import java.io.*; import java.util.*; import java.awt.image.*; import java.awt.geom.*; import java.awt.*; import javax.media.jai.*; import com.sun.media.jai.codec.*; import org.faceless.pdf2.*; // Quick and dirty example showing how to convert a PDF // to a TIFF class F file. // // Usage: java PDFToClassF file.pdf // Creates a file called "fax.tif" // public class PDFToClassF { static final float dpix = 204, dpiy = 196; public static void main(String[] args) throws Exception { PDF pdf = new PDF(new PDFReader(new File(args[0]))); PDFParser parser = new PDFParser(pdf); byte[] b = new byte[] { -1, 0 }; ColorModel model = new IndexColorModel(1, 2, b, b, b); BufferedImage[] images = new BufferedImage[pdf.getNumberOfPages()]; for (int i=0;i<images.length;i++) { PDFPage page = pdf.getPage(i); PagePainter painter = parser.getPagePainter(i); int w = Math.round(page.getWidth() * dpix / 72); int h = Math.round(page.getHeight() * dpiy / 72); float[] box = page.getBox("ViewBox"); WritableRaster raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, w, h, 1, 1, null); images[i] = new BufferedImage(model, raster, false, null); Graphics2D graphics = (Graphics2D)images[i].createGraphics(); // You might want to test the following two to see if they make any // appreciable difference in terms of speed or quality. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT); float dpi = (float)Math.max(dpix, dpiy); if (dpix!=dpiy) { graphics.transform(new AffineTransform(dpix/dpi, 0, 0, dpiy/dpi, 0, 0)); } painter.drawSubImage(graphics, box[0], box[1], box[2], box[3], dpi); graphics.dispose(); } FileOutputStream out = new FileOutputStream("fax.tif"); TIFFEncodeParam param = new TIFFEncodeParam(); param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_2D); TIFFField xres = new TIFFField(0x11A, TIFFField.TIFF_RATIONAL, 1, new long[][]{{ (long)dpix, 1 }}); TIFFField yres = new TIFFField(0x11B, TIFFField.TIFF_RATIONAL, 1, new long[][]{{ (long)dpiy, 1 }}); param.setExtraFields(new TIFFField[] { xres, yres }); ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out, param); if (images.length > 1) { param.setExtraImages(Arrays.asList(images).subList(1, images.length).iterator()); } encoder.encode(images[0]); out.close(); } }