For invoices, contracts, reports, and many other types of documents, creating PDFs from templates is a common requirement. While most developers and designers focus on the appearance and data binding aspects of templates, one critical detail is often overlooked: how tables behave at page breaks.

One reason for this is that typical PDF generators that are position-based (where you position a given element at a color coordinate on the page) lag behind in this functionality. You need to calculate the remaining space on a given page and implement strategies if more content is added in front of the calculated area. Not fun.

If your PDF generation engine doesn't support floating text or automatic content reflow (as is the case with many fixed-layout PDF libraries), table control becomes especially important.

The Problem

Let's take a look at a very typical problem in tables when they wrap across pages.

Breaking tables

In the provided screenshot above, the table is awkwardly split across two pages in a way that disrupts the reading flow and introduces ambiguity. Specifically, the row for "Widget H" begins on the first page, but its content, particularly the description, continues on the second page. This continuation appears below the repeated table header, making it appear as if the word "product." is the beginning of a new row, when in fact it is merely the end of the previous description.

Readers may misinterpret where one row ends and the next begins, especially when only part of a cell's content is visible before a page break. This undermines the clarity of the data and can make structured content, such as invoices or product listings, appear inconsistent or poorly generated.

The root of the problem is that the table row is allowed to split across the page boundary without any layout constraints to keep it together. While the header row is correctly repeated on the next page (which is good practice), it unintentionally adds to the confusion by visually separating the two halves of the same table row.

The Solution

Instead of calculating whether the content will fit on the current page, you should make sure that your PDF library supports preventing table rows from splitting across pages and out-of-the-box support for table headers.

TX Text Control provides fine-grained control over tables in document templates:

Preventing Page Breaks

In the next screenshot, you can see how the table looks at the page break when the table row is not allowed to break.

Fixed table

The AllowPageBreak TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
TableRow Class
AllowPageBreak Property
Gets or sets a value specifying how the table row is formatted at page breaks.
property of the TableRow TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
TableRow Class
An instance of the TableRow class represents a single row of a table in a Text Control document.
is used to specify this setting. This can be done programmatically with the following code:

TXTextControl.TableRow myRow = myTable.Rows.GetItem(1);
myRow.AllowPageBreak = false;
view raw test.cs hosted with ❤ by GitHub

Keep with Next

Let's look at another typical problem:

Table with empty cells

This screenshot shows a different, but equally problematic, page break behavior in a PDF table layout. The problem here is not that a single row is split, but that rows that belong together are visually separated, making it difficult to understand where a row begins and ends.

To solve this in TX Text Control, we simply have to set the first line to "keep with next", which is the second line.

Table with keep with next setting

You can see that the two lines that belong together are always on the same page, even if this means that there is a blank space at the bottom of a page.

Long Text

But even with this setting, we may have a problem if the description text for our products would be longer. The next screenshot shows that the second line is wrapped to the next page, even though both lines technically start on the same page.

Table with keep with next setting and long text

To solve this, we use a combination of the "keep with next" setting for the first line, and we don't allow the second line to break across pages. The screenshot below shows the clean result.

Table with keep with next setting and long text

In TX Text Control, all of this can be customized directly in the template using the full-featured WYSIWYG document editor, where end users can design their templates using MS Word skills.

The MailMerge class, which merges data into these designed templates, will follow your designed rules to create the perfect PDF.

Conclusion

In this blog post, we explored the importance of controlling table behavior at page breaks when creating PDF documents. We discussed how to prevent awkward breaks and ensure that related rows stay together, improving the readability and clarity of your documents.

By leveraging the power of TX Text Control, you can create professional-looking PDF files that maintain a consistent, easy-to-use layout, even when dealing with complex tables and data.