How do I get all the fields and value from a PDF file using iText?

前端 未结 2 506
情话喂你
情话喂你 2020-12-10 23:02

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?

   pa         


        
相关标签:
2条回答
  • 2020-12-10 23:31

    To get all the fields and their values with 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 ) );
    }
    

    Beyond that, it would help if we could see a PDF that is giving you trouble.

    0 讨论(0)
  • 2020-12-10 23:38

    Here's the answer updated for iText version 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();
    
    0 讨论(0)
提交回复
热议问题