|
| 1 | +--- |
| 2 | +title: Exporting Spreadsheets with Charts to PDF with RadSpreadProcessing and WinForms RadChartView |
| 3 | +description: Learn how to export charts from Excel files to PDF format using RadSpreadProcessing, including handling chart images with the WinForms ChartView control. |
| 4 | +type: how-to |
| 5 | +page_title: How to Export Charts from Excel to PDF with RadSpreadProcessing and WinForms RadChartView |
| 6 | +slug: export-charts-to-pdf-radspreadprocessing |
| 7 | +tags: spreadprocessing, document, processing, export, chart, pdf, excel, image, winforms |
| 8 | +res_type: kb |
| 9 | +ticketid: 1659898 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.2.426| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +When converting an Excel file with charts to PDF, 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 how to ensure charts are correctly exported from Excel files to PDF. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To export charts from Excel files to PDF format using RadSpreadProcessing, follow these steps: |
| 25 | + |
| 26 | +1. **Implement a Custom IPdfChartRenderer**: The export process requires providing an `IPdfChartRenderer` implementation. This renderer is responsible for converting the chart shapes from the Excel file into images that can be inserted into the PDF. |
| 27 | + |
| 28 | +2. **Use the WinForms RadChartView control**: The WinForms RadSpreadsheet control (that supports charts) internally uses the WinForms RadChartView for visualization and it provides a convenient API for image creation having the FloatingChartShape from the SpreadProcessing model. |
| 29 | + |
| 30 | +3. **Export the Excel to PDF**: Use the `PdfFormatProvider` to export the workbook to PDF, setting the `ChartRenderer` property to your custom renderer implementation. |
| 31 | + |
| 32 | +Below is a sample implementation using the WinForms RadChartView for chart visualization: |
| 33 | + |
| 34 | +```csharp |
| 35 | +public class WinFormsPdfChartImageRenderer : IPdfChartRenderer |
| 36 | +{ |
| 37 | + public void RenderChart(FixedContentEditor editor, FloatingChartShape chartShape) |
| 38 | + { |
| 39 | + string filePath = @"exportedChart.png"; |
| 40 | + |
| 41 | + System.Drawing.Size size = new System.Drawing.Size((int)(chartShape.Width ),(int)(chartShape.Height)); |
| 42 | + System.Drawing.Image chartImage = Telerik.WinForms.Controls.Spreadsheet.Layers.ChartModelToImageConverter.GetImageFromFloatingChartShape(chartShape, size); |
| 43 | + |
| 44 | + chartImage.Save(filePath); |
| 45 | + using (FileStream fs = new FileStream(filePath, FileMode.Open)) |
| 46 | + { |
| 47 | + editor.DrawImage(fs); |
| 48 | + } |
| 49 | + File.Delete(filePath); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +PdfFormatProvider pdfFormatProvider = new PdfFormatProvider(); |
| 54 | +pdfFormatProvider.ExportSettings.ChartRenderer = new WinFormsPdfChartImageRenderer(); |
| 55 | +``` |
| 56 | + |
| 57 | +## See Also |
| 58 | + |
| 59 | +- [RadSpreadProcessing Overview]({%slug radspreadprocessing-overview%}) |
| 60 | +- [Export Chart to PDF]({%slug radspreadprocessing-features-charts-pdf-export%}) |
| 61 | +- [RadChartView for WinForms Overview]({%slug winforms/chartview/overview%}) |
| 62 | +- [Export Chart to Image in WinForms]({%slug winforms/chartview-/features/export%}) |
| 63 | +- [WinForms Spreadsheet]({%slug radspreadsheet-overview%}) |
0 commit comments