Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.fdf » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.examples.fdf;
   18   
   19   import java.io.IOException;
   20   
   21   import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
   22   import org.apache.pdfbox.pdmodel.interactive.form.PDField;
   23   
   24   import org.apache.pdfbox.pdmodel.PDDocument;
   25   import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
   26   
   27   import org.apache.pdfbox.exceptions.COSVisitorException;
   28   
   29   import org.apache.pdfbox.examples.AbstractExample;
   30   
   31   /**
   32    * This example will take a PDF document and set a FDF field in it.
   33    *
   34    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   35    * @version $Revision: 1.7 $
   36    */
   37   public class SetField extends AbstractExample
   38   {
   39   
   40       /**
   41        * This will set a single field in the document.
   42        *
   43        * @param pdfDocument The PDF to set the field in.
   44        * @param name The name of the field to set.
   45        * @param value The new value of the field.
   46        *
   47        * @throws IOException If there is an error setting the field.
   48        */
   49       public void setField( PDDocument pdfDocument, String name, String value ) throws IOException
   50       {
   51           PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
   52           PDAcroForm acroForm = docCatalog.getAcroForm();
   53           PDField field = acroForm.getField( name );
   54           if( field != null )
   55           {
   56               field.setValue( value );
   57           }
   58           else
   59           {
   60               System.err.println( "No field found with name:" + name );
   61           }
   62   
   63       }
   64   
   65       /**
   66        * This will read a PDF file and set a field and then write it the pdf out again.
   67        * <br />
   68        * see usage() for commandline
   69        *
   70        * @param args command line arguments
   71        *
   72        * @throws IOException If there is an error importing the FDF document.
   73        * @throws COSVisitorException If there is an error writing the PDF.
   74        */
   75       public static void main(String[] args) throws IOException, COSVisitorException
   76       {
   77           SetField setter = new SetField();
   78           setter.setField( args );
   79       }
   80   
   81       private void setField( String[] args ) throws IOException, COSVisitorException
   82       {
   83           PDDocument pdf = null;
   84           try
   85           {
   86               if( args.length != 3 )
   87               {
   88                   usage();
   89               }
   90               else
   91               {
   92                   SetField example = new SetField();
   93   
   94                   pdf = PDDocument.load( args[0] );
   95                   example.setField( pdf, args[1], args[2] );
   96                   pdf.save( args[0] );
   97               }
   98           }
   99           finally
  100           {
  101               if( pdf != null )
  102               {
  103                   pdf.close();
  104               }
  105           }
  106       }
  107       /**
  108        * This will print out a message telling how to use this example.
  109        */
  110       private static void usage()
  111       {
  112           System.err.println( "usage: org.apache.pdfbox.examples.fdf.SetField <pdf-file> <field-name> <field-value>" );
  113       }
  114   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.fdf » [javadoc | source]