@@ -18,8 +18,16 @@ ticketid: 1660148
1818## Description
1919Creating 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] ( https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview ) library.
2020
21+ Here is demonstrated a sample design for such a custom layout:
22+
23+ ![ Custom Table design] ( images/custom-table-design.png )
24+
2125## Solution
2226
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+ ![ Divided Table design] ( images/divided-table-design.png )
30+
2331To create a table with varying column layouts, follow the steps below:
2432
25331 . Define a ` Table ` and set its ` LayoutType ` to ` AutoFit ` . Customize the ` DefaultCellProperties ` to set padding and borders for the cells.
@@ -32,11 +40,153 @@ Here's an example code snippet demonstrating the setup:
3240
3341``` csharp
3442
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+
60+ using (Stream output = File .OpenWrite (outputFilePath ))
61+ {
62+ provider .Export (fixedDocument , output );
63+ }
64+ Process .Start (new ProcessStartInfo () { FileName = outputFilePath , UseShellExecute = true });
65+ }
66+
67+ private static Table GenerateCustomTable ()
68+ {
69+ Border blackBorder = new Border (1 , new RgbColor (0 , 0 , 0 ));
70+ Table table = new Table
71+ {
72+ LayoutType = TableLayoutType .AutoFit ,
73+ DefaultCellProperties =
74+ {
75+ Padding = new Thickness (5 , 5 , 5 , 5 ),
76+ Borders = new TableCellBorders (blackBorder , blackBorder , blackBorder , blackBorder )
77+ }
78+ };
79+
80+ TableRow row = table .Rows .AddTableRow ();
81+ TableCell cell = row .Cells .AddTableCell ();
82+ cell .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Truck-Reference" );
83+ cell .ColumnSpan = 5 ;
84+ TableCell cell2 = row .Cells .AddTableCell ();
85+ cell2 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Date:" );
86+ cell2 .ColumnSpan = 2 ;
87+
88+ TableRow row2 = table .Rows .AddTableRow ();
89+ TableCell cell3 = row2 .Cells .AddTableCell ();
90+ cell3 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Appointment:" );
91+ cell3 .ColumnSpan = 7 ;
92+
93+ TableRow row3 = table .Rows .AddTableRow ();
94+ TableCell cell4 = row3 .Cells .AddTableCell ();
95+ cell4 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " CtrNr.:" );
96+ cell4 .ColumnSpan = 2 ;
97+ TableCell cell5 = row3 .Cells .AddTableCell ();
98+ cell5 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Type:" );
99+ cell5 .ColumnSpan = 2 ;
100+ TableCell cell6 = row3 .Cells .AddTableCell ();
101+ cell6 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Tara(kg):" );
102+ cell6 .ColumnSpan = 2 ;
103+ TableCell cell7 = row3 .Cells .AddTableCell ();
104+ cell7 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Net(kg):" );
105+ cell7 .ColumnSpan = 1 ;
106+
107+ TableRow row4 = table .Rows .AddTableRow ();
108+ TableCell cell8 = row4 .Cells .AddTableCell ();
109+ cell8 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Content:" );
110+ cell8 .ColumnSpan = 7 ;
111+
112+ TableRow row5 = table .Rows .AddTableRow ();
113+ TableCell cell9 = row5 .Cells .AddTableCell ();
114+ cell9 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " License Truck:" );
115+ cell9 .ColumnSpan = 3 ;
116+ TableCell cell10 = row5 .Cells .AddTableCell ();
117+ cell10 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Trailer:" );
118+ cell10 .ColumnSpan = 4 ;
35119
36120
121+ TableRow row6 = table .Rows .AddTableRow ();
122+ TableCell cell11 = row6 .Cells .AddTableCell ();
123+ cell11 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Full Cont.:" );
124+ cell11 .ColumnSpan = 7 ;
125+
126+ TableRow row7 = table .Rows .AddTableRow ();
127+ TableCell cell12 = row7 .Cells .AddTableCell ();
128+ cell12 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Depot:" );
129+ cell12 .ColumnSpan = 3 ;
130+ TableCell cell13 = row7 .Cells .AddTableCell ();
131+ cell13 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Available:" );
132+ cell13 .ColumnSpan = 4 ;
133+
134+ TableRow row8 = table .Rows .AddTableRow ();
135+ TableCell cell14 = row8 .Cells .AddTableCell ();
136+ cell14 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Loadingplace:" );
137+ cell14 .ColumnSpan = 7 ;
138+
139+ TableRow row9 = table .Rows .AddTableRow ();
140+ TableCell cell15 = row9 .Cells .AddTableCell ();
141+ cell15 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Reference:" );
142+ cell15 .ColumnSpan = 3 ;
143+ TableCell cell16 = row9 .Cells .AddTableCell ();
144+ cell16 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Origin:" );
145+ cell16 .ColumnSpan = 4 ;
146+
147+ TableRow row10 = table .Rows .AddTableRow ();
148+ TableCell cell17 = row10 .Cells .AddTableCell ();
149+ cell17 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Info Driver:" );
150+ cell17 .ColumnSpan = 7 ;
151+
152+ TableRow row11 = table .Rows .AddTableRow ();
153+ TableCell cell18 = row11 .Cells .AddTableCell ();
154+ cell18 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Remarks:" );
155+ cell18 .ColumnSpan = 7 ;
156+
157+ TableRow row12 = table .Rows .AddTableRow ();
158+ TableCell cell19 = row12 .Cells .AddTableCell ();
159+ cell19 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Customs:" );
160+ cell19 .ColumnSpan = 1 ;
161+ TableCell cell20 = row12 .Cells .AddTableCell ();
162+ cell20 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Procedure:" );
163+ cell20 .ColumnSpan = 4 ;
164+ TableCell cell21 = row12 .Cells .AddTableCell ();
165+ cell21 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Info:" );
166+ cell21 .ColumnSpan = 2 ;
167+
168+ TableRow row13 = table .Rows .AddTableRow ();
169+ TableCell cell22 = row13 .Cells .AddTableCell ();
170+ cell22 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Arrical/Date/Time:" );
171+ cell22 .ColumnSpan = 1 ;
172+ TableCell cell23 = row13 .Cells .AddTableCell ();
173+ cell23 .Blocks .AddBlock ().InsertText (new FontFamily (" Helvetica" ), FontStyles .Normal , FontWeights .Normal , " Date/Stamp/Signature:" );
174+ cell23 .ColumnSpan = 6 ;
175+
176+ return table ;
177+ }
178+
37179```
38180 The code snippet achieves the below result:
39181
182+ ![ Achieved Table design] ( images/achieved-table-design.png )
183+
184+ When dealing the ColumnSpan functionality, pay attention to two important things:
185+
186+ * 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.
187+
188+ * At least one row should contain the cells participating in the ColumnSpan for the other cells. If all rows contain cells which span the same columns, you wouldn't need the column span functionality. You will just need a wider column to fit the content.
189+
40190## See Also
41191
42192- [ RadPdfProcessing Overview] ( https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview )
0 commit comments