1 31 package org.pdfbox.pdmodel.interactive.documentnavigation.outline; 32 33 import java.awt.Color ; 34 import java.io.IOException ; 35 import java.util.List ; 36 37 import org.pdfbox.cos.COSArray; 38 import org.pdfbox.cos.COSDictionary; 39 import org.pdfbox.cos.COSFloat; 40 import org.pdfbox.exceptions.OutlineNotLocalException; 41 import org.pdfbox.pdmodel.PDDestinationNameTreeNode; 42 import org.pdfbox.pdmodel.PDDocument; 43 import org.pdfbox.pdmodel.PDDocumentNameDictionary; 44 import org.pdfbox.pdmodel.PDPage; 45 import org.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement; 46 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceInstance; 47 import org.pdfbox.pdmodel.graphics.color.PDDeviceRGB; 48 import org.pdfbox.pdmodel.interactive.action.type.PDAction; 49 import org.pdfbox.pdmodel.interactive.action.type.PDActionGoTo; 50 import org.pdfbox.pdmodel.interactive.action.PDActionFactory; 51 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination; 52 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination; 53 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination; 54 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination; 55 import org.pdfbox.util.BitFlagHelper; 56 57 63 public class PDOutlineItem extends PDOutlineNode 64 { 65 66 private static final int ITALIC_FLAG = 1; 67 private static final int BOLD_FLAG = 2; 68 69 72 public PDOutlineItem() 73 { 74 super(); 75 } 76 77 82 public PDOutlineItem( COSDictionary dic ) 83 { 84 super( dic ); 85 } 86 87 92 public void insertSiblingAfter( PDOutlineItem item ) 93 { 94 item.setParent( getParent() ); 95 PDOutlineItem next = getNextSibling(); 96 setNextSibling( item ); 97 item.setPreviousSibling( this ); 98 if( next != null ) 99 { 100 item.setNextSibling( next ); 101 next.setPreviousSibling( item ); 102 } 103 updateParentOpenCount( 1 ); 104 } 105 106 111 public PDOutlineItem getPreviousSibling() 112 { 113 PDOutlineItem last = null; 114 COSDictionary lastDic = (COSDictionary)node.getDictionaryObject( "Prev" ); 115 if( lastDic != null ) 116 { 117 last = new PDOutlineItem( lastDic ); 118 } 119 return last; 120 } 121 122 127 protected void setPreviousSibling( PDOutlineNode outlineNode ) 128 { 129 node.setItem( "Prev", outlineNode ); 130 } 131 132 137 public PDOutlineItem getNextSibling() 138 { 139 PDOutlineItem last = null; 140 COSDictionary lastDic = (COSDictionary)node.getDictionaryObject( "Next" ); 141 if( lastDic != null ) 142 { 143 last = new PDOutlineItem( lastDic ); 144 } 145 return last; 146 } 147 148 153 protected void setNextSibling( PDOutlineNode outlineNode ) 154 { 155 node.setItem( "Next", outlineNode ); 156 } 157 158 163 public String getTitle() 164 { 165 return node.getString( "Title" ); 166 } 167 168 173 public void setTitle( String title ) 174 { 175 node.setString( "Title", title ); 176 } 177 178 184 public PDDestination getDestination() throws IOException 185 { 186 return PDDestination.create( node.getDictionaryObject( "Dest" ) ); 187 } 188 189 194 public void setDestination( PDDestination dest ) 195 { 196 node.setItem( "Dest", dest ); 197 } 198 199 204 public void setDestination( PDPage page ) 205 { 206 PDPageXYZDestination dest = null; 207 if( page != null ) 208 { 209 dest = new PDPageXYZDestination(); 210 dest.setPage( page ); 211 } 212 setDestination( dest ); 213 } 214 215 225 public PDPage findDestinationPage( PDDocument doc ) throws IOException 226 { 227 PDPage page = null; 228 PDDestination rawDest = getDestination(); 229 if( rawDest == null ) 230 { 231 PDAction outlineAction = getAction(); 232 if( outlineAction instanceof PDActionGoTo ) 233 { 234 rawDest = ((PDActionGoTo)outlineAction).getDestination(); 235 } 236 else if( outlineAction == null ) 237 { 238 } 241 else 242 { 243 throw new OutlineNotLocalException( "Error: Outline does not reference a local page." ); 244 } 245 } 246 247 PDPageDestination pageDest = null; 248 if( rawDest instanceof PDNamedDestination ) 249 { 250 PDNamedDestination namedDest = (PDNamedDestination)rawDest; 252 PDDocumentNameDictionary namesDict = doc.getDocumentCatalog().getNames(); 253 if( namesDict != null ) 254 { 255 PDDestinationNameTreeNode destsTree = namesDict.getDests(); 256 if( destsTree != null ) 257 { 258 pageDest = (PDPageDestination)destsTree.getValue( namedDest.getNamedDestination() ); 259 } 260 } 261 } 262 else if( rawDest instanceof PDPageDestination) 263 { 264 pageDest = (PDPageDestination) rawDest; 265 } 266 else if( rawDest == null ) 267 { 268 } 270 else 271 { 272 throw new IOException ( "Error: Unknown destination type " + rawDest ); 273 } 274 275 if( pageDest != null ) 276 { 277 page = pageDest.getPage(); 278 if( page == null ) 279 { 280 int pageNumber = pageDest.getPageNumber(); 281 if( pageNumber != -1 ) 282 { 283 List allPages = doc.getDocumentCatalog().getAllPages(); 284 page = (PDPage)allPages.get( pageNumber ); 285 } 286 } 287 } 288 289 return page; 290 } 291 292 297 public PDAction getAction() 298 { 299 return PDActionFactory.createAction( (COSDictionary)node.getDictionaryObject( "A" ) ); 300 } 301 302 307 public void setAction( PDAction action ) 308 { 309 node.setItem( "A", action ); 310 } 311 312 317 public PDStructureElement getStructureElement() 318 { 319 PDStructureElement se = null; 320 COSDictionary dic = (COSDictionary)node.getDictionaryObject( "SE" ); 321 if( dic != null ) 322 { 323 se = new PDStructureElement( dic ); 324 } 325 return se; 326 } 327 328 333 public void setStructuredElement( PDStructureElement structureElement ) 334 { 335 node.setItem( "SE", structureElement ); 336 } 337 338 344 public PDColorSpaceInstance getTextColor() 345 { 346 PDColorSpaceInstance retval = null; 347 COSArray csValues = (COSArray)node.getDictionaryObject( "C" ); 348 if( csValues == null ) 349 { 350 csValues = new COSArray(); 351 csValues.growToSize( 3, new COSFloat( 0 ) ); 352 node.setItem( "C", csValues ); 353 } 354 retval = new PDColorSpaceInstance(csValues); 355 retval.setColorSpace( PDDeviceRGB.INSTANCE ); 356 return retval; 357 } 358 359 364 public void setTextColor( PDColorSpaceInstance textColor ) 365 { 366 node.setItem( "C", textColor.getCOSColorSpaceValue() ); 367 } 368 369 374 public void setTextColor( Color textColor ) 375 { 376 COSArray array = new COSArray(); 377 array.add( new COSFloat( textColor.getRed()/255f)); 378 array.add( new COSFloat( textColor.getGreen()/255f)); 379 array.add( new COSFloat( textColor.getBlue()/255f)); 380 node.setItem( "C", array ); 381 } 382 383 388 public boolean isItalic() 389 { 390 return BitFlagHelper.getFlag( node, "F", ITALIC_FLAG ); 391 } 392 393 398 public void setItalic( boolean italic ) 399 { 400 BitFlagHelper.setFlag( node, "F", ITALIC_FLAG, italic ); 401 } 402 403 408 public boolean isBold() 409 { 410 return BitFlagHelper.getFlag( node, "F", BOLD_FLAG ); 411 } 412 413 418 public void setBold( boolean bold ) 419 { 420 BitFlagHelper.setFlag( node, "F", BOLD_FLAG, bold ); 421 } 422 423 } 424 | Popular Tags |