2011-05-19 65 views
1

使用下面的代碼,我試圖獲取所有的字段和它們的值,但它只返回字段值。我需要做什麼才能同時獲得?如何使用iText從PDF文件獲取所有字段和值?

package PrintFiled; 
    /* 

     Usage: java TestDocument <pdf file> 

    */ 

    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 && !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(); 
     } 
    } 
+0

從您的意見,目前還不清楚你的期望。您的代碼適用於某些PDF,但不適用於其他版本?我想他們有不同的字段名稱。 – 2011-05-19 17:19:25

回答

9

要獲取所有字段和它們的值與iText的:

// you only need a PdfStamper if you're going to change the existing PDF. 
PdfReader reader = new PdfReader(pdfPath); 

AcroFields fields = reader.getAcroFields(); 

Set<String> fldNames = fields.getFields().keySet(); 

for (String fldName : fldNames) { 
    System.out.println(fldName + ": " + fields.getField(fldName)); 
} 

除此之外,這將有助於如果我們可以看到,是給你的麻煩的PDF文件。

3

這裏有更新的版本的iText 7.0.2答案:

PdfReader reader = new PdfReader(pdfPath); 
PdfDocument document = new PdfDocument(reader); 
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(document, false); 
Map<String,PdfFormField> fields = acroForm.getFormFields(); 
for (String fldName : fields.keySet()) { 
    System.out.println(fldName + ": " + fields.get(fldName).getValueAsString()); 
} 
document.close(); 
reader.close(); 
相關問題