KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > PDDocumentInformation


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.pdmodel;
32
33 import java.io.IOException JavaDoc;
34
35 import java.util.Calendar JavaDoc;
36
37 import org.pdfbox.cos.COSBase;
38 import org.pdfbox.cos.COSDictionary;
39 import org.pdfbox.cos.COSName;
40
41 import org.pdfbox.pdmodel.common.COSObjectable;
42
43 /**
44  * This is the document metadata. Each getXXX method will return the entry if
45  * it exists or null if it does not exist. If you pass in null for the setXXX
46  * method then it will clear the value.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.12 $
50  */

51 public class PDDocumentInformation implements COSObjectable
52 {
53     private static final COSName TITLE = COSName.getPDFName( "Title" );
54     private static final COSName AUTHOR = COSName.getPDFName( "Author" );
55     private static final COSName SUBJECT = COSName.getPDFName( "Subject" );
56     private static final COSName KEYWORDS = COSName.getPDFName( "Keywords" );
57     private static final COSName CREATOR = COSName.getPDFName( "Creator" );
58     private static final COSName PRODUCER = COSName.getPDFName( "Producer" );
59     private static final COSName CREATION_DATE = COSName.getPDFName( "CreationDate" );
60     private static final COSName MODIFICATION_DATE = COSName.getPDFName( "ModDate" );
61     private static final COSName TRAPPED = COSName.getPDFName( "Trapped" );
62     private COSDictionary info;
63
64     
65     /**
66      * Default Constructor.
67      */

68     public PDDocumentInformation()
69     {
70         info = new COSDictionary();
71     }
72     
73     /**
74      * Constructor that is used for a preexisting dictionary.
75      *
76      * @param dic The underlying dictionary.
77      */

78     public PDDocumentInformation( COSDictionary dic )
79     {
80         info = dic;
81     }
82
83     /**
84      * This will get the underlying dictionary that this object wraps.
85      *
86      * @return The underlying info dictionary.
87      */

88     public COSDictionary getDictionary()
89     {
90         return info;
91     }
92     
93     /**
94      * Convert this standard java object to a COS object.
95      *
96      * @return The cos object that matches this Java object.
97      */

98     public COSBase getCOSObject()
99     {
100         return info;
101     }
102
103     /**
104      * This will get the title of the document. This will return null if no title exists.
105      *
106      * @return The title of the document.
107      */

108     public String JavaDoc getTitle()
109     {
110         return info.getString( TITLE );
111     }
112
113     /**
114      * This will set the title of the document.
115      *
116      * @param title The new title for the document.
117      */

118     public void setTitle( String JavaDoc title )
119     {
120         info.setString( TITLE, title );
121     }
122
123     /**
124      * This will get the author of the document. This will return null if no author exists.
125      *
126      * @return The author of the document.
127      */

128     public String JavaDoc getAuthor()
129     {
130         return info.getString( AUTHOR );
131     }
132
133     /**
134      * This will set the author of the document.
135      *
136      * @param author The new author for the document.
137      */

138     public void setAuthor( String JavaDoc author )
139     {
140         info.setString( AUTHOR, author );
141     }
142
143     /**
144      * This will get the subject of the document. This will return null if no subject exists.
145      *
146      * @return The subject of the document.
147      */

148     public String JavaDoc getSubject()
149     {
150         return info.getString( SUBJECT );
151     }
152
153     /**
154      * This will set the subject of the document.
155      *
156      * @param subject The new subject for the document.
157      */

158     public void setSubject( String JavaDoc subject )
159     {
160         info.setString( SUBJECT, subject );
161     }
162
163     /**
164      * This will get the keywords of the document. This will return null if no keywords exists.
165      *
166      * @return The keywords of the document.
167      */

168     public String JavaDoc getKeywords()
169     {
170         return info.getString( KEYWORDS );
171     }
172
173     /**
174      * This will set the keywords of the document.
175      *
176      * @param keywords The new keywords for the document.
177      */

178     public void setKeywords( String JavaDoc keywords )
179     {
180         info.setString( KEYWORDS, keywords );
181     }
182
183     /**
184      * This will get the creator of the document. This will return null if no creator exists.
185      *
186      * @return The creator of the document.
187      */

188     public String JavaDoc getCreator()
189     {
190         return info.getString( CREATOR );
191     }
192
193     /**
194      * This will set the creator of the document.
195      *
196      * @param creator The new creator for the document.
197      */

198     public void setCreator( String JavaDoc creator )
199     {
200         info.setString( CREATOR, creator );
201     }
202
203     /**
204      * This will get the producer of the document. This will return null if no producer exists.
205      *
206      * @return The producer of the document.
207      */

208     public String JavaDoc getProducer()
209     {
210         return info.getString( PRODUCER );
211     }
212
213     /**
214      * This will set the producer of the document.
215      *
216      * @param producer The new producer for the document.
217      */

218     public void setProducer( String JavaDoc producer )
219     {
220         info.setString( PRODUCER, producer );
221     }
222
223     /**
224      * This will get the creation date of the document. This will return null if no creation date exists.
225      *
226      * @return The creation date of the document.
227      *
228      * @throws IOException If there is an error creating the date.
229      */

230     public Calendar JavaDoc getCreationDate() throws IOException JavaDoc
231     {
232         return info.getDate( CREATION_DATE );
233     }
234
235     /**
236      * This will set the creation date of the document.
237      *
238      * @param date The new creation date for the document.
239      */

240     public void setCreationDate( Calendar JavaDoc date )
241     {
242         info.setDate( CREATION_DATE, date );
243     }
244
245     /**
246      * This will get the modification date of the document. This will return null if no modification date exists.
247      *
248      * @return The modification date of the document.
249      *
250      * @throws IOException If there is an error creating the date.
251      */

252     public Calendar JavaDoc getModificationDate() throws IOException JavaDoc
253     {
254         return info.getDate( MODIFICATION_DATE );
255     }
256
257     /**
258      * This will set the modification date of the document.
259      *
260      * @param date The new modification date for the document.
261      */

262     public void setModificationDate( Calendar JavaDoc date )
263     {
264         info.setDate( MODIFICATION_DATE, date );
265     }
266
267     /**
268      * This will get the trapped value for the document.
269      * This will return null if one is not found.
270      *
271      * @return The trapped value for the document.
272      */

273     public String JavaDoc getTrapped()
274     {
275         return info.getNameAsString( TRAPPED );
276     }
277     
278     /**
279      * This will get the value of a custom metadata information field for the document.
280      * This will return null if one is not found.
281      *
282      * @param fieldName Name of custom metadata field from pdf document.
283      *
284      * @return String Value of metadata field
285      *
286      * @author Gerardo Ortiz
287      */

288     public String JavaDoc getCustomMetadataValue(String JavaDoc fieldName)
289     {
290         return info.getString( fieldName );
291     }
292     
293     /**
294      * Set the custom metadata value.
295      *
296      * @param fieldName The name of the custom metadata field.
297      * @param fieldValue The value to the custom metadata field.
298      */

299     public void setCustomMetadataValue( String JavaDoc fieldName, String JavaDoc fieldValue )
300     {
301         info.setString( fieldName, fieldValue );
302     }
303
304     /**
305      * This will set the trapped of the document. This will be
306      * 'True', 'False', or 'Unknown'.
307      *
308      * @param value The new trapped value for the document.
309      */

310     public void setTrapped( String JavaDoc value )
311     {
312         if( value != null &&
313             !value.equals( "True" ) &&
314             !value.equals( "False" ) &&
315             !value.equals( "Unknown" ) )
316         {
317             throw new RuntimeException JavaDoc( "Valid values for trapped are " +
318                                         "'True', 'False', or 'Unknown'" );
319         }
320
321         info.setName( TRAPPED, value );
322     }
323 }
Popular Tags