KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > ImportFDF


1 /**
2  * Copyright (c) 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;
32
33 import java.io.IOException JavaDoc;
34
35 import org.pdfbox.pdmodel.PDDocument;
36 import org.pdfbox.pdmodel.PDDocumentCatalog;
37
38 import org.pdfbox.pdmodel.fdf.FDFDocument;
39
40 import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
41
42 /**
43  * This example will take a PDF document and fill the fields with data from the
44  * FDF fields.
45  *
46  * @author <a HREF="ben@benlitchfield.com">Ben Litchfield</a>
47  * @version $Revision: 1.2 $
48  */

49 public class ImportFDF
50 {
51     /**
52      * Creates a new instance of ImportFDF.
53      */

54     public ImportFDF()
55     {
56     }
57
58     /**
59      * This will takes the values from the fdf document and import them into the
60      * PDF document.
61      *
62      * @param pdfDocument The document to put the fdf data into.
63      * @param fdfDocument The FDF document to get the data from.
64      *
65      * @throws IOException If there is an error setting the data in the field.
66      */

67     public void importFDF( PDDocument pdfDocument, FDFDocument fdfDocument ) throws IOException JavaDoc
68     {
69         PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
70         PDAcroForm acroForm = docCatalog.getAcroForm();
71         acroForm.setCacheFields( true );
72         acroForm.importFDF( fdfDocument );
73     }
74
75     /**
76      * This will import an fdf document and write out another pdf.
77      * <br />
78      * see usage() for commandline
79      *
80      * @param args command line arguments
81      *
82      * @throws Exception If there is an error importing the FDF document.
83      */

84     public static void main(String JavaDoc[] args) throws Exception JavaDoc
85     {
86         ImportFDF importer = new ImportFDF();
87         importer.importFDF( args );
88     }
89
90     private void importFDF( String JavaDoc[] args ) throws Exception JavaDoc
91     {
92         PDDocument pdf = null;
93         FDFDocument fdf = null;
94
95         try
96         {
97             if( args.length != 3 )
98             {
99                 usage();
100             }
101             else
102             {
103                 ImportFDF importer = new ImportFDF();
104
105                 pdf = PDDocument.load( args[0] );
106                 fdf = FDFDocument.load( args[1] );
107                 importer.importFDF( pdf, fdf );
108                 
109                 pdf.save( args[2] );
110             }
111         }
112         finally
113         {
114             close( fdf );
115             close( pdf );
116         }
117     }
118
119     /**
120      * This will print out a message telling how to use this example.
121      */

122     private static void usage()
123     {
124         System.err.println( "usage: org.pdfbox.ImportFDF <pdf-file> <fdf-file> <output-file>" );
125     }
126
127     /**
128      * Close the document.
129      *
130      * @param doc The doc to close.
131      *
132      * @throws IOException If there is an error closing the document.
133      */

134     public void close( FDFDocument doc ) throws IOException JavaDoc
135     {
136         if( doc != null )
137         {
138             doc.close();
139         }
140     }
141
142     /**
143      * Close the document.
144      *
145      * @param doc The doc to close.
146      *
147      * @throws IOException If there is an error closing the document.
148      */

149     public void close( PDDocument doc ) throws IOException JavaDoc
150     {
151         if( doc != null )
152         {
153             doc.close();
154         }
155     }
156 }
Popular Tags