KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > pdmodel > AddMetadataFromDocInfo


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

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 JavaDoc;
40 import java.util.Calendar JavaDoc;
41 import java.util.GregorianCalendar JavaDoc;
42
43 /**
44  * This is an example on how to add metadata to a document.
45  *
46  * Usage: java org.pdfbox.examples.pdmodel.AddMetadataToDocument <input-pdf> <output-pdf>
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.3 $
50  */

51 public class AddMetadataFromDocInfo
52 {
53     private static final String JavaDoc PADDING =
54         " " +
55         " " +
56         " " +
57         " " +
58         " " +
59         " " +
60         " " +
61         " " +
62         " " +
63         " " +
64         " " +
65         " ";
66         
67         
68     
69     private AddMetadataFromDocInfo()
70     {
71         //utility class
72
}
73     
74     /**
75      * This will print the documents data.
76      *
77      * @param args The command line arguments.
78      *
79      * @throws Exception If there is an error parsing the document.
80      */

81     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
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                 //Right now, PDFBox does not have any XMP library, so we will
103
//just consruct the XML by hand.
104
StringBuffer JavaDoc xmp= new StringBuffer JavaDoc();
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 JavaDoc() )+ "'>\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 spec says we should put padding, so that the metadata can be appended to
141
//in place
142
xmp.append( PADDING );
143                 xmp.append( PADDING );
144                 xmp.append( PADDING );
145                 xmp.append( "\n<?xpacket end='w'?>" );
146                 ByteArrayInputStream JavaDoc mdInput = new ByteArrayInputStream JavaDoc( 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 JavaDoc fixNull( String JavaDoc string )
164     {
165         return string == null ? "" : string;
166     }
167     
168     private static String JavaDoc fixNull( Calendar JavaDoc cal )
169     {
170         return cal == null ? "" : DateConverter.toISO8601( cal );
171     }
172
173     /**
174      * This will print the usage for this document.
175      */

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