KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003-2005, 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 java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
39 import org.pdfbox.pdmodel.interactive.form.PDField;
40
41 import org.pdfbox.exceptions.CryptographyException;
42 import org.pdfbox.exceptions.InvalidPasswordException;
43
44 import org.pdfbox.pdmodel.PDDocument;
45 import org.pdfbox.pdmodel.PDDocumentCatalog;
46
47 /**
48  * This example will take a PDF document and print all the fields from the file.
49  *
50  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
51  * @version $Revision: 1.16 $
52  */

53 public class PrintFields
54 {
55
56     /**
57      * This will print all the fields from the document.
58      *
59      * @param pdfDocument The PDF to get the fields from.
60      *
61      * @throws IOException If there is an error getting the fields.
62      */

63     public void printFields( PDDocument pdfDocument ) throws IOException JavaDoc
64     {
65         PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
66         PDAcroForm acroForm = docCatalog.getAcroForm();
67         List JavaDoc fields = acroForm.getFields();
68         Iterator JavaDoc fieldsIter = fields.iterator();
69         
70         System.out.println(new Integer JavaDoc(fields.size()).toString() + " top-level fields were found on the form");
71
72         while( fieldsIter.hasNext())
73         {
74             PDField field = (PDField)fieldsIter.next();
75                processField(field, "|--", field.getPartialName());
76         }
77     }
78     
79     private void processField(PDField field, String JavaDoc sLevel, String JavaDoc sParent) throws IOException JavaDoc
80     {
81         List JavaDoc kids = field.getKids();
82         if(kids != null)
83         {
84             Iterator JavaDoc kidsIter = kids.iterator();
85             if(!sParent.equals(field.getPartialName()))
86             {
87                sParent = sParent + "." + field.getPartialName();
88             }
89             System.out.println(sLevel + sParent);
90             //System.out.println(sParent + " is of type " + field.getClass().getName());
91
while(kidsIter.hasNext())
92             {
93                Object JavaDoc pdfObj = kidsIter.next();
94                if(pdfObj instanceof PDField)
95                {
96                    PDField kid = (PDField)pdfObj;
97                    processField(kid, "| " + sLevel, sParent);
98                }
99             }
100          }
101          else
102          {
103              String JavaDoc outputString = sLevel + sParent + "." + field.getPartialName() + " = " + field.getValue() +
104                  ", type=" + field.getClass().getName();
105
106              System.out.println(outputString);
107          }
108     }
109
110     /**
111      * This will read a PDF file and print out the form elements.
112      * <br />
113      * see usage() for commandline
114      *
115      * @param args command line arguments
116      *
117      * @throws IOException If there is an error importing the FDF document.
118      * @throws CryptographyException If there is an error decrypting the document.
119      */

120     public static void main(String JavaDoc[] args) throws IOException JavaDoc, CryptographyException
121     {
122         PDDocument pdf = null;
123         try
124         {
125             if( args.length != 1 )
126             {
127                 usage();
128             }
129             else
130             {
131                 pdf = PDDocument.load( args[0] );
132                 PrintFields exporter = new PrintFields();
133                 if( pdf.isEncrypted() )
134                 {
135                     try
136                     {
137                         pdf.decrypt( "" );
138                     }
139                     catch( InvalidPasswordException e )
140                     {
141                         System.err.println( "Error: The document is encrypted." );
142                         usage();
143                     }
144                 }
145                 exporter.printFields( pdf );
146             }
147         }
148         finally
149         {
150             if( pdf != null )
151             {
152                 pdf.close();
153             }
154         }
155     }
156     /**
157      * This will print out a message telling how to use this example.
158      */

159     private static void usage()
160     {
161         System.err.println( "usage: org.pdfbox.examples.fdf.PrintFields <pdf-file>" );
162     }
163 }
Popular Tags