1 31 package org.pdfbox.examples.fdf; 32 33 import java.io.IOException ; 34 35 import org.pdfbox.pdmodel.interactive.form.PDAcroForm; 36 import org.pdfbox.pdmodel.interactive.form.PDField; 37 38 import org.pdfbox.pdmodel.PDDocument; 39 import org.pdfbox.pdmodel.PDDocumentCatalog; 40 41 import org.pdfbox.exceptions.COSVisitorException; 42 43 import org.pdfbox.examples.AbstractExample; 44 45 51 public class SetField extends AbstractExample 52 { 53 54 63 public void setField( PDDocument pdfDocument, String name, String value ) throws IOException 64 { 65 PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); 66 PDAcroForm acroForm = docCatalog.getAcroForm(); 67 PDField field = acroForm.getField( name ); 68 if( field != null ) 69 { 70 field.setValue( value ); 71 } 72 else 73 { 74 System.err.println( "No field found with name:" + name ); 75 } 76 77 } 78 79 89 public static void main(String [] args) throws IOException , COSVisitorException 90 { 91 SetField setter = new SetField(); 92 setter.setField( args ); 93 } 94 95 private void setField( String [] args ) throws IOException , COSVisitorException 96 { 97 PDDocument pdf = null; 98 try 99 { 100 if( args.length != 3 ) 101 { 102 usage(); 103 } 104 else 105 { 106 SetField example = new SetField(); 107 108 pdf = PDDocument.load( args[0] ); 109 example.setField( pdf, args[1], args[2] ); 110 pdf.save( args[0] ); 111 } 112 } 113 finally 114 { 115 if( pdf != null ) 116 { 117 pdf.close(); 118 } 119 } 120 } 121 124 private static void usage() 125 { 126 System.err.println( "usage: org.pdfbox.examples.fdf.SetField <pdf-file> <field-name> <field-value>" ); 127 } 128 } | Popular Tags |