Products Technologies Demo Docs Blog Support Company

Why Table Control in Templates is Important for Professional PDF Creation in C#

Controlling how tables behave at page breaks is an important factor in creating professional-looking documents. This article discusses the importance of table control in templates for PDF generation.

Why Table Control in Templates is Important for Professional PDF Creation in C#

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:

  • KeepLinesTogether and KeepWithNext properties on rows and paragraphs
  • Repeatable table headers for multi-page tables
  • Floating text layout for automatic content reflow

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 property of the TableRow 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;

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.

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

AngularASP.NETASP.NET Core

Creating Advanced Tables in PDF and DOCX Documents with C#

This article shows how to create advanced tables in PDF and DOCX documents using the TX Text Control .NET for ASP.NET Server component. This article shows how to create tables from scratch,…


ASP.NETASP.NET CorePDF

Validate Digital Signatures and the Integrity of PDF Documents in C# .NET

Learn how to validate digital signatures and the integrity of PDF documents using the PDF Validation component from TX Text Control in C# .NET. Ensure the authenticity and compliance of your…


ASP.NETASP.NET CorePDF

Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET

The new TXTextControl.PDF.Validation NuGet package enables you to validate PDF/UA documents and verify digital signatures directly in your code without relying on third-party tools or external…


ASP.NETASP.NET CoreC#

How To Choose the Right C# PDF Generation Library: Developer Checklist

To make your choice easy, this guide provides a systematic evaluation framework for two library categories: basic and enterprise PDF libraries. It covers matching features to use cases, evaluating…


ASP.NETASP.NET CoreDigital Signatures

Why Digitally Signing your PDFs is the Only Reliable Way to Prevent Tampering

PDF documents are widely used for sharing information because of their fixed layout and cross-platform compatibility. However, it is crucial to ensure the integrity and authenticity of these…

Summarize this blog post with:

Share on this blog post on: