Articles in this section
Category / Section

How to export Silverlight GridTemplateColumn images to PDF document ?

5 mins read

SfDataGrid control exports the data to PDF using Syncfusion PDF Component. Generally, you can export the images to PDF document when the images are loaded inside the GridImageColumn.

You can also load the image by using GridTemplateColumn. The following screenshot illustrates the GridTemplateColumn “Pass or Fail” loaded with the corresponding images that are defined as a DataTemplate of that column.

Figure 1: SfDataGrid with image loaded in GridTemplateColumn

You cannot export the images to PDF document when it is loaded inside the GridTemplateColumn.

In that case, you can achieve it by customizing and assigning the same images to the background of PDFGridCell while exporting, with the help of CellsExportingEventHandler delegate of PdfExportingOptions. Refer to the following code example.

C#

var options = new PdfExportingOptions();
//GridCellPdfExportingEvent is assinged to CellsExportingEventHandler delegate
options.CellsExportingEventHandler = GridCellPdfExportingEvent;
var document = this.grid.ExportToPdf(options);
private void GridCellPdfExportingEvent(object sender, GridCellPdfExportingEventArgs e)
{
}

In the above code example, GridCellPdfExporting event handler is assigned to the CellsExportingEventHandler delegate and it is invoked while exporting each cell to PDF. Here, you can handle and customize each PdfGridCell while exporting, and GridCellPdfExportingEvent contains the following argument as a parameter.

  1. Sender: The sender is SfDataGrid.
  2. GridCellPdfExportingEventArgs: It contains a set of properties that are used to customize the cell to PDF and some of them are as follows:
  • CellType: You can use this property to check the type of the exported cell.
  • CellValue: It contains the actual value that is to be exported to PDF.
  • ColumnName: Specifies the column name of the exporting cell.
  • PdfGridCell: Specifies the PdfGridCell to be exported and you can customize the style, background, foreground and alignment of a particular cell.

Here, you can access the images from the resource file and you can set them to the background of PdfGridCell style as illustrated in the following code example.

C#

private void GridCellPdfExportingEvent(object sender, GridCellPdfExportingEventArgs e)
{
   //Here the image is loaded for each cell by Checking whether the ExportCellType is     RecordCell or not           
    if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsCheck")
    {
        PdfPen normalBorder = new PdfPen(PdfBrushes.DarkGray, 0.2f);
        var style = new PdfGridCellStyle();
        if (e.CellValue.Equals("Pass"))
        {
            //Access the image as stream from the specified path              
            Stream stream = Application.GetResourceStream(new Uri("Images/icon.jpg", UriKind.Relative)).Stream;
            //Create the PDFimage for the specified stream and assigned to BackgroundImage of the PdfGridCellStyle
            style.BackgroundImage = PdfImage.FromStream(stream);
            //Customize the image position as Fit 
            e.PdfGridCell.ImagePosition = PdfGridImagePosition.Fit;
            //Apply the new style to PdfGridCell's style
            e.PdfGridCell.Style = style;
        }
        else
        {
            //Access the image as stream from the specified path              
            Stream stream = Application.GetResourceStream(new Uri("Images/Untied.jpg", UriKind.Relative)).Stream;
            //Create the PDFimage for the specified stream and assigned to BackgroundImage of the PdfGridCellStyle
            style.BackgroundImage = PdfImage.FromStream(stream);
            //Customize the image position as Fit 
            e.PdfGridCell.ImagePosition = PdfGridImagePosition.Fit;
            //Apply the new style to PdfGridCell's style
            e.PdfGridCell.Style = style;
        }
        e.PdfGridCell.Style.Borders.All = normalBorder;
        e.CellValue = null;                
    }
}

The above GridTemplateColumn images are exported into PDF document as displayed in the following screenshot.

Figure 2: PDF View

You can refer to the following sample link for exporting the images that are loaded inside the GridTemplateColumn of SfDataGrid to PDF document in Silverlight platform.

Link: ExportImageToPDF_Silverlight

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied