title: How do I get all the fields and value from a PDF file using iText?
body: <p>Using the code below, I'm trying to get all the fields and their values, but it's only returning the field values.  What do I need to do to get both?</p>
  
  <pre><code>   package PrintFiled;
      /*
  
          Usage: java TestDocument &lt;pdf file&gt;
  
      */
  
      import java.io.File;
      import java.io.FileOutputStream;
      import java.util.Collection;
  
      import java.util.HashMap;
      import java.util.List;
      import java.util.Iterator;
      import java.util.Map;
  
      import org.pdfbox.pdmodel.interactive.form.PDField;
  
      import com.lowagie.text.pdf.AcroFields;
      import com.lowagie.text.pdf.MultiColumnText;
      import com.lowagie.text.pdf.PdfDictionary;
      import com.lowagie.text.pdf.RadioCheckField;
      import com.lowagie.text.pdf.PdfSignature;
  
      import com.lowagie.text.pdf.PdfReader;
      import com.lowagie.text.pdf.PdfStamper;
      import com.lowagie.text.pdf.AcroFields;
      import com.lowagie.text.pdf.AcroFields.*;
  
      public class TestDocument
      {
          public static void main(String[] args) throws Exception
          {
  
              // get the destination
  
              String location = "test/";
  
              if (location != null &amp;&amp; !location.endsWith(File.separator))
  
              {
  
              location = location + File.separator;
  
              }
  
  
  
              PdfReader reader = new PdfReader(location + "acc.pdf");
  
              String name = "Output.pdf";
  
              PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(location + name));
  
              AcroFields form = stamp.getAcroFields();
  
  
              String last = "subform[0].LastName[0]";
  
              String first = "subform[0].FirstName[0]";
  
              String ProcessDate="subform[0].ProcessDate[0]";
  
              form.setField(last, "HRISTOV");
  
              form.setField(first, "NEDKO");
              form.setField(ProcessDate, "Process");
  
              System.out.println("last Name  :"+last.toString());
  
  
              Map val =new HashMap();
              //val=form.getFieldCache();
              //System.out.println("Value   :"+val);
               Iterator it = val.entrySet().iterator();
               while (it.hasNext()) 
                  {
                      Map.Entry pairs = (Map.Entry)it.next();
                      System.out.println("Key   :"+pairs.getKey() + " = "+"Value  :" + pairs.getValue());
                  }
  
                      Collection fieldlist = form.getFields().keySet();
      //              for (Iterator i = val.iterator(); i.hasNext(); ) 
                           for (Iterator i = fieldlist.iterator(); i.hasNext(); ) 
                                  {
  
                                      System.out.println(i.next().toString());
  
                                  System.out.println("Value    :"+val);
                                  }
              /*List fields = form.getFields();
              Iterator fieldsIter = fields.iterator();
  
              System.out.println(new Integer(fields.size()).toString()
                      + " top-level fields were found on the form");
  
           while (fieldsIter.hasNext()) {
                  PDField field = (PDField) fieldsIter.next();
                 // processField(field, "|--", field.getPartialName());
                  System.out.println("Field  :"+fieldsIter);
             }
               */
              System.out.println("First Name:"+form.getField(first));
              System.out.println("LastName  :"+form.getField(last));
              System.out.println("ProcessDate :"+form.getField(ProcessDate));
  
              // close pdf stamper
              stamp.setFormFlattening(true);
              stamp.close();
              reader.close();
          }
      }
  </code></pre>