# Exporting Spreadsheets with Charts to PDF with RadSpreadProcessing and WinForms RadChartView

## Environment

| Version    | Product             | Author                                                                          |
| ---------- | ------------------- | ------------------------------------------------------------------------------- |
| 2024.2.426 | RadSpreadProcessing | [Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova) |

## Description

When you convert an Excel file with charts to PDF format using the Telerik Document Processing libraries, the charts may not display as expected in the PDF document. This may occur when exporting charts to PDF in .NET Framework applications. This KB article shows a sample approach for using the chart engine that the [WinForms RadChartView](https://docs.telerik.com/devtools/winforms/controls/chartview/overview) control offers to ensure charts are properly exported from Excel files to PDF.

## Solution

To export charts from Excel files to PDF format using RadSpreadProcessing, follow these steps:

1. **Implement a Custom IPdfChartRenderer**: The export process requires an [IPdfChartRenderer](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/features/charts/pdf-export) implementation. This renderer converts the chart shapes from the Excel file into images that can be inserted into the PDF.

2. **Use the WinForms RadChartView control**: The [WinForms RadSpreadsheet](https://docs.telerik.com/devtools/winforms/controls/spreadsheet/overview) control (that supports charts) internally uses the WinForms RadChartView for visualization. It provides a convenient API for image creation from the [FloatingChartShape](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/features/charts/using-charts) in the SpreadProcessing model.

3. **Export the Excel to PDF**: Use the [PdfFormatProvider](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/formats-and-conversion/pdf/pdfformatprovider) to export the workbook to PDF. Set the `ChartRenderer` property to your custom renderer implementation.

The following is a sample implementation using the WinForms RadChartView for chart visualization:

```csharp
using System.IO;
using Telerik.Windows.Documents.Fixed.Model.Editing;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.Export;
using Telerik.Windows.Documents.Spreadsheet.Model.Shapes;

public class WinFormsPdfChartImageRenderer : IPdfChartRenderer
{
    public WinFormsPdfChartImageRenderer()
    {
    }

    public void RenderChart(FixedContentEditor editor, FloatingChartShape chartShape)
    {
        string filePath = @"exportedChart.png";

        System.Drawing.Size size = new System.Drawing.Size((int)(chartShape.Width), (int)(chartShape.Height + 10));
        System.Drawing.Image chartImage = Telerik.WinForms.Controls.Spreadsheet.Layers.ChartModelToImageConverter.GetImageFromFloatingChartShape(chartShape, size);

        chartImage.Save(filePath);
        using (FileStream fs = new FileStream(filePath, FileMode.Open))
        {
            editor.DrawImage(fs);
        }
        File.Delete(filePath);
    }
}
```

Use the custom chart renderer:

```csharp
            Workbook workbook;
            IWorkbookFormatProvider xlsxFormatProvider = new XlsxFormatProvider();

            using (Stream input = new FileStream("spreadsheetWithChart.xlsx", FileMode.Open))
            {
                workbook = xlsxFormatProvider.Import(input);
            }
            
            WorksheetPageSetup pageSetup = workbook.ActiveWorksheet.WorksheetPageSetup;
            pageSetup.PaperType = PaperTypes.A4;

            PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
            pdfFormatProvider.ExportSettings.ChartRenderer = new WinFormsPdfChartImageRenderer(); // new WpfPdfChartImageRenderer();

            string outputFilePath = "spreadsheetWithChart.pdf";
            File.Delete(outputFilePath);
            using (Stream output = File.OpenWrite(outputFilePath))
            {
                pdfFormatProvider.Export(workbook, output);
            }

            var psi = new ProcessStartInfo()
            {
                FileName = outputFilePath,
                UseShellExecute = true
            };
            Process.Start(psi);    
```

### Required Assemblies

* Telerik.WinControls.RadSpreadsheet
* Telerik.WinControls
* Telerik.WinControls.ChartView
* Telerik.WinControls.UI
* TelerikCommon
* Telerik.Windows.Documents.Core
* Telerik.Windows.Documents.Fixed
* Telerik.Windows.Documents.Spreadsheet
* Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml
* Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf

## See Also

* [RadSpreadProcessing Overview](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/overview)
* [Export charts to PDF](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/features/charts/pdf-export)
* [RadChartView for WinForms Overview](https://docs.telerik.com/devtools/winforms/controls/chartview/overview)
* [Export charts to images in WinForms](https://docs.telerik.com/devtools/winforms/controls/chartview/features/export)
* [WinForms RadSpreadsheet](https://docs.telerik.com/devtools/winforms/controls/spreadsheet/overview)
