001: /**
002: * Copyright (c) 2003-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.exceptions.InvalidPasswordException;
033:
034: import org.pdfbox.pdfparser.PDFParser;
035:
036: import org.pdfbox.pdmodel.PDDocument;
037: import org.pdfbox.pdmodel.PDDocumentCatalog;
038: import org.pdfbox.pdmodel.PDDocumentInformation;
039: import org.pdfbox.pdmodel.common.PDMetadata;
040:
041: import java.io.FileInputStream;
042: import java.io.IOException;
043:
044: import java.text.SimpleDateFormat;
045:
046: import java.util.Calendar;
047:
048: /**
049: * This is an example on how to get a documents metadata information.
050: *
051: * Usage: java org.pdfbox.examples.pdmodel.PrintDocumentMetaData <input-pdf>
052: *
053: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
054: * @version $Revision: 1.11 $
055: */
056: public class PrintDocumentMetaData {
057: /**
058: * This will print the documents data.
059: *
060: * @param args The command line arguments.
061: *
062: * @throws Exception If there is an error parsing the document.
063: */
064: public static void main(String[] args) throws Exception {
065: if (args.length != 1) {
066: usage();
067: } else {
068: PDDocument document = null;
069: FileInputStream file = null;
070: try {
071: file = new FileInputStream(args[0]);
072: PDFParser parser = new PDFParser(file);
073: parser.parse();
074: document = parser.getPDDocument();
075: if (document.isEncrypted()) {
076: try {
077: document.decrypt("");
078: } catch (InvalidPasswordException e) {
079: System.err
080: .println("Error: Document is encrypted with a password.");
081: System.exit(1);
082: }
083: }
084: PrintDocumentMetaData meta = new PrintDocumentMetaData();
085: meta.printMetadata(document);
086: } finally {
087: if (file != null) {
088: file.close();
089: }
090: if (document != null) {
091: document.close();
092: }
093: }
094: }
095: }
096:
097: /**
098: * This will print the usage for this document.
099: */
100: private static void usage() {
101: System.err
102: .println("Usage: java org.pdfbox.examples.pdmodel.PrintDocumentMetaData <input-pdf>");
103: }
104:
105: /**
106: * This will print the documents data to System.out.
107: *
108: * @param document The document to get the metadata from.
109: *
110: * @throws IOException If there is an error getting the page count.
111: */
112: public void printMetadata(PDDocument document) throws IOException {
113: PDDocumentInformation info = document.getDocumentInformation();
114: PDDocumentCatalog cat = document.getDocumentCatalog();
115: PDMetadata metadata = cat.getMetadata();
116: System.out.println("Page Count=" + document.getNumberOfPages());
117: System.out.println("Title=" + info.getTitle());
118: System.out.println("Author=" + info.getAuthor());
119: System.out.println("Subject=" + info.getSubject());
120: System.out.println("Keywords=" + info.getKeywords());
121: System.out.println("Creator=" + info.getCreator());
122: System.out.println("Producer=" + info.getProducer());
123: System.out.println("Creation Date="
124: + formatDate(info.getCreationDate()));
125: System.out.println("Modification Date="
126: + formatDate(info.getModificationDate()));
127: System.out.println("Trapped=" + info.getTrapped());
128: if (metadata != null) {
129: System.out.println("Metadata="
130: + metadata.getInputStreamAsString());
131: }
132: }
133:
134: /**
135: * This will format a date object.
136: *
137: * @param date The date to format.
138: *
139: * @return A string representation of the date.
140: */
141: private String formatDate(Calendar date) {
142: String retval = null;
143: if (date != null) {
144: SimpleDateFormat formatter = new SimpleDateFormat();
145: retval = formatter.format(date.getTime());
146: }
147:
148: return retval;
149: }
150: }
|