New to Telerik UI for WPF? Download free 30-day trial

ExportToPdf

The ExportToPdf method allows exporting to "Pdf" format. As the mechanism uses RadSpreadProcessing internally, there is no need for the user to make the integration manually. The method was introduced in Q1 2015.

Assembly References

The ExportToPdf method uses additional libraries so you need to add references to the following assemblies:

  • Telerik.Windows.Documents.Core.dll
  • Telerik.Windows.Documents.Spreadsheet.dll
  • Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.dll
  • Telerik.Windows.Documents.Fixed.dll
  • Telerik.Windows.Zip.dll
  • Telerik.Windows.Controls.GridView.Export.dll

Telerik.Windows.Controls.GridView.Export.dll is a new binary introduced in Q1 SP of 2015. It delimits the exporting to Pdf functionality from Telerik.Windows.Controls.GridView.dll, so in order to use ExportToPdf method, the new dll should also be added.

Method Overloads

  1. ExportToPdf(Stream stream) - Expects the specified stream to which you are exporting data to.

  2. ExportToPdf (Stream stream, GridViewPdfExportOptions options) - Expects the specified stream to which you are exporting data to and parameter of type GridViewPdfExportOptions. The latter is used to set the following export options:

  • Culture
  • Items
  • ShowColumnFooters
  • ShowGroupFooters
  • ShowColumnHeaders
  • ExportDefaultStyles

The following example shows how to use the method on a button click:

Example 1: Usage of Method ExportToPdf

private void btnExport_Click(object sender, RoutedEventArgs e) 
{ 
    string extension = "pdf"; 
 
    SaveFileDialog dialog = new SaveFileDialog() 
    { 
        DefaultExt = extension, 
        Filter = String.Format("{1} files (.{0})|.{0}|All files (.)|.", extension, "Pdf"), 
        FilterIndex = 1 
    }; 
 
    if (dialog.ShowDialog() == true) 
    { 
        using (Stream stream = dialog.OpenFile()) 
        { 
            gridViewExport.ExportToPdf(stream, 
                new GridViewPdfExportOptions() 
                { 
                    ShowColumnFooters = true, 
                    ShowColumnHeaders = true, 
                    ShowGroupFooters = true, 
              PageOrientation = PageOrientation.Landscape 
                }); 
        } 
    } 
} 

Export Default Styles

To export the default styles of RadGridView in grouped state, at least one row must be expanded, so that the exporting engine can get the styles.

Exporting the Default Styles will take into account the styling applied to the first element of each type(cell, column header, etc.). This is due to performance optimizations. Exporting a separate style for the needed element is discussed in details here:Style Exported XLSX & PDF Documents.

RadGridView can be exported with its default styles by setting the ExportDefaultStyles property to “true”

By default the ExportDefaultStyles property is set to false. You can see the result (Figure 1)

Figure 1: Exporting with ExportDefaultStyles set to “false” (default)

Telerik WPF DataGrid export-default-styles 3

You can set the ExportDefaultStyles value to “true” and see the result (Figure 2)

Example 2: Configuring ExportDefaultStyles property

gridViewExport.ExportToPdf(stream, 
    new GridViewPdfExportOptions() 
    { 
        ShowColumnHeaders = true, 
        ShowColumnFooters = true, 
        ShowGroupFooters = true, 
        ExportDefaultStyles = true 
    });    

Figure 2: Exporting with ExportDefaultStyles set to True

Telerik WPF DataGrid export-default-styles 4

Disable Column Width Auto Fit

GridViewDocumentExportOptions expose the boolean AutoFitColumnsWidth property. Its default value is True, meaning that the column's width will be automatically fit based on its content. To disable this behavior, its value can be set to False.

Example 3: Setting the AutoFitColumnsWidth Property to False

this.gridViewExport.ExportToPdf(stream, 
    new GridViewDocumentExportOptions() 
    { 
        ShowColumnHeaders = true, 
        ShowColumnFooters = true, 
        ShowGroupFooters = true, 
        ExportDefaultStyles = true, 
        AutoFitColumnsWidth = false 
    }); 

Figure 3: Exporting with AutoFitColumnsWidth set to False

Telerik WPF DataGrid autofit-columns-width-Pdf

Disable GroupHeaderRow Aggregates

By default, the Aggregate results of the GroupHeaderRow will be exported. Note, that this is an operation performed on data level. Hiding the GroupHeaderRow Aggregates in the UI through a Style targeting the GroupHeaderRow element does not affect it. In order to disable the exporting of the GroupHeaderRow Aggregates, you can set the ShowGroupHeaderColumnAggregates of the GridViewDocumentExportOptions to False.

Example 4: Setting ShowGroupHeaderRowAggregates

if (dialog.ShowDialog() == true) 
        { 
            using (Stream stream = dialog.OpenFile()) 
            { 
                gridViewExport.ExportToPdf(stream, 
                    new GridViewDocumentExportOptions() 
                    { 
                        ShowColumnFooters = true, 
                        ShowColumnHeaders = true, 
                        ShowGroupFooters = true, 
                        ShowGroupHeaderRowAggregates = false 
                    }); 
            } 
        } 

Events

There are two events related to the exporting of RadGridView with the ExportToPdf() method: ElementExportingToDocument and ElementExportedToDocument. You can find more information regarding them in the Export Events section.

How to

See Also

In this article