We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to clear PDF fillable form fields using aspose.pdf in java

I have created a fillable PDF using aspose.pdf API. while creating the pdf form, i have created a buttonField and labeled as “Clear”. Now when i click on this button, i want to clear the fillable form fields. How to implement the clear functionality using aspose.pdf in java.
TestAsposeFeasibility_Output.pdf (53.6 KB)

Here is the sample PDF with fillable fields & Clear buttonField.

@thunga_venkat

Thank you for contacting support.

I would like to share with you that you can set value for every TextBoxField in the form, or you can set value of specific TextBoxField(s) in the form. Both approaches are explained in the code snippets below:

// Instantiate Document instance
Document pdfDocument = new Document(dataDir + "TestAsposeFeasibility_Output.pdf");

// Iterate through each field
for (com.aspose.pdf.Field formField : pdfDocument.getForm().getFields())
{    
   // If the field is a TextBoxField, perform the operation
   if ("class com.aspose.pdf.TextBoxField".equals(formField.getClass().toString()))
   {
      formField.setValue("TEST");
   }
}

// Save the updated document
pdfDocument.save(dataDir + "TestAsposeFeasibility_Output_18.1.pdf"); 

If you want to set values for selected TextBoxField(s) only, then you can differentiate by their names as in the code below:

// Instantiate Document instance
Document pdfDocument = new Document(dataDir + "TestAsposeFeasibility_Output.pdf");

// Iterate through each field
for (com.aspose.pdf.Field formField : pdfDocument.getForm().getFields())
{
   // If the fullname of field contains field_1 or field_2, perform the operation
   if (formField.getFullName().contains("field_1") || formField.getFullName().contains("field_2"))
   {
     // Cast form field as TextBox
     com.aspose.pdf.TextBoxField textBoxField = (com.aspose.pdf.TextBoxField)formField;
   
     // Modify field value
     formField.setValue("TEST");
   }
}
// Save the updated document
pdfDocument.save(dataDir + "TestAsposeFeasibility_Output_18.1.pdf");

For clearing the fields, you can pass an empty string to the field:

formField.setValue("");

Please ensure using Aspose.PDF for Java 18.1 in your environment. For more information on this topic, kindly visit Working with Forms for your kind reference. Feel free to contact us if you need any further assistance.

I understand. The above solution is to set the field values programmatically. But my requirement is to clear the fields on CLICK of a button. The sample PDF that i sent has a CLEAR button and two fillable fields. It needs to have a button action. And in button action the requirement is to clear the fillable fields. How to create a button action to clear the fillable fields.

@thunga_venkat

I would like to share with you that you can set a JavaScript Action for the button click event. I have attached generated PDF file for your kind reference TestAsposeFeasibility_JS_18.1.pdf. Please use the code snippet below in your environment and then share your feedback with us.

        // Instantiate Document instance
        Document pdfDocument = new Document(dataDir + "TestAsposeFeasibility_Output.pdf");
        
        // Iterate through each field
        for (com.aspose.pdf.Field formField : pdfDocument.getForm().getFields())
        {
            // If the fullname of field contains field_3, perform the operation
            if (formField.getFullName().contains("field_3"))
            {
                // Cast form field as Button
                com.aspose.pdf.ButtonField buttonField = (com.aspose.pdf.ButtonField)formField;
                // Set JavaScript on an event of the button
                buttonField.setOnActivated(new com.aspose.pdf.JavascriptAction("this.resetForm([\"field_1\" , \"field_2\"]);"));
            }
        }

        // Save the updated document
        pdfDocument.save(dataDir + "TestAsposeFeasibility_JS_18.1.pdf");

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