001: /**
002: * Copyright (c) 2003-2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.fdf;
031:
032: import java.io.IOException;
033:
034: import java.util.Iterator;
035: import java.util.List;
036:
037: import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
038: import org.pdfbox.pdmodel.interactive.form.PDField;
039:
040: import org.pdfbox.exceptions.CryptographyException;
041: import org.pdfbox.exceptions.InvalidPasswordException;
042:
043: import org.pdfbox.pdmodel.PDDocument;
044: import org.pdfbox.pdmodel.PDDocumentCatalog;
045:
046: /**
047: * This example will take a PDF document and print all the fields from the file.
048: *
049: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
050: * @version $Revision: 1.16 $
051: */
052: public class PrintFields {
053:
054: /**
055: * This will print all the fields from the document.
056: *
057: * @param pdfDocument The PDF to get the fields from.
058: *
059: * @throws IOException If there is an error getting the fields.
060: */
061: public void printFields(PDDocument pdfDocument) throws IOException {
062: PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
063: PDAcroForm acroForm = docCatalog.getAcroForm();
064: List fields = acroForm.getFields();
065: Iterator fieldsIter = fields.iterator();
066:
067: System.out.println(new Integer(fields.size()).toString()
068: + " top-level fields were found on the form");
069:
070: while (fieldsIter.hasNext()) {
071: PDField field = (PDField) fieldsIter.next();
072: processField(field, "|--", field.getPartialName());
073: }
074: }
075:
076: private void processField(PDField field, String sLevel,
077: String sParent) throws IOException {
078: List kids = field.getKids();
079: if (kids != null) {
080: Iterator kidsIter = kids.iterator();
081: if (!sParent.equals(field.getPartialName())) {
082: sParent = sParent + "." + field.getPartialName();
083: }
084: System.out.println(sLevel + sParent);
085: //System.out.println(sParent + " is of type " + field.getClass().getName());
086: while (kidsIter.hasNext()) {
087: Object pdfObj = kidsIter.next();
088: if (pdfObj instanceof PDField) {
089: PDField kid = (PDField) pdfObj;
090: processField(kid, "| " + sLevel, sParent);
091: }
092: }
093: } else {
094: String outputString = sLevel + sParent + "."
095: + field.getPartialName() + " = " + field.getValue()
096: + ", type=" + field.getClass().getName();
097:
098: System.out.println(outputString);
099: }
100: }
101:
102: /**
103: * This will read a PDF file and print out the form elements.
104: * <br />
105: * see usage() for commandline
106: *
107: * @param args command line arguments
108: *
109: * @throws IOException If there is an error importing the FDF document.
110: * @throws CryptographyException If there is an error decrypting the document.
111: */
112: public static void main(String[] args) throws IOException,
113: CryptographyException {
114: PDDocument pdf = null;
115: try {
116: if (args.length != 1) {
117: usage();
118: } else {
119: pdf = PDDocument.load(args[0]);
120: PrintFields exporter = new PrintFields();
121: if (pdf.isEncrypted()) {
122: try {
123: pdf.decrypt("");
124: } catch (InvalidPasswordException e) {
125: System.err
126: .println("Error: The document is encrypted.");
127: usage();
128: }
129: }
130: exporter.printFields(pdf);
131: }
132: } finally {
133: if (pdf != null) {
134: pdf.close();
135: }
136: }
137: }
138:
139: /**
140: * This will print out a message telling how to use this example.
141: */
142: private static void usage() {
143: System.err
144: .println("usage: org.pdfbox.examples.fdf.PrintFields <pdf-file>");
145: }
146: }
|