Products Technologies Demo Docs Blog Support Company

Splitting Tables at Bookmark Positions and Cloning Table Headers

This article shows how to split tables at bookmark positions and how to clone table headers in TX Text Control .NET for Windows Forms and TX Text Control .NET Server.

Splitting Tables at Bookmark Positions and Cloning Table Headers

For example, when creating long tables using the MailMerge class, it may be helpful to split a table at certain points. One way to mark rows in a table is to use bookmarks in the table row where you want to split the table. Consider this sample document where the bookmarks are visible as marks.

Tables in TX Text Control

Splitting Tables

The document consists of two tables with separate table headers. Based on the bookmarks, the first table should be split into three tables and the second into two. Splitting tables in TX Text Control is very easy and can be done simply by calling the Split method.

Splitting Tables Based on Bookmarks

So all we have to do is loop through all the bookmarks and split the table at those locations. In addition, we store a table header per table, if it exists, and insert that table header into all resulting tables from that table.

private void SplitTablesAtTargets(TextControl textControl, string targetName = "_split")
{
    foreach (DocumentTarget documentTarget in textControl.DocumentTargets)
    {
        if (documentTarget.TargetName != targetName)
            continue;

        textControl.InputPosition = new InputPosition(documentTarget.Start);
        Table table = textControl.Tables.GetItem();

        if (table == null || !table.CanSplit)
            continue;

        byte[] headerRow = GetHeaderRow(textControl, table);

        textControl.Select(documentTarget.Start, 0);
        table.Split(TableAddPosition.Before);
        textControl.Selection.Start += 1;

        if (headerRow != null)
        {
            textControl.Selection.Load(headerRow, BinaryStreamType.InternalUnicodeFormat);
        }
    }
}

private byte[] GetHeaderRow(TextControl textControl, Table table)
{
    foreach (TableRow row in table.Rows)
    {
        if (row.IsHeader)
        {
            row.Select();

            textControl.Selection.Save(out byte[] headerRow, BinaryStreamType.InternalUnicodeFormat);
            
            return headerRow;
        }
    }
    return null;
}

The following screenshot animation shows the table before and after the split.

Tables in TX Text Control

Conclusion

Splitting tables based on bookmarks is a very powerful feature that can be used to create complex documents with tables that are split at certain points. This can be used to create invoices, reports, or any other type of document where tables need to be split at certain points.

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.NETWindows FormsWPF

Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#

This article shows how to insert MergeBlocks with the DataSourceManager and how to apply table styles to those tables. The article uses the DocumentServer class to insert MergeBlocks with the…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…


ActiveXASP.NETWindows Forms

Expert Implementation Services for Legacy System Modernization

We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.