KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > ExportFDF


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

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

53     public ExportFDF()
54     {
55     }
56
57     /**
58      * This will import an fdf document and write out another pdf.
59      * <br />
60      * see usage() for commandline
61      *
62      * @param args command line arguments
63      *
64      * @throws Exception If there is an error importing the FDF document.
65      */

66     public static void main(String JavaDoc[] args) throws Exception JavaDoc
67     {
68         ExportFDF exporter = new ExportFDF();
69         exporter.exportFDF( args );
70     }
71
72     private void exportFDF( String JavaDoc[] args ) throws Exception JavaDoc
73     {
74         PDDocument pdf = null;
75         FDFDocument fdf = null;
76
77         try
78         {
79             if( args.length != 1 && args.length != 2 )
80             {
81                 usage();
82             }
83             else
84             {
85                 pdf = PDDocument.load( args[0] );
86                 PDAcroForm form = pdf.getDocumentCatalog().getAcroForm();
87                 if( form == null )
88                 {
89                     System.err.println( "Error: This PDF does not contain a form." );
90                 }
91                 else
92                 {
93                     String JavaDoc fdfName = null;
94                     if( args.length == 2 )
95                     {
96                         fdfName = args[1];
97                     }
98                     else
99                     {
100                         if( args[0].length() > 4 )
101                         {
102                             fdfName = args[0].substring( 0, args[0].length() -4 ) + ".fdf";
103                         }
104                     }
105                     fdf = form.exportFDF();
106                     fdf.save( fdfName );
107                 }
108             }
109         }
110         finally
111         {
112             close( fdf );
113             close( pdf );
114         }
115     }
116
117     /**
118      * This will print out a message telling how to use this example.
119      */

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

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

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