1 31 package org.pdfbox.examples.pdmodel; 32 33 import org.pdfbox.exceptions.InvalidPasswordException; 34 35 import org.pdfbox.pdfparser.PDFParser; 36 37 import org.pdfbox.pdmodel.PDDocument; 38 import org.pdfbox.pdmodel.PDDocumentCatalog; 39 import org.pdfbox.pdmodel.PDDocumentInformation; 40 import org.pdfbox.pdmodel.common.PDMetadata; 41 42 import java.io.FileInputStream ; 43 import java.io.IOException ; 44 45 import java.text.SimpleDateFormat ; 46 47 import java.util.Calendar ; 48 49 57 public class PrintDocumentMetaData 58 { 59 66 public static void main( String [] args ) throws Exception 67 { 68 if( args.length != 1 ) 69 { 70 usage(); 71 } 72 else 73 { 74 PDDocument document = null; 75 FileInputStream file = null; 76 try 77 { 78 file = new FileInputStream ( args[0] ); 79 PDFParser parser = new PDFParser( file ); 80 parser.parse(); 81 document = parser.getPDDocument(); 82 if( document.isEncrypted() ) 83 { 84 try 85 { 86 document.decrypt( "" ); 87 } 88 catch( InvalidPasswordException e ) 89 { 90 System.err.println( "Error: Document is encrypted with a password." ); 91 System.exit( 1 ); 92 } 93 } 94 PrintDocumentMetaData meta = new PrintDocumentMetaData(); 95 meta.printMetadata( document ); 96 } 97 finally 98 { 99 if( file != null ) 100 { 101 file.close(); 102 } 103 if( document != null ) 104 { 105 document.close(); 106 } 107 } 108 } 109 } 110 111 114 private static void usage() 115 { 116 System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintDocumentMetaData <input-pdf>" ); 117 } 118 119 126 public void printMetadata( PDDocument document ) throws IOException 127 { 128 PDDocumentInformation info = document.getDocumentInformation(); 129 PDDocumentCatalog cat = document.getDocumentCatalog(); 130 PDMetadata metadata = cat.getMetadata(); 131 System.out.println( "Page Count=" + document.getNumberOfPages() ); 132 System.out.println( "Title=" + info.getTitle() ); 133 System.out.println( "Author=" + info.getAuthor() ); 134 System.out.println( "Subject=" + info.getSubject() ); 135 System.out.println( "Keywords=" + info.getKeywords() ); 136 System.out.println( "Creator=" + info.getCreator() ); 137 System.out.println( "Producer=" + info.getProducer() ); 138 System.out.println( "Creation Date=" + formatDate( info.getCreationDate() ) ); 139 System.out.println( "Modification Date=" + formatDate( info.getModificationDate() ) ); 140 System.out.println( "Trapped=" + info.getTrapped() ); 141 if( metadata != null ) 142 { 143 System.out.println( "Metadata=" + metadata.getInputStreamAsString() ); 144 } 145 } 146 147 154 private String formatDate( Calendar date ) 155 { 156 String retval = null; 157 if( date != null ) 158 { 159 SimpleDateFormat formatter = new SimpleDateFormat (); 160 retval = formatter.format( date.getTime() ); 161 } 162 163 return retval; 164 } 165 } | Popular Tags |