Search code examples
pdfasposeaspose.pdf

How to retrieve a field value from a form using Aspose PDF in Java?


So I'm using this code to get field names and their values from a PDF file.

Document doc = new Document("path\\to\\file\\Sample.pdf");
Field[] fields = doc.getForm().getFields();
for (Field f : fields)  {
    System.out.println(f.getFullName() + " : " + f.getValue());
} 

Let's say I've the following fields in the form and the above code displays these fields as:

First Name    : John 
Middle Name   :
Last Name     : Doe
Suffix        : Jr
Date of Birth : 01-01-1985
Nationality   : American

How can I just print certain required field(s)?

Like if I want to print only the Last Name and Date of Birth using Aspose PDF


Solution

  • You can differentiate among fields with their FullName. Certain fields can be printed if you enclose the println method in an if condition, as in the code below:

    if ((f.getFullName().contains("Last Name")) || (f.getFullName().contains("Date of Birth")))  {
    System.out.println(f.getFullName() + " : " + f.getValue());
    }
    

    I hope this will be helpful. Please feel free to contact us if you need any further assistance.

    I work with Aspose as Developer Evangelist.