Products Technologies Demo Docs Blog Support Company

Creating Pre-Completed Forms Automatically

TX Text Control provides a comprehensive way to create documents with fillable form elements such as form text boxes, check box fields and drop-down elements. This article shows how to pre-complete form templates with known form field values automatically. The data for the pre-completion process is directly merged from a database.

Creating Pre-Completed Forms Automatically

Documents with form elements such as form text boxes, check box fields, drop-downs and date picker elements can be created like mail merge templates and dynamically pre-completed with known values. This helps to generate custom forms where some fields are already completed with known values to accelerate the completion process and to improve the user experience.

Sample Database

The sample uses a simple, serverless NoSQL database (LiteDB) to store customer address data. The Customer model is defined through the following code:

public class Customer
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Firstname { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Postalcode { get; set; }
    public DateTime DOB { get; set; }
    public bool Tax { get; set; }
}

The sample view is a very simple table that lists all customers from the database:

Selecting the user

The template contains some form text fields, drop-downs, a date picker element and a check box to cover all available form field elements. The form field names match the property names in the Customer data model.

Sample template

When clicking the button Download Form, the sample template is loaded into a ServerTextControl instance in order to loop through all form fields in each text part (headers, footers, main text, text frames).

[HttpPost]
public ActionResult GenerateForm(int Id)
{
    Customer customer;
    byte[] baCreatedFormDocument;

    // get customer by Id from database
    using (var db = new LiteDatabase(Server.MapPath("~/App_Data/customers.db")))
    {
        var col = db.GetCollection<Customer>("customers");

        col.EnsureIndex(x => x.Id);
        customer = col.Query()
            .Where(x => x.Id == Id)
            .SingleOrDefault();
    }

    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();

        // the form template
        tx.Load(Server.MapPath("~/App_data/form_template.tx"), 
                TXTextControl.StreamType.InternalUnicodeFormat);

        // loop through all form fields of each text part
        foreach (IFormattedText textPart in tx.TextParts)
        {
            foreach (FormField formField in textPart.FormFields)
            {
                // get associated value of property; check for null!
                var propertyValue = 
                    typeof(Customer).GetProperty(formField.Name)?.GetValue(customer);

                // cast values for specific form field types
                switch (formField.GetType().ToString())
                {
                    case "TXTextControl.TextFormField": // accepts strings
                    case "TXTextControl.SelectionFormField":
                        if (propertyValue != null)
                            ((TextFormField)formField).Text = (string)propertyValue;
                        break;

                    case "TXTextControl.CheckFormField": // accepts bool
                        if (propertyValue != null)
                            ((CheckFormField)formField).Checked = (bool)propertyValue;
                        break;

                    case "TXTextControl.DateFormField": // accepts Win32 file dates
                        if (propertyValue != null)
                        {
                            if (Convert.ToDateTime(propertyValue.ToString())
                                != DateTime.MinValue)
                                ((DateFormField)formField).Date =
                                    Convert.ToDateTime(propertyValue.ToString());
                        }
                        break;
                }
            }
        }

        // export form
        tx.Save(out baCreatedFormDocument, BinaryStreamType.AdobePDF);
    }

    // return the PDF as an attachment
    MemoryStream pdfStream = new MemoryStream();
    pdfStream.Write(baCreatedFormDocument, 0, baCreatedFormDocument.Length);
    pdfStream.Position = 0;

    Response.AppendHeader("content-disposition", 
                          "attachment; filename=form_" + Id + ".pdf");
    
    return new FileStreamResult(pdfStream, "application/pdf");
}

Generic Function to Merge the Data

The customer address record with the given Id is retrieved from the database and used to populate the existing form fields. The interesting code line is the following line:

var propertyValue = typeof(Customer).GetProperty(formField.Name)?.GetValue(customer);

This code is trying to read a property from the Customer object with the form field name. This makes this code very generic as the values are then casted to the proper type and applied to the appropriate property ( Text, Checked, Date).

After all available fields have been merged, the document is exported to PDF and returned as an attachment for download:

Sample template

You can download the sample from our GitHub repository to try this on your own. Let us know, if you have any questions about how to integrate document workflows into your business applications.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.CheckFormField.Checked Property
  • TXTextControl.DateFormField.Date Property
  • TXTextControl.ServerTextControl Class
  • TXTextControl.TextField.Text Property

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • TX Text Control .NET Server X18 (trial sufficient)
  • Visual Studio 2019

Angular

Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.

Learn more about Angular

Related Posts

AngularASP.NETReact

Form Field Compatibility: Work with AcroForms, Legacy MS Word Forms, and…

This article shows how to work with form fields in TX Text Control and how they are compatible with other industry standards. It explains how to load and save AcroForms, legacy MS Word forms and…


AngularASP.NETForm Fields

Converting DOCX Form Fields to Smart HTML Forms

TX Text Control provides a sophisticated way to create and deploy forms using the online editor and document viewer. This article shows how to convert forms to pure HTML forms to collect data in…


AngularASP.NETDocumentViewer

DocumentViewer: Deploying Forms

This sample shows how to deploy a form with form fields using the DocumentViewer in order to collect the completed data.


ASP.NETASP.NET CoreDOCX

How to Import and Read Form Fields from DOCX Documents in .NET on Linux

Learn how to import and read form fields from DOCX documents in .NET on Linux using TX Text Control. This article provides a step-by-step guide to help you get started with form fields in TX Text…


AngularASP.NETBlazor

Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor…

This article shows how to create a backend for the Document Editor and Viewer using ASP.NET Core. The backend can be hosted on Windows and Linux and can be used in Blazor, Angular, JavaScript, and…