Reset the values of PDF form fields in C#

Spire.PDF allows us to reset the values of PDF form fields using the PdfResetAction. The following code example demonstrates how we can use Spire.PDF to implement this function.

Code snippet:

Step 1: Create a PDF document and add a new page to it.

PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();

Step 2: Create a text box field, set properties for the text box field and add it to the document.

PdfTextBoxField textBoxField = new PdfTextBoxField(page, "Name");
textBoxField.BorderColor = new PdfRGBColor(Color.AliceBlue);
textBoxField.BorderStyle = PdfBorderStyle.Solid;
textBoxField.Bounds = new RectangleF(50, 50, 100, 20);
textBoxField.Text = "Shawn Smith";
document.Form.Fields.Add(textBoxField);

Step 3: Create a button field, set properties for the button field and add it to the document.

PdfButtonField button = new PdfButtonField(page, "Reset");
button.Bounds = new RectangleF(80, 100, 50, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ToolTip = "Reset";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
document.Form.Fields.Add(button);

Step 4: Create a reset action for the button field using PdfResetAction class.

PdfResetAction resetAction = new PdfResetAction();
button.Actions.GotFocus = resetAction;

Step 5: Save and close the PDF document.

document.SaveToFile("Output.pdf");
document.Close();

Screenshot before and after resetting value:

Reset the values of PDF form fields in C#

Full code:

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;

namespace Reset_form_fields_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PDF document
            PdfDocument document = new PdfDocument();

            //Add a new page
            PdfPageBase page = document.Pages.Add();

            //Create a text box field.
            PdfTextBoxField textBoxField = new PdfTextBoxField(page, "Name");

            //Set properties for the text box field.
            textBoxField.BorderColor = new PdfRGBColor(Color.AliceBlue);
            textBoxField.BorderStyle = PdfBorderStyle.Solid;
            textBoxField.Bounds = new RectangleF(50, 50, 100, 20);
            textBoxField.Text = "Shawn Smith";

            //Add the text box field to the document
            document.Form.Fields.Add(textBoxField);

            //Create a button field.
            PdfButtonField button = new PdfButtonField(page, "Reset");
             
            //Set properties for the button field
            button.Bounds = new RectangleF(80, 100, 50, 20);
            button.BorderColor = new PdfRGBColor(Color.AliceBlue);
            button.BorderStyle = PdfBorderStyle.Solid;
            button.ToolTip = "Reset";
            button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);

            //Add the button field to the document
            document.Form.Fields.Add(button);

            //Create a reset action for the button field
            PdfResetAction resetAction = new PdfResetAction();
            button.Actions.GotFocus = resetAction;

            //Save and close the PDF document
            document.SaveToFile("Output.pdf");
            document.Close();
        }
    }
}