1 31 package org.pdfbox.pdmodel.fdf; 32 33 import java.io.BufferedInputStream ; 34 import java.io.BufferedWriter ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.FileWriter ; 39 import java.io.InputStream ; 40 import java.io.IOException ; 41 import java.io.OutputStream ; 42 import java.io.Writer ; 43 44 import org.pdfbox.cos.COSDictionary; 45 import org.pdfbox.cos.COSDocument; 46 import org.pdfbox.cos.COSName; 47 48 import org.pdfbox.exceptions.COSVisitorException; 49 50 import org.pdfbox.pdfparser.PDFParser; 51 52 import org.pdfbox.pdfwriter.COSWriter; 53 54 import org.pdfbox.util.XMLUtil; 55 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.Element ; 58 59 66 public class FDFDocument 67 { 68 private COSDocument document; 69 70 75 public FDFDocument() throws IOException 76 { 77 document = new COSDocument(); 78 document.setHeaderString( "%FDF-1.2" ); 79 80 document.setTrailer( new COSDictionary() ); 82 83 FDFCatalog catalog = new FDFCatalog(); 85 setCatalog( catalog ); 86 } 87 88 94 public FDFDocument( COSDocument doc ) 95 { 96 document = doc; 97 } 98 99 105 public FDFDocument( Document doc ) throws IOException 106 { 107 this(); 108 Element xfdf = doc.getDocumentElement(); 109 if( !xfdf.getNodeName().equals( "xfdf" ) ) 110 { 111 throw new IOException ( "Error while importing xfdf document, " + 112 "root should be 'xfdf' and not '" + xfdf.getNodeName() + "'" ); 113 } 114 FDFCatalog cat = new FDFCatalog( xfdf ); 115 setCatalog( cat ); 116 } 117 118 125 public void writeXML( Writer output ) throws IOException 126 { 127 output.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); 128 output.write( "<xfdf xmlns=\"http://ns.adobe.com/xfdf/\" xml:space=\"preserve\">\n" ); 129 130 getCatalog().writeXML( output ); 131 132 output.write( "</xfdf>\n" ); 133 } 134 135 136 137 142 public COSDocument getDocument() 143 { 144 return document; 145 } 146 147 152 public FDFCatalog getCatalog() 153 { 154 FDFCatalog retval = null; 155 COSDictionary trailer = document.getTrailer(); 156 COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT ); 157 if( root == null ) 158 { 159 retval = new FDFCatalog(); 160 setCatalog( retval ); 161 } 162 else 163 { 164 retval = new FDFCatalog( root ); 165 } 166 return retval; 167 } 168 169 174 public void setCatalog( FDFCatalog cat ) 175 { 176 COSDictionary trailer = document.getTrailer(); 177 trailer.setItem( COSName.ROOT, cat ); 178 } 179 180 189 public static FDFDocument load( String filename ) throws IOException 190 { 191 return load( new BufferedInputStream ( new FileInputStream ( filename ) ) ); 192 } 193 194 203 public static FDFDocument load( File file ) throws IOException 204 { 205 return load( new BufferedInputStream ( new FileInputStream ( file ) ) ); 206 } 207 208 217 public static FDFDocument load( InputStream input ) throws IOException 218 { 219 PDFParser parser = new PDFParser( input ); 220 parser.parse(); 221 return parser.getFDFDocument(); 222 } 223 224 233 public static FDFDocument loadXFDF( String filename ) throws IOException 234 { 235 return loadXFDF( new BufferedInputStream ( new FileInputStream ( filename ) ) ); 236 } 237 238 247 public static FDFDocument loadXFDF( File file ) throws IOException 248 { 249 return loadXFDF( new BufferedInputStream ( new FileInputStream ( file ) ) ); 250 } 251 252 261 public static FDFDocument loadXFDF( InputStream input ) throws IOException 262 { 263 Document doc = XMLUtil.parse( input ); 264 return new FDFDocument( doc ); 265 } 266 267 275 public void save( File fileName ) throws IOException , COSVisitorException 276 { 277 save( new FileOutputStream ( fileName ) ); 278 } 279 280 288 public void save( String fileName ) throws IOException , COSVisitorException 289 { 290 save( new FileOutputStream ( fileName ) ); 291 } 292 293 301 public void save( OutputStream output ) throws IOException , COSVisitorException 302 { 303 COSWriter writer = null; 304 try 305 { 306 writer = new COSWriter( output ); 307 writer.write( document ); 308 writer.close(); 309 } 310 finally 311 { 312 if( writer != null ) 313 { 314 writer.close(); 315 } 316 } 317 } 318 319 327 public void saveXFDF( File fileName ) throws IOException , COSVisitorException 328 { 329 saveXFDF( new BufferedWriter ( new FileWriter ( fileName ) ) ); 330 } 331 332 340 public void saveXFDF( String fileName ) throws IOException , COSVisitorException 341 { 342 saveXFDF( new BufferedWriter ( new FileWriter ( fileName ) ) ); 343 } 344 345 353 public void saveXFDF( Writer output ) throws IOException , COSVisitorException 354 { 355 try 356 { 357 writeXML( output ); 358 } 359 finally 360 { 361 if( output != null ) 362 { 363 output.close(); 364 } 365 } 366 } 367 368 373 public void close() throws IOException 374 { 375 document.close(); 376 } 377 } | Popular Tags |