1 31 package org.pdfbox; 32 33 import org.pdfbox.pdmodel.PDDocument; 34 35 41 public class PrintPDF 42 { 43 44 private static final String PASSWORD = "-password"; 45 private static final String SILENT = "-silentPrint"; 46 47 50 private PrintPDF() 51 { 52 } 54 55 62 public static void main( String [] args ) throws Exception 63 { 64 String password = ""; 65 String pdfFile = null; 66 boolean silentPrint = false; 67 for( int i=0; i<args.length; i++ ) 68 { 69 if( args[i].equals( PASSWORD ) ) 70 { 71 i++; 72 if( i >= args.length ) 73 { 74 usage(); 75 } 76 password = args[i]; 77 } 78 else if( args[i].equals( SILENT ) ) 79 { 80 silentPrint = true; 81 } 82 else 83 { 84 pdfFile = args[i]; 85 } 86 } 87 88 if( pdfFile == null ) 89 { 90 usage(); 91 } 92 93 PDDocument document = null; 94 try 95 { 96 document = PDDocument.load( pdfFile ); 97 98 if( document.isEncrypted() ) 99 { 100 document.decrypt( password ); 101 } 102 if( silentPrint ) 103 { 104 document.silentPrint(); 105 } 106 else 107 { 108 document.print(); 109 } 110 } 111 finally 112 { 113 if( document != null ) 114 { 115 document.close(); 116 } 117 } 118 } 119 120 123 private static void usage() 124 { 125 System.err.println( "Usage: java org.pdfbox.PrintPDF [OPTIONS] <PDF file>\n" + 126 " -password <password> Password to decrypt document\n" + 127 " -silentPrint Print without prompting for printer info\n" 128 ); 129 System.exit( 1 ); 130 } 131 } | Popular Tags |