|
| 1 | +--- |
| 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. |
| 4 | +type: how-to |
| 5 | +page_title: Prevent Table Splits Across Pages in RadPdfProcessing |
| 6 | +slug: avoid-table-splits-across-pages-radpdfprocessing |
| 7 | +tags: pdf, processing, table, document, position, page, break, split |
| 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]({%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. |
| 21 | + |
| 22 | +This article demonstrates how to prevent tables from splitting across pages and apply page breaks before adding tables using FixedContentEditor. |
| 23 | + |
| 24 | +## Solution |
| 25 | + |
| 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: |
| 27 | + |
| 28 | +```csharp |
| 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 | + } |
| 89 | +``` |
| 90 | + |
| 91 | +## See Also |
| 92 | + |
| 93 | +- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) |
| 94 | +- [Tables]({%slug radpdfprocessing-editing-table%}) |
0 commit comments