{
usage();
}
else
{
PDDocument document = null;
try
{
document = PDDocument.load( args[0] );
if( document.isEncrypted() )
{
System.err.println( "Error: Cannot add metadata to encrypted document." );
System.exit( 1 );
}
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDDocumentInformation info = document.getDocumentInformation();
//Right now, PDFBox does not have any XMP library, so we will
//just consruct the XML by hand.
StringBuffer xmp= new StringBuffer();
xmp.append(
"<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n" +
"<?adobe-xap-filters esc=\"CRLF\"?>\n" +
"<x:xmpmeta>\n" +
" <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" +
" <rdf:Description rdf:about='' xmlns:pdf='http://ns.adobe.com/pdf/1.3/' " +
"pdf:Keywords='" + fixNull( info.getKeywords() ) + "' " +
"pdf:Producer='" + fixNull( info.getProducer() ) + "'></rdf:Description>\n" +
" <rdf:Description rdf:about='' xmlns:xap='http://ns.adobe.com/xap/1.0/' " +
"xap:ModifyDate='" + fixNull( info.getModificationDate() ) + "' " +
"xap:CreateDate='" + fixNull( info.getCreationDate() ) + "' " +
"xap:CreatorTool='" + fixNull( info.getCreator() ) + "' " +
"xap:MetadataDate='" + fixNull( new GregorianCalendar() )+ "'>\n" +
" </rdf:Description>\n" +
" <rdf:Description rdf:about='' xmlns:dc='http://purl.org/dc/elements/1.1/' " +
"dc:format='application/pdf'>\n" +
" <dc:title>\n" +
" <rdf:Alt>\n" +
" <rdf:li xml:lang='x-default'>" + fixNull( info.getTitle() ) +"</rdf:li>\n" +
" </rdf:Alt>\n" +
" </dc:title>\n" +
" <dc:creator>\n" +
" <rdf:Seq>\n" +
" <rdf:li>PDFBox.org</rdf:li>\n" +
" </rdf:Seq>\n" +
" </dc:creator>\n" +
" <dc:description>\n" +
" <rdf:Alt>\n" +
" <rdf:li xml:lang='x-default'>" + fixNull( info.getSubject() ) +"</rdf:li>\n" +
" </rdf:Alt>\n" +
" </dc:description>\n" +
" </rdf:Description>\n" +
" </rdf:RDF>\n" +
"</x:xmpmeta>\n" );
//xmp spec says we should put padding, so that the metadata can be appended to
//in place
xmp.append( PADDING );
xmp.append( PADDING );
xmp.append( PADDING );
xmp.append( "\n<?xpacket end='w'?>" );
ByteArrayInputStream mdInput = new ByteArrayInputStream( xmp.toString().getBytes() );
PDMetadata metadataStream = new PDMetadata(document, mdInput, false );
catalog.setMetadata( metadataStream );
document.save( args[1] );
}
finally
{
if( document != null )
{
document.close();
}
}
}
}