TX Text Control .NET for WPF

Create a PDF Form

Topic

A fillable PDF form starts as a TX Text Control document with form fields: text boxes, drop-down lists and check boxes that users fill out. When the document is saved as PDF, each of them becomes an interactive field of the PDF form. In the editor window of your first WPF application, a button inserts three named form fields in code and a second one saves the PDF form and opens it in a PDF reader.

Prerequisites

Add two buttons to the DockPanel of MainWindow.xaml, before the TextControl element:

<Button x:Name="insertFormButton" DockPanel.Dock="Top" Content="Insert Form Fields"
        Click="insertFormButton_Click" />
<Button x:Name="saveFormButton" DockPanel.Dock="Top" Content="Save PDF Form"
        Click="saveFormButton_Click" />

MainWindow.xaml.cs needs these using directives:

using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using TXTextControl;

How Form Fields Become a PDF Form

TX Text Control has four kinds of form fields: a TextFormField for text, a CheckFormField for a check box, a SelectionFormField for a drop-down list or a combo box, and a DateFormField for a date. They are added to the FormFields collection of the TextControl, which inserts each field at the input position.

Saving as PDF converts every form field into a PDF form field. No option has to be set for it, and the name of a form field becomes the name of the field in the PDF form.

Insert the Form Fields

The following handler writes a label and inserts a form field behind it, three times:

private void insertFormButton_Click(object sender, RoutedEventArgs e)
{
    textControl1.Selection.Text = "Name: ";
    AddFormField(new TextFormField(3000)   // width in twips while empty
    {
        Name = "name",
        DescriptiveText = "Your full name"
    });

    textControl1.Selection.Text = "\r\nCountry: ";
    AddFormField(new SelectionFormField(new string[] { "United States", "Germany", "Italy" }, 0)
    {
        Name = "country",
        DescriptiveText = "Your country"
    });

    textControl1.Selection.Text = "\r\nSend me the newsletter: ";
    AddFormField(new CheckFormField(false)
    {
        Name = "newsletter",
        DescriptiveText = "Subscribe to the newsletter"
    });
}

// Insert a form field and move the input position behind it
private void AddFormField(FormField formField)
{
    textControl1.FormFields.Add(formField);
    textControl1.InputPosition = new InputPosition(
        formField.Start + formField.Length - 1,
        TextFieldPosition.OutsideTextField);
}

The code works as follows:

  • Selection.Text inserts each label at the input position, and \r\n starts a new paragraph.
  • The TextFormField constructor takes the width of the empty field in twips. The SelectionFormField constructor takes the items of the list and the index of the preselected item, and the CheckFormField constructor whether the box is checked.
  • Name identifies the field in the PDF form. DescriptiveText, new in version 34.0, describes the field for users with disabilities in an accessible PDF.
  • FormFieldCollection.Add inserts the field at the input position.
  • After the insertion, the input position is still at the field, and text typed there could end up inside it. Setting InputPosition with TextFieldPosition.OutsideTextField moves it behind the field, so that the next label follows the field.

Save and Open the PDF Form

private void saveFormButton_Click(object sender, RoutedEventArgs e)
{
    string path = System.IO.Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        "form.pdf");

    // Form fields are saved as fillable PDF form fields
    textControl1.Save(path, StreamType.AdobePDF);

    Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
}

Run the application and click Insert Form Fields, then Save PDF Form. The PDF reader opens form.pdf with a text box after "Name:", a drop-down list of three countries after "Country:" and a check box after "Send me the newsletter:". The fields can be filled out and the PDF saved in the reader:

A PDF form in Adobe Acrobat with a filled text field and an open drop-down list offering United States, Germany and Italy.

Insert Form Fields With the Ribbon

Users can insert and edit the same form fields without code. The WPF ribbon has a Form Fields tab for this, the RibbonFormFieldsTab. Add it to the Ribbon element of a ribbon window and connect it in the RibbonFormFieldsTab attribute of the TextControl:

<WPF:RibbonFormFieldsTab x:Name="ribbonFormFieldsTab1" Header="Form Fields" />
<WPF:TextControl x:Name="textControl1"
                 RibbonFormFieldsTab="ribbonFormFieldsTab1" />

In the TX Words sample, the tab looks like this, next to the sidebar for the conditional instructions of a form:

The Form Fields tab of the WPF ribbon in the TX Words sample, with a booking form and the Conditional Instructions sidebar.

Create a WPF Ribbon Application builds the ribbon window.

Fill Out the Form in the Editor

The same form can be filled out in textControl1. The following code protects the text and leaves only the form fields editable:

textControl1.DocumentPermissions.AllowEditingFormFields = true;
textControl1.DocumentPermissions.ReadOnly = true;
textControl1.EditMode = EditMode.ReadAndSelect;

With EditMode set to ReadAndSelect, users can read and select the text, and the DocumentPermissions decide what else they may do: ReadOnly protects the document, and AllowEditingFormFields keeps the form fields editable.

Next Steps