1 31 package org.pdfbox.examples.pdmodel; 32 33 import org.pdfbox.pdmodel.PDDocument; 34 import org.pdfbox.pdmodel.PDDocumentCatalog; 35 import org.pdfbox.pdmodel.PDDocumentInformation; 36 import org.pdfbox.pdmodel.common.PDMetadata; 37 import org.pdfbox.util.DateConverter; 38 39 import java.io.ByteArrayInputStream ; 40 import java.util.Calendar ; 41 import java.util.GregorianCalendar ; 42 43 51 public class AddMetadataFromDocInfo 52 { 53 private static final String PADDING = 54 " " + 55 " " + 56 " " + 57 " " + 58 " " + 59 " " + 60 " " + 61 " " + 62 " " + 63 " " + 64 " " + 65 " "; 66 67 68 69 private AddMetadataFromDocInfo() 70 { 71 } 73 74 81 public static void main( String [] args ) throws Exception 82 { 83 if( args.length != 2 ) 84 { 85 usage(); 86 } 87 else 88 { 89 PDDocument document = null; 90 91 try 92 { 93 document = PDDocument.load( args[0] ); 94 if( document.isEncrypted() ) 95 { 96 System.err.println( "Error: Cannot add metadata to encrypted document." ); 97 System.exit( 1 ); 98 } 99 PDDocumentCatalog catalog = document.getDocumentCatalog(); 100 PDDocumentInformation info = document.getDocumentInformation(); 101 102 StringBuffer xmp= new StringBuffer (); 105 xmp.append( 106 "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n" + 107 "<?adobe-xap-filters esc=\"CRLF\"?>\n" + 108 "<x:xmpmeta>\n" + 109 " <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" + 110 " <rdf:Description rdf:about='' xmlns:pdf='http://ns.adobe.com/pdf/1.3/' " + 111 "pdf:Keywords='" + fixNull( info.getKeywords() ) + "' " + 112 "pdf:Producer='" + fixNull( info.getProducer() ) + "'></rdf:Description>\n" + 113 " <rdf:Description rdf:about='' xmlns:xap='http://ns.adobe.com/xap/1.0/' " + 114 "xap:ModifyDate='" + fixNull( info.getModificationDate() ) + "' " + 115 "xap:CreateDate='" + fixNull( info.getCreationDate() ) + "' " + 116 "xap:CreatorTool='" + fixNull( info.getCreator() ) + "' " + 117 "xap:MetadataDate='" + fixNull( new GregorianCalendar () )+ "'>\n" + 118 " </rdf:Description>\n" + 119 " <rdf:Description rdf:about='' xmlns:dc='http://purl.org/dc/elements/1.1/' " + 120 "dc:format='application/pdf'>\n" + 121 " <dc:title>\n" + 122 " <rdf:Alt>\n" + 123 " <rdf:li xml:lang='x-default'>" + fixNull( info.getTitle() ) +"</rdf:li>\n" + 124 " </rdf:Alt>\n" + 125 " </dc:title>\n" + 126 " <dc:creator>\n" + 127 " <rdf:Seq>\n" + 128 " <rdf:li>PDFBox.org</rdf:li>\n" + 129 " </rdf:Seq>\n" + 130 " </dc:creator>\n" + 131 " <dc:description>\n" + 132 " <rdf:Alt>\n" + 133 " <rdf:li xml:lang='x-default'>" + fixNull( info.getSubject() ) +"</rdf:li>\n" + 134 " </rdf:Alt>\n" + 135 " </dc:description>\n" + 136 " </rdf:Description>\n" + 137 " </rdf:RDF>\n" + 138 "</x:xmpmeta>\n" ); 139 140 xmp.append( PADDING ); 143 xmp.append( PADDING ); 144 xmp.append( PADDING ); 145 xmp.append( "\n<?xpacket end='w'?>" ); 146 ByteArrayInputStream mdInput = new ByteArrayInputStream ( xmp.toString().getBytes() ); 147 PDMetadata metadataStream = new PDMetadata(document, mdInput, false ); 148 catalog.setMetadata( metadataStream ); 149 150 151 document.save( args[1] ); 152 } 153 finally 154 { 155 if( document != null ) 156 { 157 document.close(); 158 } 159 } 160 } 161 } 162 163 private static String fixNull( String string ) 164 { 165 return string == null ? "" : string; 166 } 167 168 private static String fixNull( Calendar cal ) 169 { 170 return cal == null ? "" : DateConverter.toISO8601( cal ); 171 } 172 173 176 private static void usage() 177 { 178 System.err.println( "Usage: java org.pdfbox.examples.pdmodel.AddMetadata <input-pdf> <output-pdf>" ); 179 } 180 } | Popular Tags |