KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > ImportXFDF


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

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

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

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

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

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

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

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