Exporting

This section explains you how to Export the SfDatagrid to Excel and PDF file.

Overview

SfDataGrid control provides support to Export data to Excel and PDF. It also provides support for Grouping, Filtering, Sorting, Paging and Details View when Exporting.

Export to Excel

SfDataGrid control provides support to Export data to Excel and returns an ExcelEngine that contains the exported workbook.

The following assemblies are added along with the default assemblies.

  • Syncfusion.SfGridConverter.WinRT
  • Syncfusion.XlsIO.WinRT

You can Export SfDataGrid to Excel by using the following extension methods present in the Syncfusion.UI.Xaml.Grid.Converter namespace.

  • ExportCollectionToExcel
  • ExportToExcel

ExportToExcel

The ExportToExcel method has the following overloads.

Method Overload Description
ExportToExcel ExportToExcel ( ICollectionViewAdv,excelExportingOptions ) Exports data to Excel and returns an ExcelEngine with ExcelExportingOptions that is used to set the Exporting options.
ExportToExcel ExportToExcel( ICollectionViewAdv, excelExportingOptions,worksheet ) Export data to Excel with ExcelExportingOptions and Worksheet.

You can Export data to Excel by using the ExportToExcel method by passing ExcelExportingOptions as an argument. The following code example illustrates exporting data to Excel using the ExportToExcel Method.

  • c#
  • // Creating an instance for ExcelExportingOptions which is passed as a parameter to the ExportToExcel method.
    
    ExcelExportingOptions exportingOptions = new ExcelExportingOptions();
    
    exportingOptions.ExportingEventHandler = exportingHandler;
    
    exportingOptions.CellsExportingEventHandler=cellsExportingHandler;
    
    exportingOptions.ChildExportingEventHandler = ChildExportingHandler;
    
    
    
    // Exports Datagrid to Excel and returns ExcelEngine.
    
    ExcelEngine excelEngine = dataGrid.ExportToExcel(dataGrid.View, exportingOptions);
    
    
    
    // Gets the exported workbook from the ExcelEngine.
    
    IWorkbook workBook = excelEngine.Excel.Workbooks[0];
    
    
    
    // Saving the workbook.
    
    var savePicker = new FileSavePicker
    
    {
    
    SuggestedStartLocation = PickerLocationId.Desktop,
    
    SuggestedFileName = "Sample"
    
    };
    
    
    
    if (workBook.Version == ExcelVersion.Excel97to2003)   
    
    savePicker.FileTypeChoices.Add("Excel File (.xls)", new List<string>() { ".xls" }); 
    
    else        
    
    savePicker.FileTypeChoices.Add("Excel File (.xlsx)", new List<string>()  ".xlsx" });        
    
    var storageFile = await savePicker.PickSaveFileAsync(); 
    
    if (storageFile != null)        
    
    await workBook.SaveAsAsync(storageFile);

    ExcelExportingOptions has various properties and delegate handlers to customize the exporting behavior as compared to ExportCollectionToExcel.

    The following screenshot displays the output for the SfDataGrid.

    The following screenshot displays the exported excel sheet.

    The ExcelExportingOptions class has the following properties.

    Property Type Description DefaultValue
    AllowOutlining Boolean Specifies whether the groups exports with expand or collapse options or not.Indent space is not maintained when AllowOutlining is set to ‘true’. Default value is

    true

    .When the SfDataGrid contains the Details View definition, the AllowOutlining is set to ‘true’ internally.
    True
    Excel Version ExcelVersion Exports data to Excel in the specified workbook version: ExcelVersion.Excel2007, ExcelVersion.Excel2010, ExcelVersion.Excel2013, ExcelVersion.Excel97to2003
    Excel2007
    ExportAllPages Boolean A boolean variable that determines whether the method exports all the pages. By default, it exports the first page only. False
    StartRowIndex Integer Exports data to the specified row index in Excel. 1
    StartColumnIndex Integer Exports data to the specified column index in Excel. 1
    ExcludeColumns List < String > Skips the exporting of list of columns whose MappingName is added to the ExcludeColumns list. Empty
    ExportMode ExportMode Specifies how the data is exported into excel. This is an Enum type. ExportMode.Text - Exports the FormattedText to Excel ExportMode.Value - Exports the Format into Excel by setting Excel number format. It is the default value.
    Value
    ExportPageOptions ExportPageOptions. Specifies how the page collection is exported into excel. This is an Enum type. ExportPageOptions. ExportToSingleSheet - Export all pages into Single sheet. This is the default value.
    ExportPageOptions. ExportToDifferentSheets - Export each page into different sheets (For this case, Grouping is considered).

    ExportToSingleSheet
    ExportingEventHandler GridExcelExportingEventHandler Delegate handler customizes the styles for Header, Table Summary, Group Summary and Caption Summary rows. -
    CellsExportingEventHandler GridCellExcelExportingEventHandler Delegate handler handles or customizes the exporting of a particular cell in Excel. -
    ChildExportingEventHandler GridChildExportingEventHandler Delegate handler customizes the exporting of Details View. -

    ExportCollectionToExcel

    The ExportCollectionToExcel method has the following overloads.

    Method Overload Description
    ExportCollectionToExcel ExportCollectionToExcel ( ICollectionViewAdv ) Exports data to Excel and returns an ExcelEngine. The default Excel version is Excel2007.
    ExportCollectionToExcel ExportCollectionToExcel ( ICollectionViewAdv, ExcelVersion ) Exports data to Excel in the specified workbook version: ExcelVersion.Excel2007, ExcelVersion.Excel2010, ExcelVersion.Excel2013, ExcelVersion.Excel97to2003

    ExportCollectionToExcel
    ExportCollectionToExcel ( ICollectionViewAdv, ExcelVersion, GridExcelExportingEventHandler, GridCellExcelExportingEventHandler, ExportAllPages ) Exports data to Excel, and provides options to customize Exporting using the following parameters: GridExcelExportingEventHandler – A delegate type of GridExcelExportingEventHandler, GridCellExcelExportingEventHandler – A delegate type of GridCellExcelExportingEventHandler, ExportAllPages – A boolean variable that specifies whether all pages are exported or not. By default, it exports the first page only.

    The following code example illustrates how to export data to Excel by using the ExportCollectionToExcel method. The ExportCollectionToExcel method returns the ExcelEngine that contains the exported workbook. You can save the workbook as a stream or file by using SaveFileDialog.

  • c#
  • ExcelEngine excelEngine = this.sfGrid.ExportCollectionToExcel(sfGrid.View);
    
    
    
    // Gets the exported workbook from the ExcelEngine.
    
    IWorkbook workBook = excelEngine.Excel.Workbooks[0];
    
    
    
    // Saving the workbook.
    
    var savePicker = new FileSavePicker
    
    {
    
    SuggestedStartLocation = PickerLocationId.Desktop,
    
    SuggestedFileName = "Sample"
    
    };
    
    
    
    if (workBook.Version == ExcelVersion.Excel97to2003)   
    
    savePicker.FileTypeChoices.Add("Excel File (.xls)", new List<string>() { ".xls" }); 
    
    else        
    
    savePicker.FileTypeChoices.Add("Excel File (.xlsx)", new List<string>()  ".xlsx" });        
    
    var storageFile = await savePicker.PickSaveFileAsync(); 
    
    if (storageFile != null)        
    
    await workBook.SaveAsAsync(storageFile);

    Delegate Event Handlers

    ExportToExcel and ExportCollectionToExcel provide the following delegate handlers to customize the exporting behavior.

    • GridExcelExportingEventHandler
    • GridCellExcelExportingEventHandler
    • GridChildExportingEventHandler

    GridExcelExportingHandler

    This delegate handler allows you to customize the styles for Headers, Table Summary, Group Summary and Caption Summary rows. It includes the GridExcelExportingEventArgs event data class that contains the following properties.

    Property Type Description
    CellType ( ReadOnly property ) ExportCellType Specifies the cell type by using ExportCellType Enum. You can use this property to check the cell type and apply different cell styles based on the cell type.Cell types are as follows: ExportCellType.GroupCaptionCell, ExportCellType.GroupSummaryCell, ExportCellType.HeaderCell,ExportCellType.TableSummaryCell

    CellStyle
    ExportCellStyle Customizes the Foreground, Background and Font of Header, Table Summary, Group Summary and Caption Summary rows. CellStyle.ForeGroundBrush, CellStyle.BackGroundBrush, CellStyle.FontInfo

    WorkSheet
    IWorksheet Sets the Worksheet properties such as sheet protection, gridlines, and so on.
    Level Integer Specifies the level of the currently exporting grid when exporting Nested SfDataGrid. It is 0 for ParentGrid, 1 for First Level Nested SfDataGrid, and so on.
    Handled Boolean Skips exporting of Group Summary, Table Summary and Header. For Caption Summary, it skips exporting the data alone (an empty row is created in Excel).

    The following code example illustrates how to apply cell styles based on the cell type using GridExcelExportingHandler.

  • c#
  • // Creating an instance for ExcelExportingOptions which is passed as a parameter to the ExportToExcel method.
    
    ExcelExportingOptions exportingOptions = new ExcelExportingOptions();
    
    exportingOptions.ExportingEventHandler = exportingHandler;
    
    ExcelEngine excelEngine = dataGrid.ExportToExcel(dataGrid.View, exportingOptions);
    
    
    
    
    
    public void exportingHandler(object sender, GridExcelExportingEventArgs e)
    
    {
    
             if (e.CellType == ExportCellType.HeaderCell)
    
             {
    
               e.CellStyle.BackGroundBrush = new SolidColorBrush(Colors.LightSteelBlue);
    
               e.CellStyle.ForeGroundBrush = new SolidColorBrush(Colors.DarkRed);
    
               e.CellStyle.FontInfo.Bold = true;
    
             }
    
             else if (e.CellType == ExportCellType.GroupCaptionCell)
    
             {
    
               e.CellStyle.BackGroundBrush = new SolidColorBrush(Colors.LightSlateGray);
    
               e.CellStyle.ForeGroundBrush = new SolidColorBrush(Colors.LightYellow);
    
             }
    
             else if (e.CellType == ExportCellType.GroupSummaryCell)
    
             {
    
               e.CellStyle.BackGroundBrush = new SolidColorBrush(Colors.LightGray);
    
             }
    
               e.CellStyle.FontInfo.FontName = "Segoe UI";
    
    }

    The following screenshot displays the output for SfDataGrid.

    The following screenshot displays the Exported excel sheet.

    GridCellExcelExportingHandler

    A delegate handler handles or customizes a cell in Excel. It occurs for every exported cell. It includes the GridCellExcelExportingEventArgs event data class that contains the following properties.

    Property Type Description
    CellType( ReadOnly property ) ExportCellType Specifies the cell type by using the ExportCellType enum. You can use this property to check the cell type and apply different cell styles based on the cell type.Cell types are as follows: ExportCellType.GroupCaptionCell, ExportCellType.GroupSummaryCell, ExportCellType.HeaderCell, ExportCellType.RecordCell, ExportCellType.TableSummaryCell

    CellValue ( ReadOnly property )
    Object Contains the actual value that is exported to the Excel. You can use this value to apply formatting in Excel using Range property.
    ColumnName( ReadOnly property ) String Specifies the Column Name ( MappingName ) of the exporting cell. You can apply formatting for a particular column by checking the ColumnName.
    Handled Boolean Determines whether the cell is exported to Excel or not.
    NodeEntry( ReadOnly property ) Object Contains the RecordEntry, GroupEntry, or SummaryEntry based on the type of row. You can access the underlying BusinessObject by using this property.
    Range IRange Specifies the Excel range to be exported. It provides full access to the exporting cell in Excel.
    GridViewDefinition ViewDefinition For ParentGrid, it is null.You can access the properties of the Nested DataGrid by using this GridViewDefinition.
    Level Integer Specifies the level of the currently exporting Grid when exporting Nested DataGrid. It is 0 for ParentGrid, 1 for First Level Nested DataGrid, and so on.

    The following code example illustrates how to customize the font based on some criteria using GridCellExcelExportingHandler.

  • c#
  • ExcelExportingOptions exportingOptions = new ExcelExportingOptions();
    
    exportingOptions.CellsExportingEventHandler=cellsExportingHandler;
    
    // Exports Datagrid to Excel and returns ExcelEngine.
    
    ExcelEngine excelEngine = dataGrid.ExportToExcel(dataGrid.View, exportingOptions);
    
    
    
    public void cellsExportingHandler(object sender, GridCellExcelExportingEventArgs args)
    
    {
    
        if (args.CellType == ExportCellType.RecordCell && args.ColumnName == "OrderID" && args.Level == 0)
    
          {
    
             args.Range.CellStyle.Font.Bold = true;
    
             args.Range.CellStyle.Font.Italic = true;
    
          }
    
    }

    The following screenshot displays the output for SfDataGrid.

    The following screenshot displays the Exported excel sheet,

    GridChildExportingEventHandler

    This handler allows you to skip the exporting of DetailsView Grid columns or the whole DetailsView DataGrid. It includes the GridChildExportingEventArgs event data class that contains the following properties.

    Property Type Description
    Relational Column String It is the Relational column of the Nested DataGrid. As this is unique one, you can use this property to identify the Nested DataGrid.
    NodeEntry Object It is the RecordEntry of the parent Grid. You can use this property to identify the particular Nested DataGrid in combination with Relational Column.
    Level Integer Specifies the level of the currently exporting Nested DataGrid. It is 1 for First level Nested DataGrid, 2 for Second Level child it will be 2, and so on.
    Cancel Boolean Skips the exporting of Nested DataGrid to Excel.
    ExcludeColumns List<String> Specifies the list of column names ( MappingName ) to be excluded when exporting to Excel.

    The following code example illustrates how to skip the exporting of Details View Grid using ChildExportingHandler.

  • c#
  • ExcelExportingOptions exportingOptions = new ExcelExportingOptions();
    
    exportingOptions.ChildExportingEventHandler = ChildExportingHandler;
    
    // Exports Datagrid to Excel and returns ExcelEngine.
    
    ExcelEngine excelEngine = dataGrid.ExportToExcel(dataGrid.View, exportingOptions);
    
    
    
    private static void ChildExportingHandler(object sender, GridChildExportingEventArgs e)
    
    {
    
    // Remove the OrderID column from the Details View Grid while Exporting.
    
    if (e.Level > 1)
    
    e.ExcludeColumns.Add("OrderID");
    
    }

    The following screenshot displays the output for SfDataGrid.

    The following screenshot displays the exported excel sheet,

    Export to PDF

    SfDataGrid control provides support to Export its content into a PDF file. You can export Grouping, Sorting, Filtering, Paging, Summaries and Details view into PDF.

    The following assemblies are added along with default DLL’s.

    • Syncfusion.SfGridConverter.WinRT
    • Syncfusion.Pdf.WinRT

    The following extension methods present in the Syncfusion.UI.Xaml.Grid.converter namespace is used to export the SfDataGrid into PDF_._

    Method Prototype Description
    ExportToPdf ExportToPdf ( this SfDataGrid SfGrid ) This Method exports the SfDataGrid into PDF and returns the PDF Document
    ExportToPdf ExportToPdf ( this SfDataGrid SfGrid, PdfExportingOptions PdfExportingOptions ) This Method exports the SfDataGrid into PDF based on settings in the PDFExportingOptions and returns the PDFDocument
    ExportToPdf ExportToPdf ( this SfDataGrid SfGrid, ICollectionViewAdv gridCollectionView, PdfExportingOptions PDFExportingOptions ) This Method exports the specified CollectionView into PDF based on settings in the PDFExportingOptions and returns the PDFDocument
    ExportToPdfGrid ExportToPdfGrid ( this SfDataGrid SfGrid, ICollectionViewAdv gridCollectionView, PdfExportingOptions PdfExportingOptions ) This Method exports the specified CollectionView into PDF based on settings in the PDFExportingOptions and returns the PDFGrid

    You can customize how the SfDataGrid content is exported into PDF by using various properties and delegate handlers in the PdfExportingOptions.

    PdfExportingOptions has the following properties and delegate handlers.

    Property Type Description Default Value
    AutoColumnWidth Boolean Determines whether the column widths are automatically calculated based on its content or not. True
    AutoRowHeight Boolean Determines whether the row heights are automatically calculated based on its content or not. True
    ExcludeColumns List < string > Skips the exporting of list of columns whose MappingName is added to this list. Empty List
    ExportFormat Boolean Determines whether the values are exported with format or not. True
    ExportAllPages Boolean Determines whether all the pages are exported or not. This property is applicable only for paged collection.When it is set to ‘false’, then only current page is exported.When it is set to ‘true’, then all pages are exported. For this case, grouping is not considered while exporting. False
    ExportGroups Boolean Determines whether groups are exported or not True
    ExportGroupSummary Boolean Determines whether the group summaries are exported or not True
    ExportTableSummary Boolean Determines whether the table summaries are exported or not True
    ExportStackedHeaders Boolean Determines whether the stacked headers are exported or not False
    ExportDetailsView Boolean Determines whether the Details view is exported or not. Note: When it is set to ‘true’, it exports the details view that are expanded._' | markdownify }}Limitation: While exporting Details view, FitAllColumnsInOnePage is set to ‘true’ internally as horizontal pagination is not supported for Nested. False
    ExportAllDetails Boolean Determines whether Details view is exported or not when details view DataGrid is not expanded. False
    FitAllColumnsInOnePage Boolean Determines whether all the columns are fit in a page or not False
    RepeatHeaders Boolean Determines whether the headers are repeated in each page or not True
    ExportingEventHandler Delegate handler Customizes the styles for Header, Record, Table Summary, and Group Summary and Caption Summary rows. Null
    CellsExportingEventHandler Delegate handler Handles or customizes the exporting of each cell. Null
    ChildGridExportingEventHandler Delegate handler Handles or customizes the exporting of Details View. Null
    PageHeaderFooterEventHandler Delegate handler Adds the header and footer of the PDF page. Null

    The following code example illustrates you how to export SfDataGrid into PDF by using the ExportToPdf method.

  • c#
  • var document = dataGrid.ExportToPdf();
    
    
    
    var savePicker = new FileSavePicker
    
            {
    
                SuggestedStartLocation = PickerLocationId.Desktop,
    
                SuggestedFileName = "Sample"
    
            };
    
    
    
    
    
    savePicker.FileTypeChoices.Add("Pdf Files(.pdf)", new List<string>() { ".pdf" });
    
    
    
    var storageFile = await savePicker.PickSaveFileAsync();
    
    
    
    if (storageFile != null)
    
    {
    
        await pdfDocument.SaveAsync(storageFile);
    
    
    
        var messageDialog = new MessageDialog("Do you want to view the Document?", "File has been created successfully.");
    
    
    
        var yesCmd = new UICommand("Yes");
    
        var noCmd = new UICommand("No");
    
        messageDialog.Commands.Add(yesCmd);
    
        messageDialog.Commands.Add(noCmd);
    
        var cmd = await messageDialog.ShowAsync();
    
        if (cmd == yesCmd)
    
        {
    
            // Launch the saved file
    
            bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile);
    
        }
    
    }

    The following screenshot displays the exported PDF file

    Delegate and Events

    Export to PDF provides the following delegate handler to customize the exporting behavior.

    • GridPdfExportingEventHandler

    • GridCellPdfExportingEventHandler

    • ChildGridPdfExportingEventHandler

    • PdfHeaderFooterEventHandler

    GridPdfExportingEventHandler

    This delegate handler allows you to customize the styles for Header, Records, Table Summary, Group Summary and Caption Summary rows. It is invoked for each row types. It includes the GridPdfExportingEventArgs class that contains the following properties.

    Property Type Description
    CellType ExportCellType You can use this property to check the cell type and apply different cell styles based on the cell type.Cell types are as follows:ExportCellType.GroupCaptionCell, ExportCellType.GroupSummaryCell, ExportCellType.HeaderCell, ExportCellType.RecordCell, ExportCellType.StackedHeaderCell ,ExportCellType.TableSummaryCell, ExportCellType.TopTableSummaryCell
    CellStyle PdfGridCellStyle You can use this property to customize the Style (Foreground, Background, Font, Border etc.,)
    PdfGrid PdfGrid You can use this property to customize the PdfGrid’s properties such as Background, CellPadding, CellSpacing etc.
    Level Integer Specifies the level of the currently exporting Grid when exporting NestedDataGrid. It is 0 for ParentGrid, 1 for First Level Nested DataGrid, and so on.

    The following code example illustrates how to apply cell styles based on the cell type by using GridPdfExportingEventHandler_._

  • c#
  • var options = new PdfExportingOptions();
    
    options.ExportingEventHandler = GridPDFExportingEventHandler;
    
    var document = dataGrid.ExportToPdf(options);
    
    
    
    static void GridPDFExportingEventHandler(object sender, GridPdfExportingEventArgs e)
    
    {
    
        if (e.CellType == ExportCellType.HeaderCell)
    
        {
    
            e.CellStyle.BackgroundBrush = PdfBrushes.LightSteelBlue;
    
        }
    
        else if (e.CellType == ExportCellType.GroupCaptionCell)
    
        {
    
            e.CellStyle.BackgroundBrush = PdfBrushes. Purple;
    
        }
    
        else if (e.CellType == ExportCellType.GroupSummaryCell)
    
        {
    
            e.CellStyle.BackgroundBrush = PdfBrushes.Azure;
    
        }
    
       }

    The following screenshot displays the exported PDF file.

    GridCellPdfExportingEventHandler

    This delegate handler allows you to handle or customize each cell in PDF. It is invoked while exporting each cell. It includes the GridCellPdfExportingEventArgs class that contains the following properties.

    Property Type Description
    CellType ExportCellType You can use this property to check the cell type and apply different cell styles based on the cell type.Cell types are as follows: ExportCellType.GroupCaptionCell, ExportCellType.GroupSummaryCell, ExportCellType.HeaderCell, ExportCellType.RecordCell, ExportCellType.StackedHeaderCell, ExportCellType.TableSummaryCell, ExportCellType.TopTableSummaryCell
    CellValue Object Contains the actual value that is exported to the PDF. You can also customize this value.
    ColumnName String Specifies the Column Name (MappingName) of the exporting cell. You can customize the value for a particular column by checking the ColumnName.
    NodeEntry Object Contains the RecordEntry or GroupEntry or SummaryEntry based on the type of row. You can access the underlying BusinessObject by using this property.
    PropertyAccessProvider IPropertyAccessProvider Contains the PropertyAccessProvider of the view. You can use this to get value of a cell using NodeEntry.
    GridViewDefinition ViewDefinition It is the GridViewDefinition of the SfDataGrid. For ParentGrid, it is null.You can access the properties of the NestedGrid by using this GridViewDefinition.
    Level Integer Specifies the level of the currently exporting Grid when exporting Nested DataGrid. It is 0 for ParentGrid, 1 for First Level Nested DataGrid, and so on.
    PdfGridCell PdfGridCell Specifies the PDFGridCell to be exported. You can use this to customize the properties (Background, Foreground, Font, Alignment etc.,) of particular cell
    Handled Boolean Determines whether the cell is exported to PDF or not.

    The following code example illustrates how to customize the particular PDFGridCell based on some criteria by using GridCellPdfExportingEventHandler.

  • c#
  • var options = new PdfExportingOptions();
    
    options.CellsExportingEventHandler = GridCellPDFExportingEventHandler;
    
    var document = dataGrid.ExportToPdf(options);
    
    
    
    var cellStyle = new PdfGridCellStyle();
    
    cellStyle.StringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Right };
    
    var font = new Font("Segoe UI", 9f, System.Drawing.FontStyle.Regular);
    
    cellStyle.Font = new PdfTrueTypeFont(font, true);
    
    cellStyle.Borders.All = new PdfPen(PdfBrushes.DarkGray, 0.2f);
    
    
    
    static void GridCellPDFExportingEventHandler(object sender, GridCellPdfExportingEventArgs e)
    
    {
    
        if (e.CellType == ExportCellType.RecordCell && (e.ColumnName == "OrderID" || e.ColumnName == "EmployeeID"))
    
        {
    
            e.PDFGridCell.Style = cellStyle;
    
        }
    
    }

    The following screenshot displays the exported PDF file.

    Child GridPdfExportingEventHandler

    This handler allows you to skip or customize the exporting of DetailsViewGrid. It is invoked for each details view while exporting. It includes the ChildGridPdfExportingEventArgs that includes the following properties.

    Property Type Description
    RelationalColumn String It is the Relational column of the Nested DataGrid. You can use this property to identify the Nested DataGrid.
    NodeEntry Object It is the RecordEntry of the parent Grid. You can use this property to identify the particular Nested DataGrid in combination with Relational Column.
    Level Int Specifies the level of the currently exporting Nested DataGrid. It is 1 for First level Nested DataGrid, 2 for Second Level child it will be 2, and so on.
    PdfExportingOptions PdfExportingOptions You can use this to customize the exporting behavior options for details view separately.
    Cancel Bool Skips the exporting of Nested DataGrid to PDF.

    The following code example illustrates how to skip and customize the exporting of Details View Grid by using ChildGridExportingHandler.

  • c#
  • var options = new PdfExportingOptions();
    
    options.ExportDetailsView= true;
    
    options.ChildGridExportingEventHandler = ChildGridPDFExportingEventHandler;
    
    var document = dataGrid.ExportToPdf(options);
    
    static void ChildGridPDFExportingEventHandler(object sender, ChildGridPdfExportingEventArgs e)
    
    {
    
        //Skips the exporting of details view from second level
    
        if (e.Level > 1)
    
            e.Cancel = true;
    
        else
    
        {
    
            //skips the exporting of OrderID column of details view.
    
            e.PdfExportingOptions.ExcludeColumns = new List<string>() { "OrderID" };
    
        }
    
    }

    The following screenshot displays the exported PDF file.

    PdfHeaderFooterEventHandler

    This handler allows you to add the Header and Footer to each page of exported PDF file. It is invoked only once while exporting SfDataGrid into PdfDocument using ExportToPdf method and it is not invoked while using ExportToPdfGrid method. It includes the PdfHeaderFooterEventArgs that contains the following properties.

    Property Type Description
    PdfPage PdfPage Stores the PDF page information (page width, height etc.). From this information, you can customize the Header and Footer template.
    PdfDocumentTemplate PdfDocumentTemplate Allows you to add the Header and Footer into the PdfDocument template.

    The following code example illustrates how to add Header and Footer into the PDF document.

  • c#
  • var options = new PdfExportingOptions();
    
    options.PageHeaderFooterEventHandler = PDFHeaderFooterEventHandler;
    
    var document = dataGrid.ExportToPdf(options); 
    
    
    
    static void PDFHeaderFooterEventHandler(object sender, PdfHeaderFooterEventArgs e)
    
    {
    
        var width = e.PdfPage.GetClientSize().Width;
    
    
    
        PdfPageTemplateElement header = new PdfPageTemplateElement(width, 38);
    
        header.Graphics.DrawImage(PdfImage.FromFile(@"..\..\Resources\Header.jpg"), 0, 0, width / 3f, 34);
    
        e.PdfDocumentTemplate.Top = header;
    
    
    
        PdfPageTemplateElement footer = new PdfPageTemplateElement(width, 30);
    
        footer.Graphics.DrawImage(PdfImage.FromFile(@"..\..\Resources\Footer.jpg"), 0, 0);
    
        e.PdfDocumentTemplate.Bottom = footer;
    
    }

    The following screenshot displays you the exported PDF file.

    How To

    How to embedded fonts in PDF export

    This feature provides support to embed your own fonts into exported PDF file.You can create a new font from the font file and you can set that font into the PDF by using the delegate handlers.

    The following code example illustrates how to create font from the font file and set it into the PdfGridCell.

  • c#
  • static void GridPDFExportingEventHandler(object sender, GridPdfExportingEventArgs e)
    
    {
    
        if(e.CellType == ExportCellType.RecordCell)
    
        {
    
            //creates a new font from the font file.
    
            var filePath = @"..\..\Resources\segoeui.ttf";
    
            var font = new PdfTrueTypeFont(filePath, 9f, PdfFontStyle.Regular);
    
    
    
            //assign that font to PDFGridCell
    
            e.CellStyle.Font = font;
    
        }
    
    }

    How to change PDF page orientation

    You can change the page orientation of PDF while exporting. The default page orientation is Portrait.

    To change the page orientation, you can get the exported PdfGrid by using ExportToPdfGrid method and then draw that PdfGrid into a PdfDocument by changing the PageSettings.Orientation of PdfDocument.

    The following code example illustrates how to change page orientation to Landscape.

  • c#
  • var options = new PdfExportingOptions();
    
    var document = new PdfDocument();
    
    document.PageSettings.Orientation = PdfPageOrientation.Landscape;
    
    var page = document.Pages.Add();
    
    var pdfGrid = sfDataGrid.ExportToPdfGrid(sfDataGrid.View, options);
    
    var format = new PdfGridLayoutFormat()
    
    {
    
        Layout = PdfLayoutType.Paginate,
    
        Break = PdfLayoutBreakType.FitPage
    
    };
    
    
    
    pdfGrid.Draw(page, new PointF(), format);
    
    
    
    var savePicker = new FileSavePicker
    
    {
    
        SuggestedStartLocation = PickerLocationId.Desktop,
    
        SuggestedFileName = "Sample"
    
    };
    
    
    
    savePicker.FileTypeChoices.Add("Pdf File (.pdf)", new List<string>() { ".pdf" });
    
    var storageFile = await savePicker.PickSaveFileAsync();
    
    await document.SaveAsync(storageFile);

    The following screenshot displays the output for the above code,