Text fields allow the users to input variable information on PDF files, for example, information that is not constant or that cannot be predetermined with radio button choices, such as a phone number, currency, date, time, zip code etc. When the users insert a date value, you may want to restrict the date formatting to “mm/dd/yyyy” rather than any other formatting based on every individual preference. This blog explains how to create and format text fields, using a PDF component in C#.
Part 1 - Creating a Text Field
Using this API, the programmers are able to create textbox filed in PDF by initializing an instance of the PdfTextBoxField class and setting the necessary properties such as Bounds, which determine the position and size of the textbox, Font that determines if the font size, name and style should be fixed or not.
- //Create a new object of PdfDoument, insert a page
- PdfDocument doc = new PdfDocument();
- PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins());
- //Initialize a PdfTextBoxField instance and specify the name of the field
- PdfTextBoxField textbox = new PdfTextBoxField(page, "date");
- //Set the Bounds, Font properties of textbox
- textbox.Bounds = new RectangleF(0, 0, 50, 12);
- PdfTrueTypeFont textboxFont = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Regular));
- textbox.Font = textboxFont;
- //Add textbox to Pdf and save the file
- doc.Form.Fields.Add(textbox);
- doc.SaveToFile("Textbox.pdf", FileFormat.PDF);
Part 2 - Formatting Text Field
Adobe Acrobat provides various built-in JavaScripts, such as AFNumber_Keystroke(2, 0, 0, 0, "$", true) and AFNumber_Format(2, 0, 0, 0, "$", true) to format and validate the input of the text field. The script with Format suffix is used to format the input, the script with Keystroke suffix is used to validate the input. This API offers corresponding methods listed in the table below to perform formatting and validation on the text field.
| Description | Example | JavaScript | Method |
| Date | 01/31/2008 | AFDate_FormatEx("mm/dd/yyyy"); AFDate_KeystrokeEx("mm/dd/yyyy"); | GetDateFormatString("mm/dd/yyyy"); GetDateKeystrokeString("mm/dd/yyyy"); |
| Date | 1/31/2008 | AFDate_FormatEx("m/d/yyyy"); AFDate_KeystrokeEx("m/d/yyyy"); | GetDateFormatString("m/d/yyyy"); GetDateKeystrokeString("m/d/yyyy"); |
| Zip code | 12345 | AFSpecial_Format(0); AFSpecial_Keystroke(0); | GetSpecialFormatString(0); GetSpecialKeystrokeString(0); |
| Zip+4 | 12345-1234 | AFSpecial_Format(1); AFSpecial_Keystroke(1); | GetSpecialFormatString(1); GetSpecialKeystrokeString(1); |
| Phone number | (123) 456-7890 | AFSpecial_Format(2); AFSpecial_Keystroke(2); | GetSpecialFormatString(2); GetSpecialKeystrokeString(2); |
| Money (minus sign if negative) | $12,345.00 -$12,345.00 | AFNumber_Format(2, 0, 0, 0, "$", true); AFNumber_Keystroke(2, 0, 0, 0, "$", true); | GetNumberFormatString(2, 0, 0, 0, "$", true); GetNumberKeystrokeString(2, 0, 0, 0, "$", true); |
| Validate | 1≤input value≤10 | AFRange_Validate(true,1,true,10) | GetRangeValidateString(true, 1, true, 10); |
In this project, we need the date to be displayed in “mm/dd/yyyy” format, then we can use the code snippets given below. If you want to apply another formatting or data validation to the field, just replace the methods GetDateKeystrokeString("mm/dd/yyyy") and GetDateFormatString("mm/dd/yyyy") to the corresponding ones.
- //Set a JavaScript action to be performed when uses type a keystroke into a text field.
- //This action can check the keystroke for validity and reject or modify it.
- string js = PdfJavaScript.GetDateKeystrokeString("mm/dd/yyyy");
- PdfJavaScriptAction jsAction = new PdfJavaScriptAction(js);
- textbox.Actions.KeyPressed = jsAction;
- //Set a JavaScript action to format the value of text field before showing it.
- js = PdfJavaScript.GetDateFormatString("mm/dd/yyyy");
- jsAction = new PdfJavaScriptAction(js);
- textbox.Actions.Format = jsAction;
Open the resulting file with Adobe Reader, type a number that does not match the format mm/dd/yyyy and you will get the warning given below, which indicates that you’re inserting an invalid data to the text field.

Entire code
- using Spire.Pdf;
- using Spire.Pdf.Graphics;
- using System.Drawing;
- using Spire.Pdf.Fields;
- using Spire.Pdf.Actions;
- namespace CreateTextbox
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Create a new object of PdfDoument, insert a page
- PdfDocument doc = new PdfDocument();
- PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins());
- //Draw "Created Time:" on the page
- PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Regular));
- PdfBrush brush = PdfBrushes.Black;
- float x = 50;
- float y = 50;
- float tempX= 0;
- string text = "Created Time:";
- page.Canvas.DrawString(text, font, brush, x, y);
- //Initialize a PdfTextBoxField instance and specify the name of the field
- PdfTextBoxField textbox = new PdfTextBoxField(page, "date");
- //Set the Bounds, BorderWidth, BorderStyle, Font properties of textbox
- tempX = font.MeasureString(text).Width + x + 10;
- textbox.Bounds = new RectangleF(tempX, y, 50, 12);
- PdfTrueTypeFont textboxFont = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Regular));
- textbox.Font = textboxFont;
- //Set a JavaScript action to be performed when uses type a keystroke into a text field.
- //This action can check the keystroke for validity and reject or modify it.
- string js = PdfJavaScript.GetDateKeystrokeString("mm/dd/yyyy");
- PdfJavaScriptAction jsAction = new PdfJavaScriptAction(js);
- textbox.Actions.KeyPressed = jsAction;
- //Set a JavaScript action to format the value of text field before showing it.
- js = PdfJavaScript.GetDateFormatString("mm/dd/yyyy");
- jsAction = new PdfJavaScriptAction(js);
- textbox.Actions.Format = jsAction;
- //Add textbox to Pdf and save the file
- doc.Form.Fields.Add(textbox);
- doc.SaveToFile("Textbox2.pdf", FileFormat.PDF);
- }
- }
- }
vishwanath upadhyayPosted Dec 15, 2017, 9:54 AM
Thanks for this,it's working for me.Can you please also explain how can i add some more controls on this pdf?