Products
PDFKit.NET 4.0 PDFKit.NET 5.0
Created
1/14/2019
Tags
PDF forms

The following code adds a JavaScript action to a submit button that will disable the button after submitting.

using System.IO;

using TallComponents.PDF;
using TallComponents.PDF.Actions;
using TallComponents.PDF.Forms.Fields;
using TallComponents.PDF.JavaScript;

namespace DisableSubmitButton
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"..\..\submit-button.pdf";

            using (FileStream fileIn = File.OpenRead(path))
            {
                Document document = new Document(fileIn);

                string name = "Submit";
                PushButtonField button = document.Fields[name] as PushButtonField;
                button.Widgets[0].MouseUpActions.Add(new JavaScriptAction {
                    JavaScript = new JavaScript($"this.getField('{name}').readonly = true;")
                });

                using (FileStream fileOut = File.OpenWrite(@"..\..\disable-submit-button.pdf"))
                {
                    document.Write(fileOut);
                }
            }
        }
    }
}