From 2779a689ab0c0ac844a7cc9686b9d214416d271d Mon Sep 17 00:00:00 2001 From: KB Bot Date: Tue, 6 May 2025 13:02:41 +0000 Subject: [PATCH 1/3] Added new kb article avoid-table-splits-across-pages-radpdfprocessing --- ...le-splits-across-pages-radpdfprocessing.md | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md diff --git a/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md b/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md new file mode 100644 index 00000000..ea6884b9 --- /dev/null +++ b/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md @@ -0,0 +1,117 @@ +--- +title: Avoiding Table Splits Across Pages in RadPdfProcessing +description: Learn how to manage 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: radpdfprocessing, tables, document-processing, positioning, page-break +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 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 knowledge base article also answers the following questions: +- How to stop tables from splitting across pages in RadPdfProcessing? +- How to apply page breaks before adding tables in RadPdfProcessing? +- What is the best way to manage table positioning in RadPdfProcessing? + +## Solution + +### Using RadFixedDocumentEditor + +Measuring the table and calculating the remaining page height is a valid approach. Here's how you can implement it: + +```csharp +var tableSize = table1.Measure().Height; +double remainingPageHeight = availablePageHeight - tableSize; + +if (remainingPageHeight <= minimumRemainingHeight) +{ + editor.InsertPageBreak(); + editor.InsertTable(table1); + editor.InsertLineBreak(); + availablePageHeight = editor.SectionProperties.PageSize.Height + - (editor.SectionProperties.PageMargins.Top + editor.SectionProperties.PageMargins.Bottom) + - tableSize - lineBreakSize; +} +else +{ + editor.InsertTable(table1); + editor.InsertLineBreak(); +} +``` + +### Using FixedContentEditor for Precise Control + +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: + +```csharp +RadFixedDocument document = new RadFixedDocument(); +RadFixedPage page = document.Pages.AddPage(); + +FixedContentEditor editor = new FixedContentEditor(page); +Point currentPosition = new Point(0, 0); + +List tables = new List
(); +tables.Add(GenerateTable(50)); // Large table +tables.Add(GenerateTable(15)); // Small table + +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; +} +``` + +### Key Points +1. **RadFixedDocumentEditor** is flow-based, and requires measuring and adding page breaks manually. +2. **FixedContentEditor** provides precise control over positioning and avoids table splits by managing the drawing location explicitly. + +## See Also +- [RadFixedDocumentEditor Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/radfixeddocumenteditor) +- [FixedContentEditor Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/fixedcontenteditor) +- [Drawing Tables with RadFixedDocumentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/table#drawing-table-with-radfixeddocumenteditor) +- [Drawing Tables with FixedContentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/table#drawing-table-with-fixedcontenteditor) From 0a83bc036b09360e4b6e3f694c8c5f65f3f16b8f Mon Sep 17 00:00:00 2001 From: Desislava Yordanova Date: Thu, 15 May 2025 11:57:37 +0300 Subject: [PATCH 2/3] Slugs --- ...le-splits-across-pages-radpdfprocessing.md | 159 ++++++++---------- libraries/radpdfprocessing/editing/table.md | 1 + 2 files changed, 69 insertions(+), 91 deletions(-) diff --git a/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md b/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md index ea6884b9..88a9a773 100644 --- a/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md +++ b/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md @@ -1,10 +1,10 @@ --- -title: Avoiding Table Splits Across Pages in RadPdfProcessing -description: Learn how to manage tables in RadPdfProcessing to prevent them from splitting across pages in a PDF document. +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: radpdfprocessing, tables, document-processing, positioning, page-break +tags: pdf, processing, table, document, position, page, break, split res_type: kb ticketid: 1686584 --- @@ -19,99 +19,76 @@ ticketid: 1686584 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. -This knowledge base article also answers the following questions: -- How to stop tables from splitting across pages in RadPdfProcessing? -- How to apply page breaks before adding tables in RadPdfProcessing? -- What is the best way to manage table positioning in RadPdfProcessing? +This article demonstrates how to prevent tables from splitting across pages and apply page breaks before adding tables using FixedContentEditor. ## Solution -### Using RadFixedDocumentEditor - -Measuring the table and calculating the remaining page height is a valid approach. Here's how you can implement it: - -```csharp -var tableSize = table1.Measure().Height; -double remainingPageHeight = availablePageHeight - tableSize; - -if (remainingPageHeight <= minimumRemainingHeight) -{ - editor.InsertPageBreak(); - editor.InsertTable(table1); - editor.InsertLineBreak(); - availablePageHeight = editor.SectionProperties.PageSize.Height - - (editor.SectionProperties.PageMargins.Top + editor.SectionProperties.PageMargins.Bottom) - - tableSize - lineBreakSize; -} -else -{ - editor.InsertTable(table1); - editor.InsertLineBreak(); -} -``` - -### Using FixedContentEditor for Precise Control - -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: +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 -RadFixedDocument document = new RadFixedDocument(); -RadFixedPage page = document.Pages.AddPage(); - -FixedContentEditor editor = new FixedContentEditor(page); -Point currentPosition = new Point(0, 0); - -List
tables = new List
(); -tables.Add(GenerateTable(50)); // Large table -tables.Add(GenerateTable(15)); // Small table - -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; -} + 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
tables = new List
(); + 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; + } ``` -### Key Points -1. **RadFixedDocumentEditor** is flow-based, and requires measuring and adding page breaks manually. -2. **FixedContentEditor** provides precise control over positioning and avoids table splits by managing the drawing location explicitly. - ## See Also -- [RadFixedDocumentEditor Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/radfixeddocumenteditor) -- [FixedContentEditor Overview](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/fixedcontenteditor) -- [Drawing Tables with RadFixedDocumentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/table#drawing-table-with-radfixeddocumenteditor) -- [Drawing Tables with FixedContentEditor](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/editing/table#drawing-table-with-fixedcontenteditor) + +- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) +- [Tables]({%slug radpdfprocessing-editing-table%}) diff --git a/libraries/radpdfprocessing/editing/table.md b/libraries/radpdfprocessing/editing/table.md index e335b133..43d68986 100644 --- a/libraries/radpdfprocessing/editing/table.md +++ b/libraries/radpdfprocessing/editing/table.md @@ -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%}) From 89fc8a5fb1c5e7f32b1470f784b9c569e17ae395 Mon Sep 17 00:00:00 2001 From: Yoan Karamanov Date: Thu, 15 May 2025 13:44:39 +0300 Subject: [PATCH 3/3] added slug --- .../avoid-table-splits-across-pages-radpdfprocessing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md b/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md index 88a9a773..2b4d3e1f 100644 --- a/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md +++ b/knowledge-base/avoid-table-splits-across-pages-radpdfprocessing.md @@ -17,7 +17,7 @@ ticketid: 1686584 ## Description -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. +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.