Available for: UI for WPF

Export Chart to PDF

You can export spreadsheet documents in RadSpreadProcessing using the PdfFormatProvider class. The charts functionality relies on a renderer which implements the IPdfChartRenderer interface to draw the charts in the PDF document. This topic shows how you can implement this renderer using the built-in class in WPF.

To use the functionality described in this topic, you will need to add a reference to the Telerik.Windows.Controls.Spreadsheet.dll and Telerik.Windows.Controls.Chart.dll assemblies of the Telerik UI for WPF suite.

IPdfChartRenderer

The IPdfChartRenderer interface defines members for classes which will be used by the internal logic of PdfFormatProvider to render the chart objects in a spreadsheet document when exporting it to PDF. The interface defines the RenderChart() method which should be implemented so it can be called when the internal logic of the PdfFormatProvider reaches a chart which has to be rendered.

ChartModelToImageConverter Class

The ChartModelToImageConverter object is readily available in the Telerik.Windows.Controls.Spreadsheet assembly and uses internally the RadChartView control to visualize the chart and create an image. This class exposes the GetBitmapSourceFromChartModel() method which converts a chart object to an image and can be used to draw the image in the PDF document.

Implementing Export to PDF in Your Application

The PdfFormatProvider instance accepts a renderer in its settings. The renderer needs to implement the IPdfChartRenderer interface and the RenderChart() method this interface defines. The method takes a FixedContentEditor in its parameters, which will draw the chart, and the other parameters contain the information necessary to draw it. The WpfPdfChartImageRenderer implemented in Example 1 is an example implementation which uses the Telerik.Windows.Controls.Spreadsheet and Telerik.Windows.Controls.Chart assemblies to draw the chart.

Example 1: Implementing a renderer

public class WpfPdfChartImageRenderer : IPdfChartRenderer 
{ 
    private readonly ChartModelToImageConverter chartToImageConverter; 
 
    public WpfPdfChartImageRenderer() 
    { 
        this.chartToImageConverter = new ChartModelToImageConverter(); 
    } 
 
    // This is the method which will be called when the internal logic of the PdfFormatProvider reaches a chart which has to be rendered. 
    public void RenderChart(FixedContentEditor editor, DocumentChart chart, DocumentTheme theme, Size chartSize) 
    { 
        // The ChartModelToImageConverter object is readily available in the Telerik.Windows.Controls.Spreadsheet assembly and 
        // uses internally the RadChartView control to visualize the chart and create an image. 
        BitmapSource source = this.chartToImageConverter.GetBitmapSourceFromChartModel(chart, theme, chartSize); 
 
        // The editor draws the image in the PDF. 
        editor.DrawImage(this.StreamFromBitmapSource(source)); 
    } 
 
    public Stream StreamFromBitmapSource(BitmapSource writeBmp) 
    { 
        Stream bmp = new MemoryStream(); 
 
        BitmapEncoder enc = new BmpBitmapEncoder(); 
        enc.Frames.Add(BitmapFrame.Create(writeBmp)); 
        enc.Save(bmp); 
 
        return bmp; 
    } 
} 
When you have the renderer implemented, you will need to assign it to the PdfFormatProvider instance through the ChartRenderer property of its ExportSettings.

Example 2: Registering the renderer

PdfFormatProvider pdfFormatProvider = new PdfFormatProvider(); 
pdfFormatProvider.ExportSettings.ChartRenderer = new WpfPdfChartImageRenderer(); 
Now the chart objects in the spreadsheet will be exported along with the other content after invoking the Export() method of PdfFormatProvider.

You can find a complete runnable example using the approach described in this topic in our SDK repository: Export Chart.

See Also

In this article
Not finding the help you need? Improve this article