001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.pdmodel;
031:
032: import org.pdfbox.pdmodel.PDDocument;
033: import org.pdfbox.pdmodel.PDDocumentCatalog;
034: import org.pdfbox.pdmodel.PDDocumentInformation;
035: import org.pdfbox.pdmodel.common.PDMetadata;
036: import org.pdfbox.util.DateConverter;
037:
038: import java.io.ByteArrayInputStream;
039: import java.util.Calendar;
040: import java.util.GregorianCalendar;
041:
042: /**
043: * This is an example on how to add metadata to a document.
044: *
045: * Usage: java org.pdfbox.examples.pdmodel.AddMetadataToDocument <input-pdf> <output-pdf>
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.3 $
049: */
050: public class AddMetadataFromDocInfo {
051: private static final String PADDING = " "
052: + " "
053: + " "
054: + " "
055: + " "
056: + " "
057: + " "
058: + " "
059: + " "
060: + " "
061: + " "
062: + " ";
063:
064: private AddMetadataFromDocInfo() {
065: //utility class
066: }
067:
068: /**
069: * This will print the documents data.
070: *
071: * @param args The command line arguments.
072: *
073: * @throws Exception If there is an error parsing the document.
074: */
075: public static void main(String[] args) throws Exception {
076: if (args.length != 2) {
077: usage();
078: } else {
079: PDDocument document = null;
080:
081: try {
082: document = PDDocument.load(args[0]);
083: if (document.isEncrypted()) {
084: System.err
085: .println("Error: Cannot add metadata to encrypted document.");
086: System.exit(1);
087: }
088: PDDocumentCatalog catalog = document
089: .getDocumentCatalog();
090: PDDocumentInformation info = document
091: .getDocumentInformation();
092:
093: //Right now, PDFBox does not have any XMP library, so we will
094: //just consruct the XML by hand.
095: StringBuffer xmp = new StringBuffer();
096: xmp
097: .append("<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n"
098: + "<?adobe-xap-filters esc=\"CRLF\"?>\n"
099: + "<x:xmpmeta>\n"
100: + " <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n"
101: + " <rdf:Description rdf:about='' xmlns:pdf='http://ns.adobe.com/pdf/1.3/' "
102: + "pdf:Keywords='"
103: + fixNull(info.getKeywords())
104: + "' "
105: + "pdf:Producer='"
106: + fixNull(info.getProducer())
107: + "'></rdf:Description>\n"
108: + " <rdf:Description rdf:about='' xmlns:xap='http://ns.adobe.com/xap/1.0/' "
109: + "xap:ModifyDate='"
110: + fixNull(info.getModificationDate())
111: + "' "
112: + "xap:CreateDate='"
113: + fixNull(info.getCreationDate())
114: + "' "
115: + "xap:CreatorTool='"
116: + fixNull(info.getCreator())
117: + "' "
118: + "xap:MetadataDate='"
119: + fixNull(new GregorianCalendar())
120: + "'>\n"
121: + " </rdf:Description>\n"
122: + " <rdf:Description rdf:about='' xmlns:dc='http://purl.org/dc/elements/1.1/' "
123: + "dc:format='application/pdf'>\n"
124: + " <dc:title>\n"
125: + " <rdf:Alt>\n"
126: + " <rdf:li xml:lang='x-default'>"
127: + fixNull(info.getTitle())
128: + "</rdf:li>\n"
129: + " </rdf:Alt>\n"
130: + " </dc:title>\n"
131: + " <dc:creator>\n"
132: + " <rdf:Seq>\n"
133: + " <rdf:li>PDFBox.org</rdf:li>\n"
134: + " </rdf:Seq>\n"
135: + " </dc:creator>\n"
136: + " <dc:description>\n"
137: + " <rdf:Alt>\n"
138: + " <rdf:li xml:lang='x-default'>"
139: + fixNull(info.getSubject())
140: + "</rdf:li>\n"
141: + " </rdf:Alt>\n"
142: + " </dc:description>\n"
143: + " </rdf:Description>\n"
144: + " </rdf:RDF>\n" + "</x:xmpmeta>\n");
145:
146: //xmp spec says we should put padding, so that the metadata can be appended to
147: //in place
148: xmp.append(PADDING);
149: xmp.append(PADDING);
150: xmp.append(PADDING);
151: xmp.append("\n<?xpacket end='w'?>");
152: ByteArrayInputStream mdInput = new ByteArrayInputStream(
153: xmp.toString().getBytes());
154: PDMetadata metadataStream = new PDMetadata(document,
155: mdInput, false);
156: catalog.setMetadata(metadataStream);
157:
158: document.save(args[1]);
159: } finally {
160: if (document != null) {
161: document.close();
162: }
163: }
164: }
165: }
166:
167: private static String fixNull(String string) {
168: return string == null ? "" : string;
169: }
170:
171: private static String fixNull(Calendar cal) {
172: return cal == null ? "" : DateConverter.toISO8601(cal);
173: }
174:
175: /**
176: * This will print the usage for this document.
177: */
178: private static void usage() {
179: System.err
180: .println("Usage: java org.pdfbox.examples.pdmodel.AddMetadata <input-pdf> <output-pdf>");
181: }
182: }
|