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); |

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?