KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > fdf > SetField


1 /**
2  * Copyright (c) 2003-2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.examples.fdf;
32
33 import java.io.IOException JavaDoc;
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 /**
46  * This example will take a PDF document and set a FDF field in it.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.7 $
50  */

51 public class SetField extends AbstractExample
52 {
53
54     /**
55      * This will set a single field in the document.
56      *
57      * @param pdfDocument The PDF to set the field in.
58      * @param name The name of the field to set.
59      * @param value The new value of the field.
60      *
61      * @throws IOException If there is an error setting the field.
62      */

63     public void setField( PDDocument pdfDocument, String JavaDoc name, String JavaDoc value ) throws IOException JavaDoc
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     /**
80      * This will read a PDF file and set a field and then write it the pdf out again.
81      * <br />
82      * see usage() for commandline
83      *
84      * @param args command line arguments
85      *
86      * @throws IOException If there is an error importing the FDF document.
87      * @throws COSVisitorException If there is an error writing the PDF.
88      */

89     public static void main(String JavaDoc[] args) throws IOException JavaDoc, COSVisitorException
90     {
91         SetField setter = new SetField();
92         setter.setField( args );
93     }
94
95     private void setField( String JavaDoc[] args ) throws IOException JavaDoc, 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     /**
122      * This will print out a message telling how to use this example.
123      */

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