KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003-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.exceptions.InvalidPasswordException;
34
35 import org.pdfbox.pdfparser.PDFParser;
36
37 import org.pdfbox.pdmodel.PDDocument;
38 import org.pdfbox.pdmodel.PDDocumentCatalog;
39 import org.pdfbox.pdmodel.PDDocumentInformation;
40 import org.pdfbox.pdmodel.common.PDMetadata;
41
42 import java.io.FileInputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 import java.text.SimpleDateFormat JavaDoc;
46
47 import java.util.Calendar JavaDoc;
48
49 /**
50  * This is an example on how to get a documents metadata information.
51  *
52  * Usage: java org.pdfbox.examples.pdmodel.PrintDocumentMetaData <input-pdf>
53  *
54  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
55  * @version $Revision: 1.11 $
56  */

57 public class PrintDocumentMetaData
58 {
59     /**
60      * This will print the documents data.
61      *
62      * @param args The command line arguments.
63      *
64      * @throws Exception If there is an error parsing the document.
65      */

66     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
67     {
68         if( args.length != 1 )
69         {
70             usage();
71         }
72         else
73         {
74             PDDocument document = null;
75             FileInputStream JavaDoc file = null;
76             try
77             {
78                 file = new FileInputStream JavaDoc( args[0] );
79                 PDFParser parser = new PDFParser( file );
80                 parser.parse();
81                 document = parser.getPDDocument();
82                 if( document.isEncrypted() )
83                 {
84                     try
85                     {
86                         document.decrypt( "" );
87                     }
88                     catch( InvalidPasswordException e )
89                     {
90                         System.err.println( "Error: Document is encrypted with a password." );
91                         System.exit( 1 );
92                     }
93                 }
94                 PrintDocumentMetaData meta = new PrintDocumentMetaData();
95                 meta.printMetadata( document );
96             }
97             finally
98             {
99                 if( file != null )
100                 {
101                     file.close();
102                 }
103                 if( document != null )
104                 {
105                     document.close();
106                 }
107             }
108         }
109     }
110
111     /**
112      * This will print the usage for this document.
113      */

114     private static void usage()
115     {
116         System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintDocumentMetaData <input-pdf>" );
117     }
118
119     /**
120      * This will print the documents data to System.out.
121      *
122      * @param document The document to get the metadata from.
123      *
124      * @throws IOException If there is an error getting the page count.
125      */

126     public void printMetadata( PDDocument document ) throws IOException JavaDoc
127     {
128         PDDocumentInformation info = document.getDocumentInformation();
129         PDDocumentCatalog cat = document.getDocumentCatalog();
130         PDMetadata metadata = cat.getMetadata();
131         System.out.println( "Page Count=" + document.getNumberOfPages() );
132         System.out.println( "Title=" + info.getTitle() );
133         System.out.println( "Author=" + info.getAuthor() );
134         System.out.println( "Subject=" + info.getSubject() );
135         System.out.println( "Keywords=" + info.getKeywords() );
136         System.out.println( "Creator=" + info.getCreator() );
137         System.out.println( "Producer=" + info.getProducer() );
138         System.out.println( "Creation Date=" + formatDate( info.getCreationDate() ) );
139         System.out.println( "Modification Date=" + formatDate( info.getModificationDate() ) );
140         System.out.println( "Trapped=" + info.getTrapped() );
141         if( metadata != null )
142         {
143             System.out.println( "Metadata=" + metadata.getInputStreamAsString() );
144         }
145     }
146
147     /**
148      * This will format a date object.
149      *
150      * @param date The date to format.
151      *
152      * @return A string representation of the date.
153      */

154     private String JavaDoc formatDate( Calendar JavaDoc date )
155     {
156         String JavaDoc retval = null;
157         if( date != null )
158         {
159             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc();
160             retval = formatter.format( date.getTime() );
161         }
162
163         return retval;
164     }
165 }
Popular Tags