1 31 package org.pdfbox; 32 33 import org.pdfbox.pdmodel.PDDocument; 34 import org.pdfbox.pdmodel.PDDocumentCatalog; 35 36 import org.pdfbox.pdmodel.fdf.FDFDocument; 37 38 import org.pdfbox.pdmodel.interactive.form.PDAcroForm; 39 40 import java.io.IOException ; 41 42 43 50 public class ImportXFDF 51 { 52 55 public ImportXFDF() 56 { 57 } 58 59 68 public void importFDF( PDDocument pdfDocument, FDFDocument fdfDocument ) throws IOException 69 { 70 PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); 71 PDAcroForm acroForm = docCatalog.getAcroForm(); 72 acroForm.setCacheFields( true ); 73 acroForm.importFDF( fdfDocument ); 74 } 75 76 85 public static void main(String [] args) throws Exception 86 { 87 ImportXFDF importer = new ImportXFDF(); 88 importer.importXFDF( args ); 89 } 90 91 private void importXFDF( String [] args ) throws Exception 92 { 93 PDDocument pdf = null; 94 FDFDocument fdf = null; 95 96 try 97 { 98 if( args.length != 3 ) 99 { 100 usage(); 101 } 102 else 103 { 104 ImportFDF importer = new ImportFDF(); 105 pdf = PDDocument.load( args[0] ); 106 fdf = FDFDocument.loadXFDF( args[1] ); 107 108 importer.importFDF( pdf, fdf ); 109 pdf.save( args[2] ); 110 fdf.save( "tmp/outputXFDFtoPDF.fdf"); 111 } 112 } 113 finally 114 { 115 close( fdf ); 116 close( pdf ); 117 } 118 } 119 120 123 private static void usage() 124 { 125 System.err.println( "usage: org.pdfbox.ImportXFDF <pdf-file> <fdf-file> <output-file>" ); 126 } 127 128 135 public void close( FDFDocument doc ) throws IOException 136 { 137 if( doc != null ) 138 { 139 doc.close(); 140 } 141 } 142 143 150 public void close( PDDocument doc ) throws IOException 151 { 152 if( doc != null ) 153 { 154 doc.close(); 155 } 156 } 157 } | Popular Tags |