1 31 package org.pdfbox.pdmodel.fdf; 32 33 import java.io.IOException ; 34 import java.io.Writer ; 35 import java.util.ArrayList ; 36 import java.util.List ; 37 38 import org.pdfbox.cos.COSArray; 39 import org.pdfbox.cos.COSBase; 40 import org.pdfbox.cos.COSDictionary; 41 import org.pdfbox.cos.COSStream; 42 import org.pdfbox.cos.COSString; 43 44 import org.pdfbox.pdmodel.common.COSObjectable; 45 import org.pdfbox.pdmodel.common.COSArrayList; 46 import org.pdfbox.pdmodel.common.filespecification.PDFileSpecification; 47 import org.pdfbox.pdmodel.common.filespecification.PDSimpleFileSpecification; 48 import org.w3c.dom.Element ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.NodeList ; 51 52 58 public class FDFDictionary implements COSObjectable 59 { 60 private COSDictionary fdf; 61 62 65 public FDFDictionary() 66 { 67 fdf = new COSDictionary(); 68 } 69 70 75 public FDFDictionary( COSDictionary fdfDictionary ) 76 { 77 fdf = fdfDictionary; 78 } 79 80 86 public FDFDictionary( Element fdfXML ) throws IOException 87 { 88 this(); 89 NodeList nodeList = fdfXML.getChildNodes(); 90 for( int i=0; i<nodeList.getLength(); i++ ) 91 { 92 Node node = nodeList.item( i ); 93 if( node instanceof Element ) 94 { 95 Element child = (Element )node; 96 if( child.getTagName().equals( "f" ) ) 97 { 98 PDSimpleFileSpecification fs = new PDSimpleFileSpecification(); 99 fs.setFile( child.getAttribute( "href" ) ); 100 101 } 102 else if( child.getTagName().equals( "ids" ) ) 103 { 104 COSArray ids = new COSArray(); 105 String original = child.getAttribute( "original" ); 106 String modified = child.getAttribute( "modified" ); 107 ids.add( COSString.createFromHexString( original ) ); 108 ids.add( COSString.createFromHexString( modified ) ); 109 setID( ids ); 110 } 111 else if( child.getTagName().equals( "fields" ) ) 112 { 113 NodeList fields = child.getElementsByTagName( "field" ); 114 List fieldList = new ArrayList (); 115 for( int f=0; f<fields.getLength(); f++ ) 116 { 117 fieldList.add( new FDFField( (Element )fields.item( f ) ) ); 118 } 119 setFields( fieldList ); 120 } 121 } 122 } 123 } 124 125 132 public void writeXML( Writer output ) throws IOException 133 { 134 PDFileSpecification fs = this.getFile(); 135 if( fs != null ) 136 { 137 output.write( "<f HREF=\"" + fs.getFile() + "\" />\n" ); 138 } 139 COSArray ids = this.getID(); 140 if( ids != null ) 141 { 142 COSString original = (COSString)ids.getObject( 0 ); 143 COSString modified = (COSString)ids.getObject( 1 ); 144 output.write( "<ids original=\"" + original.getHexString() + "\" " ); 145 output.write( "modified=\"" + modified.getHexString() + "\" />\n"); 146 } 147 List fields = getFields(); 148 if( fields != null && fields.size() > 0 ) 149 { 150 output.write( "<fields>\n" ); 151 for( int i=0; i<fields.size(); i++ ) 152 { 153 ((FDFField)fields.get( i )).writeXML( output ); 154 } 155 output.write( "</fields>\n" ); 156 } 157 } 158 159 164 public COSBase getCOSObject() 165 { 166 return fdf; 167 } 168 169 174 public COSDictionary getCOSDictionary() 175 { 176 return fdf; 177 } 178 179 187 public PDFileSpecification getFile() throws IOException 188 { 189 return PDFileSpecification.createFS( fdf.getDictionaryObject( "F" ) ); 190 } 191 192 197 public void setFile( PDFileSpecification fs ) 198 { 199 fdf.setItem( "F", fs ); 200 } 201 202 207 public COSArray getID() 208 { 209 return (COSArray)fdf.getDictionaryObject( "ID" ); 210 } 211 212 217 public void setID( COSArray id ) 218 { 219 fdf.setItem( "ID", id ); 220 } 221 222 228 public List getFields() 229 { 230 List retval = null; 231 COSArray fieldArray = (COSArray)fdf.getDictionaryObject( "Fields" ); 232 if( fieldArray != null ) 233 { 234 List fields = new ArrayList (); 235 for( int i=0; i<fieldArray.size(); i++ ) 236 { 237 fields.add( new FDFField( (COSDictionary)fieldArray.getObject( i ) ) ); 238 } 239 retval = new COSArrayList( fields, fieldArray ); 240 } 241 return retval; 242 } 243 244 249 public void setFields( List fields ) 250 { 251 fdf.setItem( "Fields", COSArrayList.converterToCOSArray( fields ) ); 252 } 253 254 260 public String getStatus() 261 { 262 return fdf.getString( "Status" ); 263 } 264 265 270 public void setStatus( String status ) 271 { 272 fdf.setString( "Status", status ); 273 } 274 275 280 public List getPages() 281 { 282 List retval = null; 283 COSArray pageArray = (COSArray)fdf.getDictionaryObject( "Pages" ); 284 if( pageArray != null ) 285 { 286 List pages = new ArrayList (); 287 for( int i=0; i<pageArray.size(); i++ ) 288 { 289 pages.add( new FDFPage( (COSDictionary)pageArray.get( i ) ) ); 290 } 291 retval = new COSArrayList( pages, pageArray ); 292 } 293 return retval; 294 } 295 296 302 public void setPages( List pages ) 303 { 304 fdf.setItem( "Pages", COSArrayList.converterToCOSArray( pages ) ); 305 } 306 307 313 public String getEncoding() 314 { 315 String encoding = fdf.getNameAsString( "Encoding" ); 316 if( encoding == null ) 317 { 318 encoding = "PDFDocEncoding"; 319 } 320 return encoding; 321 322 } 323 324 329 public void setEncoding( String encoding ) 330 { 331 fdf.setName( "Encoding", encoding ); 332 } 333 334 340 public List getAnnotations() 341 { 342 List retval = null; 343 COSArray annotArray = (COSArray)fdf.getDictionaryObject( "Annots" ); 344 if( annotArray != null ) 345 { 346 List annots = new ArrayList (); 347 for( int i=0; i<annotArray.size(); i++ ) 348 { 349 annots.add( new FDFAnnotation( (COSDictionary)annotArray.getObject( i ) ) ); 350 } 351 retval = new COSArrayList( annots, annotArray ); 352 } 353 return retval; 354 } 355 356 362 public void setAnnotations( List annots ) 363 { 364 fdf.setItem( "Annots", COSArrayList.converterToCOSArray( annots ) ); 365 } 366 367 372 public COSStream getDifferences() 373 { 374 return (COSStream)fdf.getDictionaryObject( "Differences" ); 375 } 376 377 382 public void setDifferences( COSStream diff ) 383 { 384 fdf.setItem( "Differences", diff ); 385 } 386 387 392 public String getTarget() 393 { 394 return fdf.getString( "Target" ); 395 } 396 397 402 public void setTarget( String target ) 403 { 404 fdf.setString( "Target", target ); 405 } 406 407 415 public List getEmbeddedFDFs() throws IOException 416 { 417 List retval = null; 418 COSArray embeddedArray = (COSArray)fdf.getDictionaryObject( "EmbeddedFDFs" ); 419 if( embeddedArray != null ) 420 { 421 List embedded = new ArrayList (); 422 for( int i=0; i<embeddedArray.size(); i++ ) 423 { 424 embedded.add( PDFileSpecification.createFS( embeddedArray.get( i ) ) ); 425 } 426 retval = new COSArrayList( embedded, embeddedArray ); 427 } 428 return retval; 429 } 430 431 438 public void setEmbeddedFDFs( List embedded ) 439 { 440 fdf.setItem( "EmbeddedFDFs", COSArrayList.converterToCOSArray( embedded ) ); 441 } 442 443 448 public FDFJavaScript getJavaScript() 449 { 450 FDFJavaScript fs = null; 451 COSDictionary dic = (COSDictionary)fdf.getDictionaryObject( "JavaScript" ); 452 if( dic != null ) 453 { 454 fs = new FDFJavaScript( dic ); 455 } 456 return fs; 457 } 458 459 464 public void setJavaScript( FDFJavaScript js ) 465 { 466 fdf.setItem( "JavaScript", js ); 467 } 468 469 } | Popular Tags |