1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel; 18 19 import java.io.IOException; 20 21 import java.util.Calendar; 22 23 import org.apache.pdfbox.cos.COSBase; 24 import org.apache.pdfbox.cos.COSDictionary; 25 import org.apache.pdfbox.cos.COSName; 26 27 import org.apache.pdfbox.pdmodel.common.COSObjectable; 28 29 /** 30 * This is the document metadata. Each getXXX method will return the entry if 31 * it exists or null if it does not exist. If you pass in null for the setXXX 32 * method then it will clear the value. 33 * 34 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 35 * @version $Revision: 1.12 $ 36 */ 37 public class PDDocumentInformation implements COSObjectable 38 { 39 private static final COSName TITLE = COSName.getPDFName( "Title" ); 40 private static final COSName AUTHOR = COSName.getPDFName( "Author" ); 41 private static final COSName SUBJECT = COSName.getPDFName( "Subject" ); 42 private static final COSName KEYWORDS = COSName.getPDFName( "Keywords" ); 43 private static final COSName CREATOR = COSName.getPDFName( "Creator" ); 44 private static final COSName PRODUCER = COSName.getPDFName( "Producer" ); 45 private static final COSName CREATION_DATE = COSName.getPDFName( "CreationDate" ); 46 private static final COSName MODIFICATION_DATE = COSName.getPDFName( "ModDate" ); 47 private static final COSName TRAPPED = COSName.getPDFName( "Trapped" ); 48 private COSDictionary info; 49 50 51 /** 52 * Default Constructor. 53 */ 54 public PDDocumentInformation() 55 { 56 info = new COSDictionary(); 57 } 58 59 /** 60 * Constructor that is used for a preexisting dictionary. 61 * 62 * @param dic The underlying dictionary. 63 */ 64 public PDDocumentInformation( COSDictionary dic ) 65 { 66 info = dic; 67 } 68 69 /** 70 * This will get the underlying dictionary that this object wraps. 71 * 72 * @return The underlying info dictionary. 73 */ 74 public COSDictionary getDictionary() 75 { 76 return info; 77 } 78 79 /** 80 * Convert this standard java object to a COS object. 81 * 82 * @return The cos object that matches this Java object. 83 */ 84 public COSBase getCOSObject() 85 { 86 return info; 87 } 88 89 /** 90 * This will get the title of the document. This will return null if no title exists. 91 * 92 * @return The title of the document. 93 */ 94 public String getTitle() 95 { 96 return info.getString( TITLE ); 97 } 98 99 /** 100 * This will set the title of the document. 101 * 102 * @param title The new title for the document. 103 */ 104 public void setTitle( String title ) 105 { 106 info.setString( TITLE, title ); 107 } 108 109 /** 110 * This will get the author of the document. This will return null if no author exists. 111 * 112 * @return The author of the document. 113 */ 114 public String getAuthor() 115 { 116 return info.getString( AUTHOR ); 117 } 118 119 /** 120 * This will set the author of the document. 121 * 122 * @param author The new author for the document. 123 */ 124 public void setAuthor( String author ) 125 { 126 info.setString( AUTHOR, author ); 127 } 128 129 /** 130 * This will get the subject of the document. This will return null if no subject exists. 131 * 132 * @return The subject of the document. 133 */ 134 public String getSubject() 135 { 136 return info.getString( SUBJECT ); 137 } 138 139 /** 140 * This will set the subject of the document. 141 * 142 * @param subject The new subject for the document. 143 */ 144 public void setSubject( String subject ) 145 { 146 info.setString( SUBJECT, subject ); 147 } 148 149 /** 150 * This will get the keywords of the document. This will return null if no keywords exists. 151 * 152 * @return The keywords of the document. 153 */ 154 public String getKeywords() 155 { 156 return info.getString( KEYWORDS ); 157 } 158 159 /** 160 * This will set the keywords of the document. 161 * 162 * @param keywords The new keywords for the document. 163 */ 164 public void setKeywords( String keywords ) 165 { 166 info.setString( KEYWORDS, keywords ); 167 } 168 169 /** 170 * This will get the creator of the document. This will return null if no creator exists. 171 * 172 * @return The creator of the document. 173 */ 174 public String getCreator() 175 { 176 return info.getString( CREATOR ); 177 } 178 179 /** 180 * This will set the creator of the document. 181 * 182 * @param creator The new creator for the document. 183 */ 184 public void setCreator( String creator ) 185 { 186 info.setString( CREATOR, creator ); 187 } 188 189 /** 190 * This will get the producer of the document. This will return null if no producer exists. 191 * 192 * @return The producer of the document. 193 */ 194 public String getProducer() 195 { 196 return info.getString( PRODUCER ); 197 } 198 199 /** 200 * This will set the producer of the document. 201 * 202 * @param producer The new producer for the document. 203 */ 204 public void setProducer( String producer ) 205 { 206 info.setString( PRODUCER, producer ); 207 } 208 209 /** 210 * This will get the creation date of the document. This will return null if no creation date exists. 211 * 212 * @return The creation date of the document. 213 * 214 * @throws IOException If there is an error creating the date. 215 */ 216 public Calendar getCreationDate() throws IOException 217 { 218 return info.getDate( CREATION_DATE ); 219 } 220 221 /** 222 * This will set the creation date of the document. 223 * 224 * @param date The new creation date for the document. 225 */ 226 public void setCreationDate( Calendar date ) 227 { 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 { 240 return info.getDate( MODIFICATION_DATE ); 241 } 242 243 /** 244 * This will set the modification date of the document. 245 * 246 * @param date The new modification date for the document. 247 */ 248 public void setModificationDate( Calendar date ) 249 { 250 info.setDate( MODIFICATION_DATE, date ); 251 } 252 253 /** 254 * This will get the trapped value for the document. 255 * This will return null if one is not found. 256 * 257 * @return The trapped value for the document. 258 */ 259 public String getTrapped() 260 { 261 return info.getNameAsString( TRAPPED ); 262 } 263 264 /** 265 * This will get the value of a custom metadata information field for the document. 266 * This will return null if one is not found. 267 * 268 * @param fieldName Name of custom metadata field from pdf document. 269 * 270 * @return String Value of metadata field 271 * 272 * @author Gerardo Ortiz 273 */ 274 public String getCustomMetadataValue(String fieldName) 275 { 276 return info.getString( fieldName ); 277 } 278 279 /** 280 * Set the custom metadata value. 281 * 282 * @param fieldName The name of the custom metadata field. 283 * @param fieldValue The value to the custom metadata field. 284 */ 285 public void setCustomMetadataValue( String fieldName, String fieldValue ) 286 { 287 info.setString( fieldName, fieldValue ); 288 } 289 290 /** 291 * This will set the trapped of the document. This will be 292 * 'True', 'False', or 'Unknown'. 293 * 294 * @param value The new trapped value for the document. 295 */ 296 public void setTrapped( String value ) 297 { 298 if( value != null && 299 !value.equals( "True" ) && 300 !value.equals( "False" ) && 301 !value.equals( "Unknown" ) ) 302 { 303 throw new RuntimeException( "Valid values for trapped are " + 304 "'True', 'False', or 'Unknown'" ); 305 } 306 307 info.setName( TRAPPED, value ); 308 } 309 }