KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PDFtoTIFF


1 import java.awt.image.ColorModel JavaDoc;
2 import java.io.*;
3 import org.faceless.pdf2.*;
4
5 /**
6  * This example converts a PDF to a multi-page TIFF, using the
7  * PDF Viewer extension.
8  *
9  * Usage is:
10  *
11  * java PDFtoTIFF [--dpi nnn] [--bw | --gray | --rgb | --rgba | --cmyk] <nnn.pdf>
12  *
13  * where dpi is the DPI of the final image (defaults to 72) and
14  * bw, gray and rgb cause the image to be created as 2-color
15  * black and white, grayscale or RGB (the default) respectively.
16  *
17  * The output filename is of the form "nnn.tif"
18  *
19  * Note converting to Black and White or GrayScale will take longer,
20  * as Java has to recolor the image as well.
21  *
22  * @since 2.5
23  * @version $Id: PDFtoTIFF.java,v 1.4 2005/09/07 11:20:50 mike Exp $
24  */

25 public class PDFtoTIFF
26 {
27     public static void main(String JavaDoc[] args)
28         throws IOException
29     {
30         ColorModel JavaDoc cm = PDFParser.RGB;
31         int dpi = 72;
32         String JavaDoc infile = null;
33
34         for (int i=0;i<args.length;i++) {
35             if (args[i].equals("--dpi")) {
36                 dpi = Integer.parseInt(args[++i]);
37             } else if (args[i].equals("--bw")) {
38                 cm = PDFParser.BLACKANDWHITE;
39             } else if (args[i].equals("--gray")) {
40                 cm = PDFParser.GRAYSCALE;
41             } else if (args[i].equals("--rgb")) {
42                 cm = PDFParser.RGB;
43             } else if (args[i].equals("--cmyk")) {
44                 cm = PDFParser.CMYK;
45             } else if (args[i].equals("--rgba")) {
46                 cm = PDFParser.RGBA;
47             } else {
48                 infile = args[i];
49             }
50         }
51         if (infile==null) {
52             System.err.println("Usage: java PDFtoTIFF [--dpi nnn] [--bw | --gray | --rgb | --rgba | --cmyk] <nnn.pdf>\n\n");
53             System.exit(-1);
54         }
55         
56         String JavaDoc outfile;
57         if (infile.endsWith(".pdf") || infile.endsWith(".PDF")) {
58             outfile = infile.substring(0, infile.length()-4)+".tif";
59         } else {
60             outfile = infile+".tif";
61         }
62
63         System.out.print("Reading \""+infile+"\"... ");
64         PDF pdf = new PDF(new PDFReader(new File(infile)));
65
66         System.out.print("Writing \""+outfile+"\"... ");
67         FileOutputStream out = new FileOutputStream(outfile);
68         PDFParser parser = new PDFParser(pdf);
69         parser.writeAsTIFF(out, dpi, cm);
70         out.close();
71         System.out.println();
72     }
73 }
74
Popular Tags