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.pdmodel;
031:
032: import java.io.IOException;
033:
034: import java.util.Calendar;
035:
036: import org.pdfbox.cos.COSBase;
037: import org.pdfbox.cos.COSDictionary;
038: import org.pdfbox.cos.COSName;
039:
040: import org.pdfbox.pdmodel.common.COSObjectable;
041:
042: /**
043: * This is the document metadata. Each getXXX method will return the entry if
044: * it exists or null if it does not exist. If you pass in null for the setXXX
045: * method then it will clear the value.
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.12 $
049: */
050: public class PDDocumentInformation implements COSObjectable {
051: private static final COSName TITLE = COSName.getPDFName("Title");
052: private static final COSName AUTHOR = COSName.getPDFName("Author");
053: private static final COSName SUBJECT = COSName
054: .getPDFName("Subject");
055: private static final COSName KEYWORDS = COSName
056: .getPDFName("Keywords");
057: private static final COSName CREATOR = COSName
058: .getPDFName("Creator");
059: private static final COSName PRODUCER = COSName
060: .getPDFName("Producer");
061: private static final COSName CREATION_DATE = COSName
062: .getPDFName("CreationDate");
063: private static final COSName MODIFICATION_DATE = COSName
064: .getPDFName("ModDate");
065: private static final COSName TRAPPED = COSName
066: .getPDFName("Trapped");
067: private COSDictionary info;
068:
069: /**
070: * Default Constructor.
071: */
072: public PDDocumentInformation() {
073: info = new COSDictionary();
074: }
075:
076: /**
077: * Constructor that is used for a preexisting dictionary.
078: *
079: * @param dic The underlying dictionary.
080: */
081: public PDDocumentInformation(COSDictionary dic) {
082: info = dic;
083: }
084:
085: /**
086: * This will get the underlying dictionary that this object wraps.
087: *
088: * @return The underlying info dictionary.
089: */
090: public COSDictionary getDictionary() {
091: return info;
092: }
093:
094: /**
095: * Convert this standard java object to a COS object.
096: *
097: * @return The cos object that matches this Java object.
098: */
099: public COSBase getCOSObject() {
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 getTitle() {
109: return info.getString(TITLE);
110: }
111:
112: /**
113: * This will set the title of the document.
114: *
115: * @param title The new title for the document.
116: */
117: public void setTitle(String title) {
118: info.setString(TITLE, title);
119: }
120:
121: /**
122: * This will get the author of the document. This will return null if no author exists.
123: *
124: * @return The author of the document.
125: */
126: public String getAuthor() {
127: return info.getString(AUTHOR);
128: }
129:
130: /**
131: * This will set the author of the document.
132: *
133: * @param author The new author for the document.
134: */
135: public void setAuthor(String author) {
136: info.setString(AUTHOR, author);
137: }
138:
139: /**
140: * This will get the subject of the document. This will return null if no subject exists.
141: *
142: * @return The subject of the document.
143: */
144: public String getSubject() {
145: return info.getString(SUBJECT);
146: }
147:
148: /**
149: * This will set the subject of the document.
150: *
151: * @param subject The new subject for the document.
152: */
153: public void setSubject(String subject) {
154: info.setString(SUBJECT, subject);
155: }
156:
157: /**
158: * This will get the keywords of the document. This will return null if no keywords exists.
159: *
160: * @return The keywords of the document.
161: */
162: public String getKeywords() {
163: return info.getString(KEYWORDS);
164: }
165:
166: /**
167: * This will set the keywords of the document.
168: *
169: * @param keywords The new keywords for the document.
170: */
171: public void setKeywords(String keywords) {
172: info.setString(KEYWORDS, keywords);
173: }
174:
175: /**
176: * This will get the creator of the document. This will return null if no creator exists.
177: *
178: * @return The creator of the document.
179: */
180: public String getCreator() {
181: return info.getString(CREATOR);
182: }
183:
184: /**
185: * This will set the creator of the document.
186: *
187: * @param creator The new creator for the document.
188: */
189: public void setCreator(String creator) {
190: info.setString(CREATOR, creator);
191: }
192:
193: /**
194: * This will get the producer of the document. This will return null if no producer exists.
195: *
196: * @return The producer of the document.
197: */
198: public String getProducer() {
199: return info.getString(PRODUCER);
200: }
201:
202: /**
203: * This will set the producer of the document.
204: *
205: * @param producer The new producer for the document.
206: */
207: public void setProducer(String producer) {
208: info.setString(PRODUCER, producer);
209: }
210:
211: /**
212: * This will get the creation date of the document. This will return null if no creation date exists.
213: *
214: * @return The creation date of the document.
215: *
216: * @throws IOException If there is an error creating the date.
217: */
218: public Calendar getCreationDate() throws IOException {
219: return info.getDate(CREATION_DATE);
220: }
221:
222: /**
223: * This will set the creation date of the document.
224: *
225: * @param date The new creation date for the document.
226: */
227: public void setCreationDate(Calendar date) {
228: info.setDate(CREATION_DATE, date);
229: }
230:
231: /**
232: * This will get the modification date of the document. This will return null if no modification date exists.
233: *
234: * @return The modification date of the document.
235: *
236: * @throws IOException If there is an error creating the date.
237: */
238: public Calendar getModificationDate() throws IOException {
239: return info.getDate(MODIFICATION_DATE);
240: }
241:
242: /**
243: * This will set the modification date of the document.
244: *
245: * @param date The new modification date for the document.
246: */
247: public void setModificationDate(Calendar date) {
248: info.setDate(MODIFICATION_DATE, date);
249: }
250:
251: /**
252: * This will get the trapped value for the document.
253: * This will return null if one is not found.
254: *
255: * @return The trapped value for the document.
256: */
257: public String getTrapped() {
258: return info.getNameAsString(TRAPPED);
259: }
260:
261: /**
262: * This will get the value of a custom metadata information field for the document.
263: * This will return null if one is not found.
264: *
265: * @param fieldName Name of custom metadata field from pdf document.
266: *
267: * @return String Value of metadata field
268: *
269: * @author Gerardo Ortiz
270: */
271: public String getCustomMetadataValue(String fieldName) {
272: return info.getString(fieldName);
273: }
274:
275: /**
276: * Set the custom metadata value.
277: *
278: * @param fieldName The name of the custom metadata field.
279: * @param fieldValue The value to the custom metadata field.
280: */
281: public void setCustomMetadataValue(String fieldName,
282: String fieldValue) {
283: info.setString(fieldName, fieldValue);
284: }
285:
286: /**
287: * This will set the trapped of the document. This will be
288: * 'True', 'False', or 'Unknown'.
289: *
290: * @param value The new trapped value for the document.
291: */
292: public void setTrapped(String value) {
293: if (value != null && !value.equals("True")
294: && !value.equals("False") && !value.equals("Unknown")) {
295: throw new RuntimeException("Valid values for trapped are "
296: + "'True', 'False', or 'Unknown'");
297: }
298:
299: info.setName(TRAPPED, value);
300: }
301: }
|