Articles in this section
Category / Section

How to export WinRT Grid loaded images in GridTemplateColumn to PDF?

2 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/Fail” loaded with the corresponding images that is 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.

Therefore, you can achieve this 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 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 etc., 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#

public void GridPdfExportingEvent(object sender, GridCellPdfExportingEventArgs args)
{
    //Here the image is loaded for each cell, by Checking whether the ExportCellType is RecordCell or not 
    if (args.CellType == ExportCellType.RecordCell && args.ColumnName == "IsCheck")
    {
        var style = new PdfGridCellStyle();
        PdfPen normalBorder = new PdfPen(PdfBrushes.DarkGray, 0.2f);
        if (args.CellValue.Equals("Pass"))
        {
            //Access the image as stream from the specified path 
            Stream imagestream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("SfDataGrid_ExportIconToPdf.Image.icon.jpg") as Stream;
            //Create the PDFimage for the specified stream and assigned to BackgroundImage of the PdfGridCellStyle
            style.BackgroundImage = PdfImage.FromStream(imagestream);
            //Customize the image position as Fit 
            args.PdfGridCell.ImagePosition = PdfGridImagePosition.Fit;
            //Apply the new style to PdfGridCell's style
            args.PdfGridCell.Style = style;
        }
        else
        {
            //Access the image as stream from the specified path 
            Stream imagestream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("SfDataGrid_ExportIconToPdf.Image.Untied.jpg") as Stream;
            //Create the PDFimage for the specified stream and assigned to BackgroundImage of the PdfGridCellStyle
            style.BackgroundImage = PdfImage.FromStream(imagestream);
            //Customize the image position as Fit 
            args.PdfGridCell.ImagePosition = PdfGridImagePosition.Center;
            //Apply the new style to PdfGridCell's style
            args.PdfGridCell.Style = style;
        }
        args.PdfGridCell.Style.Borders.All = normalBorder;
        args.CellValue = null;
    }
}

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

Figure 2: PDF View

Sample Links:

WinRT

UWP



Conclusion

I hope you enjoyed learning about how to export WinRT Grid loaded images in GridTemplateColumn to PDF.

You can refer to our WinRT Grid feature tour page to know about its other groundbreaking feature representations and how to quickly get started for configuration specifications. You can also explore our example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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