1 31 package org.pdfbox.cos; 32 33 import java.io.File ; 34 import java.io.IOException ; 35 36 import java.util.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Map ; 41 42 43 import org.pdfbox.exceptions.COSVisitorException; 44 import org.pdfbox.io.RandomAccess; 45 import org.pdfbox.io.RandomAccessFile; 46 47 import org.pdfbox.pdfparser.PDFObjectStreamParser; 48 import org.pdfbox.persistence.util.COSObjectKey; 49 50 57 public class COSDocument extends COSBase 58 { 59 private float version; 60 61 64 private List objects = new ArrayList (); 65 66 70 private Map objectPool = new HashMap (); 71 72 75 private COSDictionary trailer; 76 77 80 private RandomAccess scratchFile = null; 81 82 private File tmpFile = null; 83 84 private String headerString = "%PDF-1.4"; 85 86 92 public COSDocument() throws IOException 93 { 94 this( new File ( System.getProperty( "java.io.tmpdir" ) ) ); 95 } 96 97 105 public COSDocument( File scratchDir ) throws IOException 106 { 107 tmpFile = File.createTempFile( "pdfbox", "tmp", scratchDir ); 108 scratchFile = new RandomAccessFile( tmpFile, "rw" ); 109 } 110 111 119 public COSDocument( RandomAccess file ) 120 { 121 scratchFile = file; 122 } 123 124 129 public RandomAccess getScratchFile() 130 { 131 return scratchFile; 132 } 133 134 141 public COSObject getObjectByType( String type ) 142 { 143 return getObjectByType( COSName.getPDFName( type ) ); 144 } 145 146 153 public COSObject getObjectByType( COSName type ) 154 { 155 COSObject retval = null; 156 Iterator iter = objects.iterator(); 157 while( iter.hasNext() && retval == null) 158 { 159 COSObject object = (COSObject)iter.next(); 160 161 COSBase realObject = object.getObject(); 162 if( realObject instanceof COSDictionary ) 163 { 164 COSDictionary dic = (COSDictionary)realObject; 165 COSName objectType = (COSName)dic.getItem( COSName.TYPE ); 166 if( objectType != null && objectType.equals( type ) ) 167 { 168 retval = object; 169 } 170 } 171 } 172 return retval; 173 } 174 175 182 public List getObjectsByType( String type ) 183 { 184 return getObjectsByType( COSName.getPDFName( type ) ); 185 } 186 187 194 public List getObjectsByType( COSName type ) 195 { 196 List retval = new ArrayList (); 197 Iterator iter = objects.iterator(); 198 while( iter.hasNext() ) 199 { 200 COSObject object = (COSObject)iter.next(); 201 202 COSBase realObject = object.getObject(); 203 if( realObject instanceof COSDictionary ) 204 { 205 COSDictionary dic = (COSDictionary)realObject; 206 COSName objectType = (COSName)dic.getItem( COSName.TYPE ); 207 if( objectType != null && objectType.equals( type ) ) 208 { 209 retval.add( object ); 210 } 211 } 212 } 213 return retval; 214 } 215 216 219 public void print() 220 { 221 Iterator iter = objects.iterator(); 222 while( iter.hasNext() ) 223 { 224 COSObject object = (COSObject)iter.next(); 225 System.out.println( object); 226 } 227 } 228 229 234 public void setVersion( float versionValue ) 235 { 236 version = versionValue; 237 } 238 239 244 public float getVersion() 245 { 246 return version; 247 } 248 249 254 public boolean isEncrypted() 255 { 256 boolean encrypted = false; 257 if( trailer != null ) 258 { 259 encrypted = trailer.getDictionaryObject( "Encrypt" ) != null; 260 } 261 return encrypted; 262 } 263 264 270 public COSDictionary getEncryptionDictionary() 271 { 272 return (COSDictionary)trailer.getDictionaryObject( COSName.getPDFName( "Encrypt" ) ); 273 } 274 275 281 public void setEncryptionDictionary( COSDictionary encDictionary ) 282 { 283 trailer.setItem( COSName.getPDFName( "Encrypt" ), encDictionary ); 284 } 285 286 291 public COSArray getDocumentID() 292 { 293 return (COSArray) getTrailer().getItem(COSName.getPDFName("ID")); 294 } 295 296 301 public void setDocumentID( COSArray id ) 302 { 303 getTrailer().setItem(COSName.getPDFName("ID"), id); 304 } 305 306 317 328 329 338 public COSObject getCatalog() throws IOException 339 { 340 COSObject catalog = getObjectByType( COSName.CATALOG ); 341 if( catalog == null ) 342 { 343 throw new IOException ( "Catalog cannot be found" ); 344 } 345 return catalog; 346 } 347 348 353 public List getObjects() 354 { 355 return new ArrayList (objects); 356 } 357 358 363 public COSDictionary getTrailer() 364 { 365 return trailer; 366 } 367 368 374 public void setTrailer(COSDictionary newTrailer) 375 { 376 trailer = newTrailer; 377 } 378 379 386 public Object accept(ICOSVisitor visitor) throws COSVisitorException 387 { 388 return visitor.visitFromDocument( this ); 389 } 390 391 396 public void close() throws IOException 397 { 398 if( scratchFile != null ) 399 { 400 scratchFile.close(); 401 scratchFile = null; 402 } 403 if( tmpFile != null ) 404 { 405 tmpFile.delete(); 406 tmpFile = null; 407 } 408 } 409 410 414 protected void finalize() 415 { 416 if( tmpFile != null || scratchFile != null ) 417 { 418 Throwable t = new Throwable ( "Warning: You did not close the PDF Document" ); 419 t.printStackTrace(); 420 } 421 } 422 425 public String getHeaderString() 426 { 427 return headerString; 428 } 429 432 public void setHeaderString(String header) 433 { 434 headerString = header; 435 } 436 437 443 public void dereferenceObjectStreams() throws IOException 444 { 445 Iterator objStm = getObjectsByType( "ObjStm" ).iterator(); 446 while( objStm.hasNext() ) 447 { 448 COSObject objStream = (COSObject)objStm.next(); 449 COSStream stream = (COSStream)objStream.getObject(); 450 PDFObjectStreamParser parser = new PDFObjectStreamParser( stream, this ); 451 parser.parse(); 452 Iterator compressedObjects = parser.getObjects().iterator(); 453 while( compressedObjects.hasNext() ) 454 { 455 COSObject next = (COSObject)compressedObjects.next(); 456 COSObjectKey key = new COSObjectKey( next ); 457 COSObject obj = getObjectFromPool( key ); 458 obj.setObject( next.getObject() ); 459 } 460 } 461 } 462 463 473 public COSObject addObject(COSObject obj) throws IOException 474 { 475 COSObjectKey key = null; 476 if( obj.getObjectNumber() != null ) 477 { 478 key = new COSObjectKey( obj ); 479 } 480 COSObject fromPool = getObjectFromPool( key ); 481 fromPool.setObject( obj.getObject() ); 482 return fromPool; 483 } 484 485 494 public COSObject getObjectFromPool(COSObjectKey key) throws IOException 495 { 496 COSObject obj = null; 497 if( key != null ) 498 { 499 obj = (COSObject) objectPool.get(key); 500 } 501 if (obj == null) 502 { 503 obj = new COSObject(null); 505 if( key != null ) 506 { 507 obj.setObjectNumber( new COSInteger( key.getNumber() ) ); 508 obj.setGenerationNumber( new COSInteger( key.getGeneration() ) ); 509 objectPool.put(key, obj); 510 } 511 objects.add( obj ); 512 } 513 514 return obj; 515 } 516 } | Popular Tags |