Skip to content

Commit 0a83bc0

Browse files
committed
Slugs
1 parent 2779a68 commit 0a83bc0

File tree

2 files changed

+69
-91
lines changed

2 files changed

+69
-91
lines changed
Lines changed: 68 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Avoiding Table Splits Across Pages in RadPdfProcessing
3-
description: Learn how to manage tables in RadPdfProcessing to prevent them from splitting across pages in a PDF document.
2+
title: Avoiding Table Splits Across Pages Using FixedContentEditor in RadPdfProcessing
3+
description: Learn how to measure tables in RadPdfProcessing to prevent them from splitting across pages in a PDF document.
44
type: how-to
55
page_title: Prevent Table Splits Across Pages in RadPdfProcessing
66
slug: avoid-table-splits-across-pages-radpdfprocessing
7-
tags: radpdfprocessing, tables, document-processing, positioning, page-break
7+
tags: pdf, processing, table, document, position, page, break, split
88
res_type: kb
99
ticketid: 1686584
1010
---
@@ -19,99 +19,76 @@ ticketid: 1686584
1919

2020
When adding tables in RadPdfProcessing 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.
2121

22-
This knowledge base article also answers the following questions:
23-
- How to stop tables from splitting across pages in RadPdfProcessing?
24-
- How to apply page breaks before adding tables in RadPdfProcessing?
25-
- What is the best way to manage table positioning in RadPdfProcessing?
22+
This article demonstrates how to prevent tables from splitting across pages and apply page breaks before adding tables using FixedContentEditor.
2623

2724
## Solution
2825

29-
### Using RadFixedDocumentEditor
30-
31-
Measuring the table and calculating the remaining page height is a valid approach. Here's how you can implement it:
32-
33-
```csharp
34-
var tableSize = table1.Measure().Height;
35-
double remainingPageHeight = availablePageHeight - tableSize;
36-
37-
if (remainingPageHeight <= minimumRemainingHeight)
38-
{
39-
editor.InsertPageBreak();
40-
editor.InsertTable(table1);
41-
editor.InsertLineBreak();
42-
availablePageHeight = editor.SectionProperties.PageSize.Height
43-
- (editor.SectionProperties.PageMargins.Top + editor.SectionProperties.PageMargins.Bottom)
44-
- tableSize - lineBreakSize;
45-
}
46-
else
47-
{
48-
editor.InsertTable(table1);
49-
editor.InsertLineBreak();
50-
}
51-
```
52-
53-
### Using FixedContentEditor for Precise Control
54-
55-
For more 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:
26+
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:
5627

5728
```csharp
58-
RadFixedDocument document = new RadFixedDocument();
59-
RadFixedPage page = document.Pages.AddPage();
60-
61-
FixedContentEditor editor = new FixedContentEditor(page);
62-
Point currentPosition = new Point(0, 0);
63-
64-
List<Table> tables = new List<Table>();
65-
tables.Add(GenerateTable(50)); // Large table
66-
tables.Add(GenerateTable(15)); // Small table
67-
68-
foreach (Table item in tables)
69-
{
70-
Size size = item.Measure();
71-
72-
if (size.Height < (page.Size.Height - currentPosition.Y))
73-
{
74-
editor.Position.Translate(currentPosition.X, currentPosition.Y);
75-
currentPosition = new Point(0, currentPosition.Y + size.Height + 10);
76-
editor.DrawTable(item);
77-
}
78-
else
79-
{
80-
page = document.Pages.AddPage();
81-
editor = new FixedContentEditor(page);
82-
editor.DrawTable(item);
83-
currentPosition = new Point(0, size.Height + 10);
84-
}
85-
}
86-
87-
PdfFormatProvider provider = new PdfFormatProvider();
88-
string outputFilePath = "exported.pdf";
89-
File.Delete(outputFilePath);
90-
File.WriteAllBytes(outputFilePath, provider.Export(document, TimeSpan.FromSeconds(10)));
91-
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
92-
93-
private static Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table GenerateTable(int numberOfRows)
94-
{
95-
Table table = new Table();
96-
table.LayoutType = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.TableLayoutType.FixedWidth;
97-
98-
for (int i = 0; i < numberOfRows; i++)
99-
{
100-
TableRow row = table.Rows.AddTableRow();
101-
TableCell cell = row.Cells.AddTableCell();
102-
cell.Blocks.AddBlock().InsertText("Row: "+i);
103-
}
104-
105-
return table;
106-
}
29+
static void Main(string[] args)
30+
{
31+
GeneratedTableWithFixedContentEditor();
32+
}
33+
34+
private static void GeneratedTableWithFixedContentEditor()
35+
{
36+
RadFixedDocument document = new RadFixedDocument();
37+
RadFixedPage page = document.Pages.AddPage();
38+
39+
FixedContentEditor editor = new FixedContentEditor(page);
40+
Point currentPosition = new Point(0, 0);
41+
42+
List<Table> tables = new List<Table>();
43+
tables.Add(GenerateTable(50));
44+
45+
tables.Add(GenerateTable(15)); //can fit
46+
//tables.Add(GenerateTable(30)); //can't fit
47+
48+
foreach (Table item in tables)
49+
{
50+
Size size = item.Measure();
51+
52+
if (size.Height < (page.Size.Height - currentPosition.Y))
53+
{
54+
editor.Position.Translate(currentPosition.X, currentPosition.Y);
55+
currentPosition = new Point(0, currentPosition.Y + size.Height + 10);
56+
editor.DrawTable(item);
57+
}
58+
else
59+
{
60+
page = document.Pages.AddPage();
61+
editor = new FixedContentEditor(page);
62+
editor.DrawTable(item);
63+
currentPosition = new Point(0, size.Height + 10);
64+
}
65+
66+
}
67+
68+
PdfFormatProvider provider = new PdfFormatProvider();
69+
string outputFilePath = "exported.pdf";
70+
File.Delete(outputFilePath);
71+
File.WriteAllBytes(outputFilePath, provider.Export(document, TimeSpan.FromSeconds(10)));
72+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
73+
}
74+
75+
private static Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table GenerateTable(int numberOfRows)
76+
{
77+
Table table = new Table();
78+
table.LayoutType = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.TableLayoutType.FixedWidth;
79+
80+
for (int i = 0; i < numberOfRows; i++)
81+
{
82+
TableRow row = table.Rows.AddTableRow();
83+
TableCell cell = row.Cells.AddTableCell();
84+
cell.Blocks.AddBlock().InsertText("Row: "+i);
85+
}
86+
87+
return table;
88+
}
10789
```
10890

109-
### Key Points
110-
1. **RadFixedDocumentEditor** is flow-based, and requires measuring and adding page breaks manually.
111-
2. **FixedContentEditor** provides precise control over positioning and avoids table splits by managing the drawing location explicitly.
112-
11391
## See Also
114-
- [RadFixedDocumentEditor Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/radfixeddocumenteditor)
115-
- [FixedContentEditor Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/fixedcontenteditor)
116-
- [Drawing Tables with RadFixedDocumentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/table#drawing-table-with-radfixeddocumenteditor)
117-
- [Drawing Tables with FixedContentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/table#drawing-table-with-fixedcontenteditor)
92+
93+
- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%})
94+
- [Tables]({%slug radpdfprocessing-editing-table%})

libraries/radpdfprocessing/editing/table.md

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

0 commit comments

Comments
 (0)