Skip to content

Added new kb article avoid-table-splits-across-pages-radpdfprocessing #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: Avoiding Table Splits Across Pages Using FixedContentEditor in RadPdfProcessing
description: Learn how to measure tables in RadPdfProcessing to prevent them from splitting across pages in a PDF document.
type: how-to
page_title: Prevent Table Splits Across Pages in RadPdfProcessing
slug: avoid-table-splits-across-pages-radpdfprocessing
tags: pdf, processing, table, document, position, page, break, split
res_type: kb
ticketid: 1686584
---

## Environment

| Version | Product | Author |
| ---- | ---- | ---- |
| 2025.1.205| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|

## Description

When adding tables in [RadPdfProcessing]({%slug radpdfprocessing-overview%}) using the [RadFixedDocumentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/radfixeddocumenteditor), tables may sometimes split across pages if they cannot fit within the remaining space on the current page. To ensure a table fits entirely on one page and starts on a new page if necessary, you can adopt a strategy to measure the table size and calculate the remaining page height.

This article demonstrates how to prevent tables from splitting across pages and apply page breaks before adding tables using FixedContentEditor.

## Solution

Measuring the table and calculating the remaining page height is the suitable approach. For precise positioning, you can use the [FixedContentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/fixedcontenteditor). This editor allows you to measure and draw tables with exact positioning. Below is an example implementation:

```csharp
static void Main(string[] args)
{
GeneratedTableWithFixedContentEditor();
}

private static void GeneratedTableWithFixedContentEditor()
{
RadFixedDocument document = new RadFixedDocument();
RadFixedPage page = document.Pages.AddPage();

FixedContentEditor editor = new FixedContentEditor(page);
Point currentPosition = new Point(0, 0);

List<Table> tables = new List<Table>();
tables.Add(GenerateTable(50));

tables.Add(GenerateTable(15)); //can fit
//tables.Add(GenerateTable(30)); //can't fit

foreach (Table item in tables)
{
Size size = item.Measure();

if (size.Height < (page.Size.Height - currentPosition.Y))
{
editor.Position.Translate(currentPosition.X, currentPosition.Y);
currentPosition = new Point(0, currentPosition.Y + size.Height + 10);
editor.DrawTable(item);
}
else
{
page = document.Pages.AddPage();
editor = new FixedContentEditor(page);
editor.DrawTable(item);
currentPosition = new Point(0, size.Height + 10);
}

}

PdfFormatProvider provider = new PdfFormatProvider();
string outputFilePath = "exported.pdf";
File.Delete(outputFilePath);
File.WriteAllBytes(outputFilePath, provider.Export(document, TimeSpan.FromSeconds(10)));
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
}

private static Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table GenerateTable(int numberOfRows)
{
Table table = new Table();
table.LayoutType = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.TableLayoutType.FixedWidth;

for (int i = 0; i < numberOfRows; i++)
{
TableRow row = table.Rows.AddTableRow();
TableCell cell = row.Cells.AddTableCell();
cell.Blocks.AddBlock().InsertText("Row: "+i);
}

return table;
}
```

## See Also

- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%})
- [Tables]({%slug radpdfprocessing-editing-table%})
1 change: 1 addition & 0 deletions libraries/radpdfprocessing/editing/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,5 @@ As of **Q3 2024**, along with the BorderStyle.*Single*, RadPdfProcessing offers
* [Creating Custom Layout Tables with RadPdfProcessing]({%slug customize-table-layout-radpdfprocessing%})
* [Implementing Column Span in RadPdfProcessing Tables]({%slug table-column-span-radpdfprocessing%})
* [Generating a Table with RadFixedDocumentEditor]({%slug generate-table-with-radfixeddocumenteditor%})
* [Avoiding Table Splits Across Pages Using FixedContentEditor in RadPdfProcessing]({%slug avoid-table-splits-across-pages-radpdfprocessing%})