Create And Format Text Fields In PDF In C#

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.

  1. //Create a new object of PdfDoument, insert a page  
  2. PdfDocument doc = new PdfDocument();  
  3. PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins());  
  4.   
  5. //Initialize a PdfTextBoxField instance and specify the name of the field  
  6. PdfTextBoxField textbox = new PdfTextBoxField(page, "date");  
  7.   
  8. //Set the Bounds, Font properties of textbox  
  9. textbox.Bounds = new RectangleF(0, 0, 50, 12);  
  10. PdfTrueTypeFont textboxFont = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Regular));  
  11. textbox.Font = textboxFont;  
  12.   
  13. //Add textbox to Pdf and save the file  
  14. doc.Form.Fields.Add(textbox);  
  15. 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.

DescriptionExampleJavaScriptMethod
Date01/31/2008AFDate_FormatEx("mm/dd/yyyy");
AFDate_KeystrokeEx("mm/dd/yyyy");
GetDateFormatString("mm/dd/yyyy");
GetDateKeystrokeString("mm/dd/yyyy");
Date1/31/2008AFDate_FormatEx("m/d/yyyy");
AFDate_KeystrokeEx("m/d/yyyy");
GetDateFormatString("m/d/yyyy");
GetDateKeystrokeString("m/d/yyyy");
Zip code12345AFSpecial_Format(0);
AFSpecial_Keystroke(0);
GetSpecialFormatString(0);
GetSpecialKeystrokeString(0);
Zip+412345-1234AFSpecial_Format(1);
AFSpecial_Keystroke(1);
GetSpecialFormatString(1);
GetSpecialKeystrokeString(1);
Phone number(123) 456-7890AFSpecial_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);
Validate1≤input value≤10AFRange_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. 

  1. //Set a JavaScript action to be performed when uses type a keystroke into a text field.  
  2. //This action can check the keystroke for validity and reject or modify it.  
  3. string js = PdfJavaScript.GetDateKeystrokeString("mm/dd/yyyy");  
  4. PdfJavaScriptAction jsAction = new PdfJavaScriptAction(js);  
  5. textbox.Actions.KeyPressed = jsAction;  
  6.   
  7. //Set a JavaScript action to format the value of text field before showing it.  
  8. js = PdfJavaScript.GetDateFormatString("mm/dd/yyyy");  
  9. jsAction = new PdfJavaScriptAction(js);  
  10. 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 

  1. using Spire.Pdf;  
  2. using Spire.Pdf.Graphics;  
  3. using System.Drawing;  
  4. using Spire.Pdf.Fields;  
  5. using Spire.Pdf.Actions;  
  6.   
  7. namespace CreateTextbox  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             //Create a new object of PdfDoument, insert a page  
  14.             PdfDocument doc = new PdfDocument();  
  15.             PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins());  
  16.   
  17.             //Draw "Created Time:" on the page  
  18.             PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Regular));  
  19.             PdfBrush brush = PdfBrushes.Black;  
  20.             float x = 50;  
  21.             float y = 50;  
  22.             float tempX= 0;  
  23.             string text = "Created Time:";  
  24.             page.Canvas.DrawString(text, font, brush, x, y);  
  25.               
  26.             //Initialize a PdfTextBoxField instance and specify the name of the field  
  27.             PdfTextBoxField textbox = new PdfTextBoxField(page, "date");  
  28.               
  29.             //Set the Bounds, BorderWidth, BorderStyle, Font properties of textbox  
  30.             tempX = font.MeasureString(text).Width + x + 10;  
  31.             textbox.Bounds = new RectangleF(tempX, y, 50, 12);  
  32.             PdfTrueTypeFont textboxFont = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Regular));  
  33.             textbox.Font = textboxFont;  
  34.   
  35.             //Set a JavaScript action to be performed when uses type a keystroke into a text field.  
  36.             //This action can check the keystroke for validity and reject or modify it.  
  37.             string js = PdfJavaScript.GetDateKeystrokeString("mm/dd/yyyy");  
  38.             PdfJavaScriptAction jsAction = new PdfJavaScriptAction(js);  
  39.             textbox.Actions.KeyPressed = jsAction;  
  40.   
  41.             //Set a JavaScript action to format the value of text field before showing it.  
  42.             js = PdfJavaScript.GetDateFormatString("mm/dd/yyyy");  
  43.             jsAction = new PdfJavaScriptAction(js);  
  44.             textbox.Actions.Format = jsAction;  
  45.   
  46.             //Add textbox to Pdf and save the file  
  47.             doc.Form.Fields.Add(textbox);  
  48.             doc.SaveToFile("Textbox2.pdf", FileFormat.PDF);  
  49.         }  
  50.     }  
  51. }  
Ebook Download
View all
Learn
View all