Thursday, 01 August 2019 09:07

Detect Required Fields in a PDF Document in Java

Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to detect the required form fields in an existing PDF document using Spire.PDF for Java.

import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;

public class DetectRequiredFields {

    public static void main(String[] args) {

        //load a PDF file
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Fields.pdf");

        //get form widget from the PDF document.
        PdfFormWidget formWidget = (PdfFormWidget)doc.getForm();

        //loop through the fields widget
        for (int i = 0; i < formWidget.getFieldsWidget().getList().getCapacity(); i++) {

            //get the specific field
            PdfField field = (PdfField) formWidget.getFieldsWidget().getList().get_Item(i);

            //get the field name
            String fieldName = field.getName();

            //determine if the field is required
            boolean isRequired = field.getRequired();
            if (isRequired){

                //print the required field
                System.out.println(fieldName + " is required");
            }
        }
    }
}

Detect Required Fields in a PDF Document in Java

Additional Info

  • tutorial_title: