/* * Created on Aug 14, 2008 * */ package jPDFImagesSamples; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import com.qoppa.pdf.PDFPassword; import com.qoppa.pdf.TIFFOptions; import com.qoppa.pdfImages.PDFImages; public class PDFImagesCLI { private final static int OF_TIFF = 0; private final static int OF_JPEG = 1; private final static int OF_PNG = 2; private final static String OF_JPEG_STRING = "jpeg"; private final static String OF_JPG_STRING = "jpg"; private final static String OF_TIFF_STRING = "tiff"; private final static String OF_PNG_STRING = "png"; private static class JobInfo { public String m_InputFile; public String m_OutputFile; public int m_OutputFormat; private PDFPassword m_Password; private int m_DPI = 150; private float m_JPEGQuality = 0.8f; private String m_TIFFCompression = TIFFOptions.TIFF_PACKBITS; private String m_LicenseKey; private void validate() { // Check that the output file is defined if (m_OutputFile == null) { System.out.println ("Missing output file."); System.exit(-1); } File outputFile = new File (m_OutputFile); if (m_OutputFormat == OF_TIFF) { if (outputFile.exists()) { try { System.out.println ("Overwrite " + outputFile.getName() + " (Y/N)?"); BufferedReader inReader = new BufferedReader (new InputStreamReader (System.in)); String readLine = inReader.readLine(); if ("y".equalsIgnoreCase(readLine) == false && "yes".equalsIgnoreCase(readLine) == false) { System.exit(-1); } inReader.close(); } catch (IOException ioE) { System.err.println (ioE.getMessage()); System.exit (-1); } } } else { if (outputFile.exists() && outputFile.isDirectory() == false) { System.err.println ("Output file must be a folder."); System.exit (-1); } else if (outputFile.exists() == false) { outputFile.mkdirs(); } } } } public static void main (String [] args) { try { // Parse the arguments JobInfo jobInfo = parseArgs (args); // check dirs and overwriting jobInfo.validate(); // Check for the key if (jobInfo.m_LicenseKey != null) { PDFImages.setKey (jobInfo.m_LicenseKey); } // Load the document PDFImages pdfDoc = new PDFImages (jobInfo.m_InputFile, jobInfo.m_Password); // Export images if (jobInfo.m_OutputFormat == OF_TIFF) { TIFFOptions options = new TIFFOptions (jobInfo.m_DPI, jobInfo.m_TIFFCompression); pdfDoc.saveDocumentAsTIFF (jobInfo.m_OutputFile, options); } else { File outputDir = new File (jobInfo.m_OutputFile); // Output file prefix String outPrefix = jobInfo.m_InputFile; if (outPrefix.toLowerCase().endsWith(".pdf")) { outPrefix = outPrefix.substring(0, outPrefix.length() - 4); } // Loop through the output pages for (int pageIx = 0; pageIx < pdfDoc.getPageCount(); ++pageIx) { String outFileName = outPrefix + "_" + pageIx; if (jobInfo.m_OutputFormat == OF_JPEG) { File outputFile = new File (outputDir, outFileName + ".jpg"); pdfDoc.savePageAsJPEG(pageIx, outputFile.getAbsolutePath(), jobInfo.m_DPI, jobInfo.m_JPEGQuality); } else { File outputFile = new File (outputDir, outFileName + ".png"); pdfDoc.savePageAsPNG(pageIx, outputFile.getAbsolutePath(), jobInfo.m_DPI); } } } } catch (Throwable t) { System.out.println (t.getMessage()); System.exit(-1); } } private static JobInfo parseArgs (String [] args) { if (args.length == 0) { printUsage(); System.exit (0); } // Create new job info JobInfo jobInfo = new JobInfo(); // Loop through the arguments int ix = 0; while (ix < args.length) { if ("-output".equalsIgnoreCase(args [ix])) { jobInfo.m_OutputFile = args [ix+1]; ix += 2; } else if ("-pwd".equalsIgnoreCase(args [ix])) { jobInfo.m_Password = new PDFPassword (args [ix+1]); ix += 2; } else if ("-format".equalsIgnoreCase(args [ix])) { jobInfo.m_OutputFormat = getFormat (args [ix+1]); ix += 2; } else if (args [ix].equalsIgnoreCase("-dpi")) { jobInfo.m_DPI = Integer.valueOf(args [ix+1]).intValue(); ix += 2; } else if (args [ix].equalsIgnoreCase("-jpegqual")) { jobInfo.m_JPEGQuality = Float.valueOf(args [ix+1]).floatValue(); jobInfo.m_JPEGQuality = Math.max (0.01f, jobInfo.m_JPEGQuality); jobInfo.m_JPEGQuality = Math.min (1f, jobInfo.m_JPEGQuality); ix += 2; } else if (args [ix].equalsIgnoreCase("-tiffcomp")) { jobInfo.m_TIFFCompression = getTIFFCompression(args [ix+1]); ix += 2; } else if ("-key".equalsIgnoreCase(args [ix])) { jobInfo.m_LicenseKey = args [ix+1]; ix += 2; } else if (args [ix].startsWith("-") == false) { jobInfo.m_InputFile = args [ix]; ++ix; } else { throw new RuntimeException ("Unrecognized command line option: " + args [ix]); } } // Check that we got an input file if (jobInfo.m_InputFile == null) { throw new RuntimeException ("Missing input file."); } // Check that the input file exists and is not a folder File inputFile = new File (jobInfo.m_InputFile); if (inputFile.exists() == false) { throw new RuntimeException ("Invalid input file: " + jobInfo.m_InputFile); } else if (inputFile.isDirectory() == true) { throw new RuntimeException ("File can not be a folder."); } return jobInfo; } private static int getFormat (String formatName) { if (OF_JPEG_STRING.equalsIgnoreCase(formatName) || OF_JPG_STRING.equalsIgnoreCase(formatName)) { return OF_JPEG; } else if (OF_TIFF_STRING.equalsIgnoreCase(formatName)) { return OF_TIFF; } else if (OF_PNG_STRING.equalsIgnoreCase(formatName)) { return OF_PNG; } else { throw new RuntimeException ("Invalid format name: " + formatName); } } private static String getTIFFCompression (String option) { if ("deflate".equalsIgnoreCase(option)) { return TIFFOptions.TIFF_DEFLATE; } else if ("ccitt_rle".equalsIgnoreCase(option)) { return TIFFOptions.TIFF_CCITT_RLE; } else if ("fax_group_3".equalsIgnoreCase(option)) { return TIFFOptions.TIFF_FAX_GROUP3; } else if ("fax_group_4".equalsIgnoreCase(option)) { return TIFFOptions.TIFF_FAX_GROUP4; } else if ("lzw".equalsIgnoreCase(option)) { return TIFFOptions.TIFF_LZW; } else if ("packbits".equalsIgnoreCase(option)) { return TIFFOptions.TIFF_PACKBITS; } else if ("nocompression".equalsIgnoreCase(option)) { return TIFFOptions.TIFF_NO_COMPRESSION; } else { throw new RuntimeException ("Invalid TIFF Compression: " + option); } } private static void printUsage() { System.out.println ("Usage:"); System.out.println ("\tjPDFImages [options...] file"); System.out.println (); System.out.println ("Where:"); System.out.println ("\tfile is the path to a PDF file to convert."); System.out.println (); System.out.println ("Options: "); System.out.println ("\t-output - Name of the output TIFF file or directory to write the output files to."); System.out.println ("\t-pwd - Password to open the PDF file."); System.out.println ("\t-format - Output image format."); System.out.println ("\t-dpi - The DPI to use when creating the output image, an integer value."); System.out.println ("\t-jpegqual - The quality setting to use when outputting an image in JPEG format."); System.out.println ("\t-tiffcomp - The TIFF compression to use in an output TIFF image."); System.out.println (); System.out.println ("\t-key - License key to run the product in production mode."); System.out.println (); } }