|
| 1 | +--- |
| 2 | +title: Creating Custom Layout Tables with RadPdfProcessing |
| 3 | +description: Learn how to create tables with various column layouts using RadPdfProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Customize Table Layouts in RadPdfProcessing Documents |
| 6 | +slug: customize-table-layout-radpdfprocessing |
| 7 | +tags: pdfprocessing, document, processing, table, layout, customize, col, span, columnspan |
| 8 | +res_type: kb |
| 9 | +ticketid: 1660148 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | +Creating tables with customized layouts, including varying column spans, is a common requirement for document processing. This article demonstrates how to achieve a table with multiple rows, each having a different column layout, using the [RadPdfProcessing]({%slug radpdfprocessing-overview%}) library. |
| 20 | + |
| 21 | +Here is demonstrated a sample design for such a custom layout: |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Solution |
| 26 | + |
| 27 | +Before starting with the solution, let's divide the table columns with red lines for better understanding how to build the layout: |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +To create a table with varying column layouts, follow the steps below: |
| 32 | + |
| 33 | +1. Define a `Table` and set its `LayoutType` to `AutoFit`. Customize the `DefaultCellProperties` to set padding and borders for the cells. |
| 34 | +2. Add table rows using `table.Rows.AddTableRow()`. |
| 35 | +3. For each row, add cells using `row.Cells.AddTableCell()`. Customize each cell's content by adding blocks of text with specific styles (e.g., font family, font style, font weight). |
| 36 | +4. To change the column layout, set the `ColumnSpan` property of the cells accordingly. |
| 37 | +5. To adjust the row height, insert content with the desired height in each cell or use the `Padding` property for minor adjustments. |
| 38 | + |
| 39 | +Here's an example code snippet demonstrating the setup: |
| 40 | + |
| 41 | +```csharp |
| 42 | + |
| 43 | + static void Main(string[] args) |
| 44 | + { |
| 45 | + RadFixedDocument fixedDocument = new RadFixedDocument(); |
| 46 | + RadFixedPage page = new RadFixedPage(); |
| 47 | + page.Size = new Size(1200, 800); |
| 48 | + fixedDocument.Pages.Add(page); |
| 49 | + |
| 50 | + FixedContentEditor editor = new FixedContentEditor(page); |
| 51 | + editor.Position.Translate(Unit.CmToDip(2), Unit.CmToDip(2)); |
| 52 | + |
| 53 | + editor.DrawTable(GenerateCustomTable()); |
| 54 | + |
| 55 | + string outputFilePath = "sample.pdf"; |
| 56 | + File.Delete(outputFilePath); |
| 57 | + PdfFormatProvider provider = new PdfFormatProvider(); |
| 58 | + |
| 59 | + using (Stream output = File.OpenWrite(outputFilePath)) |
| 60 | + { |
| 61 | + provider.Export(fixedDocument, output); |
| 62 | + } |
| 63 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 64 | + } |
| 65 | + |
| 66 | + private static Table GenerateCustomTable() |
| 67 | + { |
| 68 | + Border blackBorder = new Border(1, new RgbColor(0, 0, 0)); |
| 69 | + Table table = new Table |
| 70 | + { |
| 71 | + LayoutType = TableLayoutType.AutoFit, |
| 72 | + DefaultCellProperties = |
| 73 | + { |
| 74 | + Padding = new Thickness(5, 5, 5, 5), |
| 75 | + Borders = new TableCellBorders(blackBorder, blackBorder, blackBorder, blackBorder) |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + TableRow row = table.Rows.AddTableRow(); |
| 80 | + TableCell cell = row.Cells.AddTableCell(); |
| 81 | + cell.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Truck-Reference"); |
| 82 | + cell.ColumnSpan = 5; |
| 83 | + TableCell cell2 = row.Cells.AddTableCell(); |
| 84 | + cell2.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Date:"); |
| 85 | + cell2.ColumnSpan = 2; |
| 86 | + |
| 87 | + TableRow row2 = table.Rows.AddTableRow(); |
| 88 | + TableCell cell3 = row2.Cells.AddTableCell(); |
| 89 | + cell3.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Appointment:"); |
| 90 | + cell3.ColumnSpan = 7; |
| 91 | + |
| 92 | + TableRow row3 = table.Rows.AddTableRow(); |
| 93 | + TableCell cell4 = row3.Cells.AddTableCell(); |
| 94 | + cell4.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "CtrNr.:"); |
| 95 | + cell4.ColumnSpan = 2; |
| 96 | + TableCell cell5 = row3.Cells.AddTableCell(); |
| 97 | + cell5.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Type:"); |
| 98 | + cell5.ColumnSpan = 2; |
| 99 | + TableCell cell6 = row3.Cells.AddTableCell(); |
| 100 | + cell6.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Tara(kg):"); |
| 101 | + cell6.ColumnSpan = 2; |
| 102 | + TableCell cell7 = row3.Cells.AddTableCell(); |
| 103 | + cell7.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Net(kg):"); |
| 104 | + cell7.ColumnSpan = 1; |
| 105 | + |
| 106 | + TableRow row4 = table.Rows.AddTableRow(); |
| 107 | + TableCell cell8 = row4.Cells.AddTableCell(); |
| 108 | + cell8.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Content:"); |
| 109 | + cell8.ColumnSpan = 7; |
| 110 | + |
| 111 | + TableRow row5 = table.Rows.AddTableRow(); |
| 112 | + TableCell cell9 = row5.Cells.AddTableCell(); |
| 113 | + cell9.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "License Truck:"); |
| 114 | + cell9.ColumnSpan = 3; |
| 115 | + TableCell cell10 = row5.Cells.AddTableCell(); |
| 116 | + cell10.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Trailer:"); |
| 117 | + cell10.ColumnSpan = 4; |
| 118 | + |
| 119 | + TableRow row6 = table.Rows.AddTableRow(); |
| 120 | + TableCell cell11 = row6.Cells.AddTableCell(); |
| 121 | + cell11.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Full Cont.:"); |
| 122 | + cell11.ColumnSpan = 7; |
| 123 | + |
| 124 | + TableRow row7 = table.Rows.AddTableRow(); |
| 125 | + TableCell cell12 = row7.Cells.AddTableCell(); |
| 126 | + cell12.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Depot:"); |
| 127 | + cell12.ColumnSpan = 3; |
| 128 | + TableCell cell13 = row7.Cells.AddTableCell(); |
| 129 | + cell13.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Available:"); |
| 130 | + cell13.ColumnSpan = 4; |
| 131 | + |
| 132 | + TableRow row8 = table.Rows.AddTableRow(); |
| 133 | + TableCell cell14 = row8.Cells.AddTableCell(); |
| 134 | + cell14.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Loadingplace:"); |
| 135 | + cell14.ColumnSpan = 7; |
| 136 | + |
| 137 | + TableRow row9 = table.Rows.AddTableRow(); |
| 138 | + TableCell cell15 = row9.Cells.AddTableCell(); |
| 139 | + cell15.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Reference:"); |
| 140 | + cell15.ColumnSpan = 3; |
| 141 | + TableCell cell16 = row9.Cells.AddTableCell(); |
| 142 | + cell16.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Origin:"); |
| 143 | + cell16.ColumnSpan = 4; |
| 144 | + |
| 145 | + TableRow row10 = table.Rows.AddTableRow(); |
| 146 | + TableCell cell17 = row10.Cells.AddTableCell(); |
| 147 | + cell17.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Info Driver:"); |
| 148 | + cell17.ColumnSpan = 7; |
| 149 | + |
| 150 | + TableRow row11 = table.Rows.AddTableRow(); |
| 151 | + TableCell cell18 = row11.Cells.AddTableCell(); |
| 152 | + cell18.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Remarks:"); |
| 153 | + cell18.ColumnSpan = 7; |
| 154 | + |
| 155 | + TableRow row12 = table.Rows.AddTableRow(); |
| 156 | + TableCell cell19 = row12.Cells.AddTableCell(); |
| 157 | + cell19.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Customs:"); |
| 158 | + cell19.ColumnSpan = 1; |
| 159 | + TableCell cell20 = row12.Cells.AddTableCell(); |
| 160 | + cell20.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Procedure:"); |
| 161 | + cell20.ColumnSpan = 4; |
| 162 | + TableCell cell21 = row12.Cells.AddTableCell(); |
| 163 | + cell21.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Info:"); |
| 164 | + cell21.ColumnSpan = 2; |
| 165 | + |
| 166 | + TableRow row13 = table.Rows.AddTableRow(); |
| 167 | + TableCell cell22 = row13.Cells.AddTableCell(); |
| 168 | + cell22.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Arrical/Date/Time:"); |
| 169 | + cell22.ColumnSpan = 1; |
| 170 | + TableCell cell23 = row13.Cells.AddTableCell(); |
| 171 | + cell23.Blocks.AddBlock().InsertText(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, "Date/Stamp/Signature:"); |
| 172 | + cell23.ColumnSpan = 6; |
| 173 | + |
| 174 | + return table; |
| 175 | + } |
| 176 | + |
| 177 | +``` |
| 178 | + The code snippet achieves the below result: |
| 179 | + |
| 180 | +  |
| 181 | + |
| 182 | + When dealing the ColumnSpan functionality, pay attention to two important things: |
| 183 | + |
| 184 | +* The rows which contain cells with ColumnSpan should contain less text blocks, e.g. if ColumnSpan=4, you need to insert 4 text blocks less for this row. Hence, skip adding the text block for the cells participating in the ColumnSpan functionality. |
| 185 | + |
| 186 | +* For a column to exist and have a calculated width, it must contain at least one cell with content among the rows within the table. |
| 187 | + |
| 188 | + |
| 189 | +## See Also |
| 190 | + |
| 191 | +- [Tables in RadPdfProcessing]({%slug radpdfprocessing-editing-table%}) |
| 192 | +- [TableCell]({%slug radpdfprocessing-editing-tablecell%}) |
0 commit comments