This project has moved. For the latest updates, please go here.

Suppose we have a list of users with their names and photos. We want to show the name and photo of each user in one cell and not 2 separate cells.
Built-in cell templates of PdfReport are suitable for displaying only one object per each cell. To define a custom cell template, we need to implement the IColumnItemsTemplate interface or using a shortcut:

                 columns.AddColumn(column =>
                 {
                     column.PropertyName("User");
                     column.CellsHorizontalAlignment(HorizontalAlignment.Center);
                     column.IsVisible(true);
                     column.Order(1);
                     column.Width(3);
                     column.HeaderCell("User");
                     column.CalculatedField(list =>
                         {
                             var user = list.GetSafeStringValueOf("User");
                             var photo = new Uri(list.GetSafeStringValueOf("Photo"));
                             var image = string.Format("<img src='{0}' />", photo);
                             return
                                    @"<table style='width: 100%; font-size:9pt;'>
                                                <tr>
                                                    <td>" + user + @"</td>
                                                </tr>
                                                <tr>
                                                    <td>" + image + @"</td>
                                                </tr>
                                       </table>
                                     ";
                         });
                     column.ColumnItemsTemplate(template =>
                         {
                             template.Html(); // Using iTextSharp's limited HTML to PDF capabilities (HTMLWorker class).
                         });
                 });

Here iTextSharp's HTMLWorker class is used behind the scene. By using the CalculatedField, we can inject our new value of a cell and then process it by the selected ColumnItemsTemplate.
Note: HTML to PDF capabilities of  iTextSharp's HTMLWorker class is very limited, and don't expect too much about it.

You can find this sample here: (+)

Last edited Nov 7, 2012 at 4:26 PM by VahidN, version 3

Comments

No comments yet.