1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.examples.fdf; 18 19 import java.io.IOException; 20 21 import java.util.Iterator; 22 import java.util.List; 23 24 import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; 25 import org.apache.pdfbox.pdmodel.interactive.form.PDField; 26 27 import org.apache.pdfbox.exceptions.CryptographyException; 28 import org.apache.pdfbox.exceptions.InvalidPasswordException; 29 30 import org.apache.pdfbox.pdmodel.PDDocument; 31 import org.apache.pdfbox.pdmodel.PDDocumentCatalog; 32 33 /** 34 * This example will take a PDF document and print all the fields from the file. 35 * 36 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 37 * @version $Revision: 1.16 $ 38 */ 39 public class PrintFields 40 { 41 42 /** 43 * This will print all the fields from the document. 44 * 45 * @param pdfDocument The PDF to get the fields from. 46 * 47 * @throws IOException If there is an error getting the fields. 48 */ 49 public void printFields( PDDocument pdfDocument ) throws IOException 50 { 51 PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); 52 PDAcroForm acroForm = docCatalog.getAcroForm(); 53 List fields = acroForm.getFields(); 54 Iterator fieldsIter = fields.iterator(); 55 56 System.out.println(new Integer(fields.size()).toString() + " top-level fields were found on the form"); 57 58 while( fieldsIter.hasNext()) 59 { 60 PDField field = (PDField)fieldsIter.next(); 61 processField(field, "|--", field.getPartialName()); 62 } 63 } 64 65 private void processField(PDField field, String sLevel, String sParent) throws IOException 66 { 67 List kids = field.getKids(); 68 if(kids != null) 69 { 70 Iterator kidsIter = kids.iterator(); 71 if(!sParent.equals(field.getPartialName())) 72 { 73 sParent = sParent + "." + field.getPartialName(); 74 } 75 System.out.println(sLevel + sParent); 76 //System.out.println(sParent + " is of type " + field.getClass().getName()); 77 while(kidsIter.hasNext()) 78 { 79 Object pdfObj = kidsIter.next(); 80 if(pdfObj instanceof PDField) 81 { 82 PDField kid = (PDField)pdfObj; 83 processField(kid, "| " + sLevel, sParent); 84 } 85 } 86 } 87 else 88 { 89 String outputString = sLevel + sParent + "." + field.getPartialName() + " = " + field.getValue() + 90 ", type=" + field.getClass().getName(); 91 92 System.out.println(outputString); 93 } 94 } 95 96 /** 97 * This will read a PDF file and print out the form elements. 98 * <br /> 99 * see usage() for commandline 100 * 101 * @param args command line arguments 102 * 103 * @throws IOException If there is an error importing the FDF document. 104 * @throws CryptographyException If there is an error decrypting the document. 105 */ 106 public static void main(String[] args) throws IOException, CryptographyException 107 { 108 PDDocument pdf = null; 109 try 110 { 111 if( args.length != 1 ) 112 { 113 usage(); 114 } 115 else 116 { 117 pdf = PDDocument.load( args[0] ); 118 PrintFields exporter = new PrintFields(); 119 if( pdf.isEncrypted() ) 120 { 121 try 122 { 123 pdf.decrypt( "" ); 124 } 125 catch( InvalidPasswordException e ) 126 { 127 System.err.println( "Error: The document is encrypted." ); 128 usage(); 129 } 130 } 131 exporter.printFields( pdf ); 132 } 133 } 134 finally 135 { 136 if( pdf != null ) 137 { 138 pdf.close(); 139 } 140 } 141 } 142 /** 143 * This will print out a message telling how to use this example. 144 */ 145 private static void usage() 146 { 147 System.err.println( "usage: org.apache.pdfbox.examples.fdf.PrintFields <pdf-file>" ); 148 } 149 }