This sample shows how to create text fields that allow only digits to be input.

In order to create such text fields a special JavaScript action should be used (see more samples about actions in corresponding section). The action must be associated with PdfTextBox.OnKeyPress event. After that any non-digit characters input in the text field will be filtered out. Note that it will only work if Javascript is supported and turned on in your PDF Viewer.

C#
using System.Diagnostics;
using System.Text;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class NumericTextField
    {
        public static void Main()
        {
            StringBuilder validationJavascript = new StringBuilder();
            validationJavascript.AppendLine("function validateNumeric(event) {");
            validationJavascript.AppendLine("    var validCharacters = \"0123456789\";");
            validationJavascript.AppendLine("    for (var i = 0; i < event.change.length; i++) {");
            validationJavascript.AppendLine("        if (validCharacters.indexOf(event.change.charAt(i)) == -1) {");
            validationJavascript.AppendLine("            app.beep(0);");
            validationJavascript.AppendLine("            event.rc = false;");
            validationJavascript.AppendLine("            break;");
            validationJavascript.AppendLine("        }");
            validationJavascript.AppendLine("    }");
            validationJavascript.AppendLine("}");

            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "NumericTextField.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.SharedScripts.Add(
                    pdf.CreateJavaScriptAction(validationJavascript.ToString())
                );

                PdfTextBox textBox = pdf.Pages[0].AddTextBox(20, 50, 100, 20);
                textBox.OnKeyPress = pdf.CreateJavaScriptAction("validateNumeric(event);");

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Text

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class NumericTextField
        Public Shared Sub Main()
            Dim validationJavascript As New StringBuilder()
            validationJavascript.AppendLine("function validateNumeric(event) {")
            validationJavascript.AppendLine("    var validCharacters = ""0123456789"";")
            validationJavascript.AppendLine("    for (var i = 0; i < event.change.length; i++) {")
            validationJavascript.AppendLine("        if (validCharacters.indexOf(event.change.charAt(i)) == -1) {")
            validationJavascript.AppendLine("            app.beep(0);")
            validationJavascript.AppendLine("            event.rc = false;")
            validationJavascript.AppendLine("            break;")
            validationJavascript.AppendLine("        }")
            validationJavascript.AppendLine("    }")
            validationJavascript.AppendLine("}")

            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "NumericTextField.pdf"

            Using pdf As New PdfDocument()

                pdf.SharedScripts.Add(pdf.CreateJavaScriptAction(validationJavascript.ToString()))

                Dim textBox As PdfTextBox = pdf.Pages(0).AddTextBox(20, 50, 100, 20)
                textBox.OnKeyPress = pdf.CreateJavaScriptAction("validateNumeric(event);")

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace