For example, when creating long tables using the Mail ╰ TX Text Control .NET Server for ASP.NET
╰ DocumentServer Namespace
╰ MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services. 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.
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 ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ Table Class
╰ Split Method
Splits a table below or above the current input position. 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.
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.