Exporting Spreadsheets with Charts to PDF with RadSpreadProcessing and WinForms RadChartView
Environment
| Version | Product | Author |
|---|---|---|
| 2024.2.426 | RadSpreadProcessing | 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 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:
-
Implement a Custom IPdfChartRenderer: The export process requires an IPdfChartRenderer implementation. This renderer converts the chart shapes from the Excel file into images that can be inserted into the PDF.
-
Use the WinForms RadChartView control: The WinForms RadSpreadsheet control (that supports charts) internally uses the WinForms RadChartView for visualization. It provides a convenient API for image creation from the FloatingChartShape in the SpreadProcessing model.
-
Export the Excel to PDF: Use the PdfFormatProvider to export the workbook to PDF. Set the
ChartRendererproperty to your custom renderer implementation.
The following is a sample implementation using the WinForms RadChartView for chart visualization:
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:
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