Products Technologies Demo Docs Blog Support Company

New MailMerge Live Demo: Form Field Mail Merge

To increase the user experience, to accelerate the process and to decrease errors, parts of a document can be pre-populated. The MailMerge class can be used to merge data into form fields.

New MailMerge Live Demo: Form Field Mail Merge

The MailMerge class can be used to merge form fields with data from JSON objects or IEnumerable objects. Form fields can be pre-populated to prepare forms for users to complete or they can be flattened.

We just published a new online demo that shows how to implement these options based on a typical form used in the healthcare industry.

TX Text Control form

Pre-Selecting Form Fields

On the first button click, the form fields are pre-selected with existing JSON data. To increase the user experience, to accelerate the process and to decrease errors, parts of a document can be pre-populated with known values.

TX Text Control form

The HttpGet controller method PDFPrepare is called with the boolean flatten parameter false.

[HttpGet]
public string PDFPrepare(bool flatten) {
   byte[] bDocument;

   // create temporary ServerTextControl
   using (ServerTextControl tx = new ServerTextControl()) {
      tx.Create();

      // loading the template
      tx.Load(Server.MapPath("~/App_Data/Documents/patient_history.tx"), StreamType.InternalUnicodeFormat);

      // fire up the MailMerge engine
      using (MailMerge mailMerge = new MailMerge()) {
         mailMerge.TextComponent = tx; // connect to ServerTextControl

         // pre-selecting form fields         
         if (flatten == false)
            mailMerge.FormFieldMergeType = FormFieldMergeType.Preselect;
         else // flatten form fields
            mailMerge.FormFieldMergeType = FormFieldMergeType.Replace;

         string jsonData = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/Documents/data.json"));
         
         // merge JSON into fields
         mailMerge.MergeJsonData(jsonData);
      }

      // export to PDF
      tx.Save(out bDocument, BinaryStreamType.InternalUnicodeFormat);
   }

   // return as Base64 encoded string
   return Convert.ToBase64String(bDocument);
}

In this case, the FormFieldMergeType property is set to Preselect which basically tells the MailMerge engine to set the text of the form fields and to keep the form fields in the document.

TX Text Control form

Flattening Form Fields

On the second button, the controller method PDFPrepare is called with the boolean flatten parameter true. The FormFieldMergeType property is set to Replace. In this case, the MailMerge engine is removing known form fields and replaces them with the given text.

TX Text Control form

Live Demo

Test this demo live in our online demos.

Live Demo

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETASP.NET CoreMailMerge

When to Generate Documents Server-Side Instead of Client-Side: A Focus on…

When it comes to document generation, deciding whether to handle the task server-side or client-side is a key architectural decision for any organization. This article discusses the benefits of…


ASP.NETASP.NET CoreMailMerge

Getting Started Video Tutorial: How to use the MailMerge and…

This video tutorial shows how to use the MailMerge and ServerTextControl classes in an ASP.NET Core application using C#. This tutorial is part of the TX Text Control Getting Started series…


ASP.NETASP.NET CoreGetting Started Video

Getting Started Videos: New Text Control YouTube Channel

We just launched a new YouTube channel that is dedicated to getting started videos. The first videos show how to use the Document Editor, Document Viewer, and MailMerge components in ASP.NET Core…


ASP.NETMailMergeServerTextControl

Creating Valid XRechnung / ZUGFeRD Invoices with ASP.NET Core C#

This article shows how to create valid XRechnung and ZUGFeRD invoices with ASP.NET Core C#. The invoice is created with the help MailMerge component and the ZUGFeRD-csharp library.


ASP.NETHealthcareMailMerge

Use Case: Blood Pressure Report with Charts in C#

Providing human-readable reports in healthcare is an important part of improving transparency and patient satisfaction. This sample template shows how to visualize a series of blood pressure…