1 31 package org.pdfbox; 32 33 import java.io.IOException ; 34 35 import org.pdfbox.pdmodel.PDDocument; 36 37 import org.pdfbox.pdmodel.interactive.form.PDAcroForm; 38 39 import org.pdfbox.pdmodel.fdf.FDFDocument; 40 41 48 public class ExportFDF 49 { 50 53 public ExportFDF() 54 { 55 } 56 57 66 public static void main(String [] args) throws Exception 67 { 68 ExportFDF exporter = new ExportFDF(); 69 exporter.exportFDF( args ); 70 } 71 72 private void exportFDF( String [] args ) throws Exception 73 { 74 PDDocument pdf = null; 75 FDFDocument fdf = null; 76 77 try 78 { 79 if( args.length != 1 && args.length != 2 ) 80 { 81 usage(); 82 } 83 else 84 { 85 pdf = PDDocument.load( args[0] ); 86 PDAcroForm form = pdf.getDocumentCatalog().getAcroForm(); 87 if( form == null ) 88 { 89 System.err.println( "Error: This PDF does not contain a form." ); 90 } 91 else 92 { 93 String fdfName = null; 94 if( args.length == 2 ) 95 { 96 fdfName = args[1]; 97 } 98 else 99 { 100 if( args[0].length() > 4 ) 101 { 102 fdfName = args[0].substring( 0, args[0].length() -4 ) + ".fdf"; 103 } 104 } 105 fdf = form.exportFDF(); 106 fdf.save( fdfName ); 107 } 108 } 109 } 110 finally 111 { 112 close( fdf ); 113 close( pdf ); 114 } 115 } 116 117 120 private static void usage() 121 { 122 System.err.println( "usage: org.pdfbox.ExortFDF <pdf-file> [output-fdf-file]" ); 123 System.err.println( " [output-fdf-file] - Default is pdf name, test.pdf->test.fdf" ); 124 } 125 126 133 public void close( FDFDocument doc ) throws IOException 134 { 135 if( doc != null ) 136 { 137 doc.close(); 138 } 139 } 140 141 148 public void close( PDDocument doc ) throws IOException 149 { 150 if( doc != null ) 151 { 152 doc.close(); 153 } 154 } 155 } | Popular Tags |