# 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.

- **Author:** Bjoern Meyer
- **Published:** 2020-04-21
- **Modified:** 2025-11-16
- **Description:** This article shows how to pre-complete form templates with known form field values automatically.
- **4 min read** (620 words)
- **Tags:**
  - Angular
  - ASP.NET
  - Document Workflow
  - Form Fields
- **Web URL:** https://www.textcontrol.com/blog/2020/04/21/creating-pre-completed-forms-automatically/
- **LLMs URL:** https://www.textcontrol.com/blog/2020/04/21/creating-pre-completed-forms-automatically/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2020/04/21/creating-pre-completed-forms-automatically/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.Web.MVC.FormFieldsComplete

---

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](https://www.litedb.org/)) 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](https://s1-www.textcontrol.com/assets/dist/blog/2020/04/21/a/assets/demo_1.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2020/04/21/a/assets/template.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2020/04/21/a/assets/pdf.webp "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.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Form Field Compatibility: Work with AcroForms, Legacy MS Word Forms, and Content Controls](https://www.textcontrol.com/blog/2024/04/04/form-fields-working-with-acroforms-legacy-ms-word-forms-and-content-controls/llms.txt)
- [Converting DOCX Form Fields to Smart HTML Forms](https://www.textcontrol.com/blog/2021/09/06/converting-docx-form-fields-to-smart-html-forms/llms.txt)
- [DocumentViewer: Deploying Forms](https://www.textcontrol.com/blog/2021/07/02/document-viewer-deploying-forms/llms.txt)
- [Automatically Mapping TX Text Control Form Fields to JSON Data in .NET C#](https://www.textcontrol.com/blog/2026/06/03/automatically-mapping-tx-text-control-form-fields-to-json-data-in-dotnet-csharp/llms.txt)
- [How to Import and Read Form Fields from DOCX Documents in .NET on Linux](https://www.textcontrol.com/blog/2025/05/19/how-to-import-and-read-form-fields-from-docx-documents-in-net-on-linux/llms.txt)
- [Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor and Viewer](https://www.textcontrol.com/blog/2025/03/26/building-an-asp-net-core-backend-for-the-document-editor-and-viewer/llms.txt)
- [Impressions from .NET Developer Conference DDC 2024](https://www.textcontrol.com/blog/2024/11/28/impressions-from-net-developer-conference-ddc-2024/llms.txt)
- [Back from Florida: Impressions from VSLive! Orlando 2024](https://www.textcontrol.com/blog/2024/11/21/back-from-florida-impressions-from-vslive-orlando-2024/llms.txt)
- [Create Fillable PDF Forms in .NET C#](https://www.textcontrol.com/blog/2024/11/12/create-fillable-pdf-forms-in-net-c-sharp/llms.txt)
- [Implementing a Security Middleware for Angular Document Editor Applications in C#](https://www.textcontrol.com/blog/2024/10/14/implementing-a-security-middleware-for-angular-document-editor-applications/llms.txt)
- [Meet Text Control at DDC .NET Developer Conference 2024](https://www.textcontrol.com/blog/2024/10/07/meet-text-control-at-ddc-net-developer-conference-2024/llms.txt)
- [Visit Text Control at VSLive! in Orlando, Florida](https://www.textcontrol.com/blog/2024/10/01/visit-tx-text-control-at-vslive-in-orlando-florida/llms.txt)
- [Creating Advanced Tables in PDF and DOCX Documents with C#](https://www.textcontrol.com/blog/2024/09/30/creating-advanced-tables-in-pdf-and-docx-documents-with-csharp/llms.txt)
- [Back in the Pocono Mountains: Meet Text Control at TechBash 2024](https://www.textcontrol.com/blog/2024/09/24/back-in-the-pocono-mountains-meet-text-control-at-techbash-2024/llms.txt)
- [Back from Copenhagen Developers Festival 2024](https://www.textcontrol.com/blog/2024/09/05/back-from-copenhagen-developers-festival-2024/llms.txt)
- [Using the Document Editor in SPA Applications using the removeFromDom Method](https://www.textcontrol.com/blog/2024/09/02/using-the-document-editor-in-spa-applications-using-the-removefromdom-method/llms.txt)
- [Video Tutorial: Creating a MailMerge Template and JSON Data Structure](https://www.textcontrol.com/blog/2024/08/16/video-tutorial-creating-a-mailmerge-template-and-json-data-structure/llms.txt)
- [Observe When the Reporting Preview Tab is Active Using MutationObserver](https://www.textcontrol.com/blog/2024/07/23/observe-when-the-reporting-preview-tab-is-active-using-mutationobserver/llms.txt)
- [Extension Method: Flatten Forms Fields in PDF Documents using .NET C#](https://www.textcontrol.com/blog/2024/07/17/flatten-forms-fields-in-pdf-documents-using-net-c-sharp/llms.txt)
- [Mail Merge: The Pre-Shaped Data Concept Explained](https://www.textcontrol.com/blog/2024/05/30/mail-merge-the-pre-shaped-data-concept-explained/llms.txt)
- [Meet Text Control at NDC Oslo 2024](https://www.textcontrol.com/blog/2024/05/28/meet-text-control-at-ndc-oslo-2024/llms.txt)
- [Comments JavaScript API: Useful Tips and Tricks](https://www.textcontrol.com/blog/2024/04/01/comments-javascript-api-useful-tips-and-tricks/llms.txt)
- [Integrating Document Lifecycle Management with ASP.NET Core and C#](https://www.textcontrol.com/blog/2024/03/29/integrating-document-lifecycle-management-with-asp-net-core-and-c-sharp/llms.txt)
- [Building an ASP.NET Core Backend Application to Host the Document Editor and Document Viewer](https://www.textcontrol.com/blog/2024/03/14/building-an-asp-net-core-backend-application-to-host-the-document-editor-and-document-viewer/llms.txt)
- [Customizing Electronic Signature Fonts for Typed Signatures in Angular and ASP.NET Core](https://www.textcontrol.com/blog/2024/03/11/customizing-electronic-signature-fonts-for-typed-signatures-in-angular-and-asp-net-core/llms.txt)
