Products Technologies Demo Docs Blog Support Company

Merging Multiple Records with First-Page Headers and Footers

This article demonstrates how to merge multiple records with first-page headers and footers using the MailMerge class of the TX Text Control .NET Server.

Merging Multiple Records with First-Page Headers and Footers

When merging multiple records using the TX Text Control's MergeJsonData method, the append parameter determines whether the output is a single document with each record in a new section or multiple separate documents.

Templates that contain only a first page header can be tricky in scenarios where the append parameter is set to true to append all records to a resulting document. If set to false, individual documents are created and the first page header is only visible on page 1.

The Problem

But when appended, the first page header is rendered for all sections, and if the header contains merge fields, they are populated with data from the first record. So basically, after the merge, you have all the records merged into separate sections, but the header and footer contain the merge field data from the first record.

Let's take a look at the following template:

Section headers

You can see a first page header with a merge field and the main body containing a merge block. This is the JSON data that will be used to merge the content into the template.

[
    {
        "invoice": "INV-001",
        "date": "2023-10-01",
        "items": [
            {
                "name": "Widget A",
                "description": "A longer description of Widget A.",
                "quantity": 2,
                "unit_price": 10.00
            },
            {
                "name": "Widget B",
                "description": "A longer description of Widget B.",
                "quantity": 1,
                "unit_price": 20.00
            }
        ]
    },
    {
        "invoice": "INV-002",
        "date": "2023-10-02",
        "items": [
            {
                "name": "Widget C",
                "description": "A longer description of Widget C.",
                "quantity": 5,
                "unit_price": 5.00
            },
            {
                "name": "Widget D",
                "description": "A longer description of Widget D.",
                "quantity": 3,
                "unit_price": 15.00
            }
        ]
    }
]

The following code shows how to merge the JSON data into the template using the MergeJsonData method.

using System.IO;
using TXTextControl;
using TXTextControl.DocumentServer;

// Create and initialize a ServerTextControl instance
using var tx = new ServerTextControl();

// Create a new, empty document
tx.Create();

// Load the template in the internal Unicode format
tx.Load("template.tx", StreamType.InternalUnicodeFormat);

// Set up the MailMerge engine and assign the TextComponent
var mailMerge = new MailMerge
{
    TextComponent = tx
};

// Read JSON data from a file
var jsonData = File.ReadAllText("data.json");

// Merge JSON data into the loaded template
// `append: true` means merge all records into one document, each in a new section
mailMerge.MergeJsonData(jsonData, append: true);

// Save the merged result as a PDF file
tx.Save("result.pdf", StreamType.AdobePDF);

After the merge, the resulting document looks like this:

Merged document

The Solution

As you can see, the header of the second attached document contains the merge data from the first record. This is not what we want. A new section is added for each record, and a unique first page header should be added for all pages in each section. But we know that a new section is added for all records, so we can use a header and footer setting to implement this requirement.

For the first page header, we use the ConnectedToPrevious property and set it to false for the first page header. This means that the first page header is not connected to the header of the previous section.

Merged document

Now the first page header is not linked to the previous section, and the merge field is populated with the data from the current record. Below is the resulting document after the merge:

Merged document

As you can see, the first page header is now populated with the data from the current record.

Conclusion

In this article, we learned how to use the MergeJsonData method to merge multiple records into a single document with a first page header. We also learned how to set the ConnectedToPrevious property to false for the first page header to ensure that it is not connected to the previous section.

By using this approach, we can ensure that the first page header is populated with the data from the current record and not the first record.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETApplicationFieldsASP.NET Core

Find ApplicationFields within Sections and Paragraphs in .NET C#

This article shows how to find ApplicationFields within sections and paragraphs in a document using TX Text Control .NET. The implemented extension methods can be used to retrieve the sections and…


ASP.NETASP.NET CoreConference

DDC 2026 in Cologne: Documents, Spreadsheets, and a Nintendo Switch 2

After stops in Hamburg and Orlando, we wrapped up our conference year at the .NET Developer Conference in Cologne, Germany. We had a great time meeting developers and sharing our latest news about…


ASP.NETASP.NET CoreConference

Meet Text Control at Visual Studio Live! Orlando 2026

We are heading to Orlando! Text Control is proud to sponsor and exhibit at Visual Studio Live! 2026, taking place November 15-20 at the Royal Pacific Resort at Universal Orlando, Florida.


ASP.NETASP.NET CoreConference

code.talks 2026: Two Fantastic Days of Developers, Innovation, and Great…

This week, code.talks 2026 took place in Hamburg, bringing together developers, innovators, and tech enthusiasts for two days of insightful sessions and networking. We exhibited our latest…


ASP.NETASP.NET CoreConference

Meet Text Control at code.talks 2026: Spreadsheets, Documents, and More

Text Control is heading to Hamburg for code.talks 2026 on November 4-5. Stop by our booth to meet the team, explore our latest developer technologies, and be among the first to experience TX Sheet…

Share this blog post on:

​​​