1 31 package org.pdfbox.pdmodel; 32 33 import java.io.IOException ; 34 import java.util.ArrayList ; 35 import java.util.List ; 36 37 import org.pdfbox.cos.COSArray; 38 import org.pdfbox.cos.COSBase; 39 import org.pdfbox.cos.COSDictionary; 40 import org.pdfbox.cos.COSName; 41 import org.pdfbox.cos.COSStream; 42 import org.pdfbox.cos.COSString; 43 44 import org.pdfbox.pdmodel.common.COSArrayList; 45 import org.pdfbox.pdmodel.common.COSObjectable; 46 import org.pdfbox.pdmodel.common.PDDestinationOrAction; 47 import org.pdfbox.pdmodel.common.PDMetadata; 48 import org.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo; 49 import org.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot; 50 import org.pdfbox.pdmodel.interactive.action.type.PDActionURI; 51 import org.pdfbox.pdmodel.interactive.action.PDActionFactory; 52 import org.pdfbox.pdmodel.interactive.action.PDDocumentCatalogAdditionalActions; 53 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination; 54 import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline; 55 import org.pdfbox.pdmodel.interactive.form.PDAcroForm; 56 57 import org.pdfbox.pdmodel.interactive.pagenavigation.PDThread; 58 import org.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences; 59 60 66 public class PDDocumentCatalog implements COSObjectable 67 { 68 private COSDictionary root; 69 private PDDocument document; 70 71 private PDAcroForm acroForm = null; 72 73 77 public static final String PAGE_MODE_USE_NONE = "UseNone"; 78 81 public static final String PAGE_MODE_USE_OUTLINES = "UseOutlines"; 82 85 public static final String PAGE_MODE_USE_THUMBS = "UseThumbs"; 86 89 public static final String PAGE_MODE_FULL_SCREEN = "FullScreen"; 90 93 public static final String PAGE_MODE_USE_OPTIONAL_CONTENT = "UseOC"; 94 97 public static final String PAGE_MODE_USE_ATTACHMENTS = "UseAttachments"; 98 99 102 public static final String PAGE_LAYOUT_SINGLE_PAGE = "SinglePage"; 103 106 public static final String PAGE_LAYOUT_ONE_COLUMN = "OneColumn"; 107 110 public static final String PAGE_LAYOUT_TWO_COLUMN_LEFT = "TwoColumnLeft"; 111 114 public static final String PAGE_LAYOUT_TWO_COLUMN_RIGHT ="TwoColumnRight"; 115 119 public static final String PAGE_LAYOUT_TWO_PAGE_LEFT = "TwoPageLeft"; 120 124 public static final String PAGE_LAYOUT_TWO_PAGE_RIGHT = "TwoPageRight"; 125 126 127 128 133 public PDDocumentCatalog( PDDocument doc ) 134 { 135 document = doc; 136 root = new COSDictionary(); 137 root.setItem( COSName.TYPE, new COSString( "Catalog" ) ); 138 document.getDocument().getTrailer().setItem( COSName.ROOT, root ); 139 } 140 141 147 public PDDocumentCatalog( PDDocument doc, COSDictionary rootDictionary ) 148 { 149 document = doc; 150 root = rootDictionary; 151 } 152 153 158 public COSBase getCOSObject() 159 { 160 return root; 161 } 162 163 168 public COSDictionary getCOSDictionary() 169 { 170 return root; 171 } 172 173 179 public PDAcroForm getAcroForm() 180 { 181 if( acroForm == null ) 182 { 183 COSDictionary acroFormDic = 184 (COSDictionary)root.getDictionaryObject( COSName.ACRO_FORM ); 185 if( acroFormDic != null ) 186 { 187 acroForm = new PDAcroForm( document, acroFormDic ); 188 } 189 } 190 return acroForm; 191 } 192 193 198 public void setAcroForm( PDAcroForm acro ) 199 { 200 root.setItem( COSName.ACRO_FORM, acro ); 201 } 202 203 208 public PDPageNode getPages() 209 { 210 return new PDPageNode( (COSDictionary)root.getDictionaryObject( COSName.PAGES ) ); 211 } 212 213 220 public List getAllPages() 221 { 222 List retval = new ArrayList (); 223 PDPageNode rootNode = getPages(); 224 rootNode.getAllKids(retval); 227 return retval; 228 } 229 230 236 public PDViewerPreferences getViewerPreferences() 237 { 238 PDViewerPreferences retval = null; 239 COSDictionary dict = (COSDictionary)root.getDictionaryObject( "ViewerPreferences" ); 240 if( dict != null ) 241 { 242 retval = new PDViewerPreferences( dict ); 243 } 244 245 return retval; 246 } 247 248 253 public void setViewerPreferences( PDViewerPreferences prefs ) 254 { 255 root.setItem( "ViewerPreferences", prefs ); 256 } 257 258 264 public PDDocumentOutline getDocumentOutline() 265 { 266 PDDocumentOutline retval = null; 267 COSDictionary dict = (COSDictionary)root.getDictionaryObject( "Outlines" ); 268 if( dict != null ) 269 { 270 retval = new PDDocumentOutline( dict ); 271 } 272 273 return retval; 274 } 275 276 281 public void setDocumentOutline( PDDocumentOutline outlines ) 282 { 283 root.setItem( "Outlines", outlines ); 284 } 285 286 291 public List getThreads() 292 { 293 COSArray array = (COSArray)root.getDictionaryObject( "Threads" ); 294 if( array == null ) 295 { 296 array = new COSArray(); 297 root.setItem( "Threads", array ); 298 } 299 List pdObjects = new ArrayList (); 300 for( int i=0; i<array.size(); i++ ) 301 { 302 pdObjects.add( new PDThread( (COSDictionary)array.getObject( i ) ) ); 303 } 304 return new COSArrayList( pdObjects, array ); 305 } 306 307 312 public void setThreads( List threads ) 313 { 314 root.setItem( "Threads", COSArrayList.converterToCOSArray( threads ) ); 315 } 316 317 323 public PDMetadata getMetadata() 324 { 325 PDMetadata retval = null; 326 COSStream stream = (COSStream)root.getDictionaryObject( "Metadata" ); 327 if( stream != null ) 328 { 329 retval = new PDMetadata( stream ); 330 } 331 return retval; 332 } 333 334 339 public void setMetadata( PDMetadata meta ) 340 { 341 root.setItem( "Metadata", meta ); 342 } 343 344 349 public void setOpenAction( PDDestinationOrAction action ) 350 { 351 root.setItem( COSName.getPDFName("OpenAction"), action ); 352 } 353 354 362 public PDDestinationOrAction getOpenAction() throws IOException 363 { 364 PDDestinationOrAction action = null; 365 COSBase actionObj = root.getDictionaryObject("OpenAction"); 366 367 if( actionObj == null ) 368 { 369 } 371 else if( actionObj instanceof COSDictionary ) 372 { 373 action = PDActionFactory.createAction((COSDictionary)actionObj); 374 } 375 else if( actionObj instanceof COSArray ) 376 { 377 action = PDDestination.create( actionObj ); 378 } 379 else 380 { 381 throw new IOException ( "Unknown OpenAction " + actionObj ); 382 } 383 384 return action; 385 } 386 389 public PDDocumentCatalogAdditionalActions getActions() 390 { 391 COSDictionary addAct = (COSDictionary) root.getDictionaryObject("AA"); 392 if (addAct == null) 393 { 394 addAct = new COSDictionary(); 395 root.setItem("AA", addAct); 396 } 397 return new PDDocumentCatalogAdditionalActions(addAct); 398 } 399 400 405 public void setActions( PDDocumentCatalogAdditionalActions actions ) 406 { 407 root.setItem("AA", actions ); 408 } 409 410 413 public PDDocumentNameDictionary getNames() 414 { 415 PDDocumentNameDictionary nameDic = null; 416 COSDictionary names = (COSDictionary) root.getDictionaryObject("Names"); 417 if(names != null) 418 { 419 nameDic = new PDDocumentNameDictionary(this,names); 420 } 421 return nameDic; 422 } 423 424 429 public void setNames( PDDocumentNameDictionary names ) 430 { 431 root.setItem("Names", names ); 432 } 433 434 440 public PDMarkInfo getMarkInfo() 441 { 442 PDMarkInfo retval = null; 443 COSDictionary dic = (COSDictionary)root.getDictionaryObject( "MarkInfo" ); 444 if( dic != null ) 445 { 446 retval = new PDMarkInfo( dic ); 447 } 448 return retval; 449 } 450 451 456 public void setMarkInfo( PDMarkInfo markInfo ) 457 { 458 root.setItem( "MarkInfo", markInfo ); 459 } 460 461 465 public String getPageMode() 466 { 467 return root.getNameAsString( "PageMode", PAGE_MODE_USE_NONE ); 468 } 469 470 474 public void setPageMode( String mode ) 475 { 476 root.setName( "PageMode", mode ); 477 } 478 479 483 public String getPageLayout() 484 { 485 return root.getNameAsString( "PageLayout", PAGE_LAYOUT_SINGLE_PAGE ); 486 } 487 488 492 public void setPageLayout( String layout ) 493 { 494 root.setName( "PageLayout", layout ); 495 } 496 497 501 public PDActionURI getURI() 502 { 503 PDActionURI retval = null; 504 COSDictionary uri = (COSDictionary)root.getDictionaryObject( "URI" ); 505 if( uri != null ) 506 { 507 retval = new PDActionURI( uri ); 508 } 509 return retval; 510 } 511 512 516 public void setURI( PDActionURI uri ) 517 { 518 root.setItem( "URI", uri ); 519 } 520 521 526 public PDStructureTreeRoot getStructureTreeRoot() 527 { 528 PDStructureTreeRoot treeRoot = null; 529 COSDictionary dic = (COSDictionary)root.getDictionaryObject( "StructTreeRoot" ); 530 if( dic != null ) 531 { 532 treeRoot = new PDStructureTreeRoot( dic ); 533 } 534 return treeRoot; 535 } 536 537 542 public void setStructureTreeRoot( PDStructureTreeRoot treeRoot ) 543 { 544 root.setItem( "StructTreeRoot", treeRoot ); 545 } 546 547 552 public String getLanguage() 553 { 554 return root.getString( "Lang" ); 555 } 556 557 562 public void setLanguage( String language ) 563 { 564 root.setString( "Lang", language ); 565 } 566 } | Popular Tags |