|
| 1 | +--- |
| 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. |
| 4 | +type: how-to |
| 5 | +page_title: Prevent Table Splits Across Pages in RadPdfProcessing |
| 6 | +slug: avoid-table-splits-across-pages-radpdfprocessing |
| 7 | +tags: radpdfprocessing, tables, document-processing, positioning, page-break |
| 8 | +res_type: kb |
| 9 | +ticketid: 1686584 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| ---- | ---- | ---- | |
| 16 | +| 2025.1.205| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 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? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 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: |
| 56 | + |
| 57 | +```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 | +} |
| 107 | +``` |
| 108 | + |
| 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 | + |
| 113 | +## 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) |
0 commit comments