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

Export to PDF

RadPivotGrid can export its contents to PDF. This is achieved with the help of the RadPdfProcessing library.

Figure 1: RadPivotGrid to PDF

WinForms RadPivotGrid RadPivotGrid to PDF

The PDF export functionality is located in the TelerikExport.dll assembly and to use the functionality you need yo add reference to it.

Execute the Exporter

Before exporting to PDF, you have to initialize the PivotGridPdfExport class. The constructor takes one parameter - RadPivotGrid which will be exported. You need to create PdfExportRenderer instance as well:

PivotGridPdfExport Class

PivotGridPdfExport exporter = new PivotGridPdfExport(radPivotGrid1);
PdfExportRenderer renderer = new PdfExportRenderer();
exporter.RunExport(@"C:\PivotData.pdf", renderer);

Dim exporter As New PivotGridPdfExport(radPivotGrid1)
Dim renderer As New PdfExportRenderer()
exporter.RunExport("C:\PivotData.pdf", renderer)

The RunExport method has several overloads allowing the user to export using a stream as well:

Running Export Synchronously Using a Stream

string exportFile = @"..\..\exportedData.pdf";
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
    Telerik.WinControls.Export.PivotGridPdfExport pdfExporter = new Telerik.WinControls.Export.PivotGridPdfExport(radPivotGrid1);
    Telerik.WinControls.Export.PdfExportRenderer pdfRenderer = new Telerik.WinControls.Export.PdfExportRenderer();
    pdfExporter.RunExport(ms, pdfRenderer);
    using (System.IO.FileStream fileStream = new System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write))
    {
        ms.WriteTo(fileStream);
    }
}

Dim exportFile As String = "..\..\exportedData.pdf"
Using ms As New System.IO.MemoryStream()
    Dim pdfExporter As New Telerik.WinControls.Export.PivotGridPdfExport(radPivotGrid1)
    Dim pdfRenderer As New Telerik.WinControls.Export.PdfExportRenderer()
    pdfExporter.RunExport(ms, pdfRenderer)
    Using fileStream As New System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write)
        ms.WriteTo(fileStream)
    End Using
End Using

Properties

  • ExportVisualSettings: Gets or sets a value indicating whether the visual settings should be exported.

  • ExportSelectionOnly: Gets or sets a value indicating whether to export only selection.

  • ExportFlatData: Gets or sets a value which indicates whether to export flat data (collapsed rows and columns).

  • PageSize: Gets or sets the page size in millimeters.

  • PageMargins: Gets or sets the margins of pages in millimeters.

  • FitToPageWidth: Gets or sets a value indicating whether the content of page should fit into the page width.

  • Scale: Gets or sets the document scaling. Default value is 1. For example, scale of 1.2f means 20% size increase.

  • ShowHeaderAndFooter: Gets or sets a value indicating whether header and footer should be exported.

  • ExportSettings: This property allows you to set the document information.

The following properties define color and font of the different cell elements: CellBackColor, HeadersBackColor, DescriptorsBackColor, SubTotalsBackColor, GrandTotalsBackColor, BorderColor, HeaderCellsFont, DataCellsFont

Events

The PivotGridPdfExport exposes tree events that allows you to change the exported document.

  • CellFormatting: This event is fired for every exported cell. It allows you to change the cells properties including its value.

  • CellPaint: Occurs after a cell is drawn. This event allows you to draw additional elements to the cell.

  • PdfExported: Occurs when the export process completes.

The PdfExportRenderer is exposing two events as well. Detailed information is available here: PdfExportRenderer

Asynchronous Exporting

The PivotGrid can be exported asynchronously as well. The following example shows how you can run the exporter asynchronously.

Running Export Asynchronously


PivotGridPdfExport exporter = new PivotGridPdfExport(radPivotGrid1);
PdfExportRenderer renderer = new PdfExportRenderer();
exporter.RunExportAsync(@"C:\PivotData.pdf", renderer);

Dim exporter As New PivotGridPdfExport(radPivotGrid1)
Dim renderer As New PdfExportRenderer()
exporter.RunExportAsync("C:\PivotData.pdf", renderer)

The RunExportAsync method has several overloads allowing the user to export using a stream as well:

Running Export Asynchronously Overloads


private void buttonRunExportAsync_Click(object sender, EventArgs e)
{
    System.IO.MemoryStream ms = new System.IO.MemoryStream();         
    Telerik.WinControls.Export.PivotGridPdfExport exporter = new Telerik.WinControls.Export.PivotGridPdfExport(radPivotGrid1);
    Telerik.WinControls.Export.PdfExportRenderer renderer = new Telerik.WinControls.Export.PdfExportRenderer();
    exporter.AsyncExportCompleted += exporter_AsyncExportCompleted;
    exporter.RunExportAsync(ms, renderer);
}

private void exporter_AsyncExportCompleted(object sender, AsyncCompletedEventArgs e)
{
    RunWorkerCompletedEventArgs args = e as RunWorkerCompletedEventArgs;
    string exportFile = @"..\..\exportedAsyncData.pdf";
    using (System.IO.FileStream fileStream = new System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write))
    { 
        MemoryStream ms = args.Result as MemoryStream;
        ms.WriteTo(fileStream);
        ms.Close();
    }
}

Private Sub buttonRunExportAsync_Click(sender As Object, e As EventArgs)
    Dim ms As New System.IO.MemoryStream()
    Dim exporter As New Telerik.WinControls.Export.PivotGridPdfExport(radPivotGrid1)
    Dim renderer As New Telerik.WinControls.Export.PdfExportRenderer()
    AddHandler exporter.AsyncExportCompleted, AddressOf exporter_AsyncExportCompleted
    exporter.RunExportAsync(ms, renderer)
End Sub
Private Sub exporter_AsyncExportCompleted(sender As Object, e As AsyncCompletedEventArgs)
    Dim args As RunWorkerCompletedEventArgs = TryCast(e, RunWorkerCompletedEventArgs)
    Dim exportFile As String = "..\..\exportedAsyncData.pdf"
    Using fileStream As New System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write)
        Dim ms As MemoryStream = TryCast(args.Result, MemoryStream)
        ms.WriteTo(fileStream)
        ms.Close()
    End Using
End Sub

There are two events that can be used with the asynchronous export:

  • AsyncExportCompleted: Occurs when an asynchronous export operation is completed.

  • AsyncExportProgressChanged: Occurs when the progress of an asynchronous export operation changes.

See Also

In this article