PDF: Add a text input field to the PDF interactive form
In This Topic
Here is C#/VB.NET code that demonstrates how to create a text input field and add it to an interactive form of PDF document:
class PdfInteractiveFormTextFieldExample
{
/// <summary>
/// Creates a PDF document with numeric text field.
/// </summary>
/// <param name="filename">The filename.</param>
public static void CreateDocumentWithNumericTextField(string filename)
{
// create PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument())
{
// create interactive form in PDF document
document.InteractiveForm =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfDocumentInteractiveForm(document);
// specify that the viewer application must construct appearance streams and
// appearance properties for all widget annotations
document.InteractiveForm.NeedAppearances = true;
// create an empty page
Vintasoft.Imaging.Pdf.Tree.PdfPage page = new Vintasoft.Imaging.Pdf.Tree.PdfPage(
document, Vintasoft.Imaging.PaperSizeKind.A4);
// add page to the document
document.Pages.Add(page);
float width = 100;
float height = 25;
// create a rectangle that defines the field position on PDF page
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(
(page.Size.Width - width) / 2,
((page.Size.Height - height) / 3) * 2,
width, height);
// create a text field
Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField textField =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField(
document, "NumericField", rect, "12345");
// create the text value validation method that accepts numbers (0-9 digits) only
string javaScriptCode =
"var regex = /[^0-9]/;if(regex.test(event.change)) event.change = '';";
Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction action =
new Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction(document, javaScriptCode);
textField.AdditionalActions =
new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormFieldAdditionalActions(document);
// set the validation action for the AdditionalActions.Keystroke trigger event
textField.AdditionalActions.Keystroke = action;
// set the default appearance of text
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font = document.FontManager.GetStandardFont(
Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman);
textField.SetTextDefaultAppearance(font, 12, System.Drawing.Color.Black);
// set the border style
textField.Annotation.BorderStyle = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyle(document);
textField.Annotation.BorderStyle.Style = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyleType.Inset;
textField.Annotation.BorderStyle.Width = 1;
// set the appearance characteristics
textField.Annotation.AppearanceCharacteristics =
new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationAppearanceCharacteristics(document);
textField.Annotation.AppearanceCharacteristics.BorderColor = System.Drawing.Color.Gray;
// add the text field to the interactive form of document
document.InteractiveForm.Fields.Add(textField);
// add annotation, associated with the text field, to the page
page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document);
page.Annotations.Add(textField.Annotation);
// save the document
document.Save(filename);
}
}
}
Class PdfInteractiveFormTextFieldExample
''' <summary>
''' Creates a PDF document with numeric text field.
''' </summary>
''' <param name="filename">The filename.</param>
Public Shared Sub CreateDocumentWithNumericTextField(filename As String)
' create PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument()
' create interactive form in PDF document
document.InteractiveForm = New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfDocumentInteractiveForm(document)
' specify that the viewer application must construct appearance streams and
' appearance properties for all widget annotations
document.InteractiveForm.NeedAppearances = True
' create an empty page
Dim page As New Vintasoft.Imaging.Pdf.Tree.PdfPage(document, Vintasoft.Imaging.PaperSizeKind.A4)
' add page to the document
document.Pages.Add(page)
Dim width As Single = 100
Dim height As Single = 25
' create a rectangle that defines the field position on PDF page
Dim rect As New System.Drawing.RectangleF((page.Size.Width - width) / 2, ((page.Size.Height - height) / 3) * 2, width, height)
' create a text field
Dim textField As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField(document, "NumericField", rect, "12345")
' create the text value validation method that accepts numbers (0-9 digits) only
Dim javaScriptCode As String = "var regex = /[^0-9]/;if(regex.test(event.change)) event.change = '';"
Dim action As New Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction(document, javaScriptCode)
textField.AdditionalActions = New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormFieldAdditionalActions(document)
' set the validation action for the AdditionalActions.Keystroke trigger event
textField.AdditionalActions.Keystroke = action
' set the default appearance of text
Dim font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman)
textField.SetTextDefaultAppearance(font, 12, System.Drawing.Color.Black)
' set the border style
textField.Annotation.BorderStyle = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyle(document)
textField.Annotation.BorderStyle.Style = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyleType.Inset
textField.Annotation.BorderStyle.Width = 1
' set the appearance characteristics
textField.Annotation.AppearanceCharacteristics = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationAppearanceCharacteristics(document)
textField.Annotation.AppearanceCharacteristics.BorderColor = System.Drawing.Color.Gray
' add the text field to the interactive form of document
document.InteractiveForm.Fields.Add(textField)
' add annotation, associated with the text field, to the page
page.Annotations = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document)
page.Annotations.Add(textField.Annotation)
' save the document
document.Save(filename)
End Using
End Sub
End Class