Skip to content

Commit 997bea7

Browse files
Merge pull request #454 from telerik/new-kb-export-charts-to-pdf-radspreadprocessing-146b0a5455c5441fb2950ff48c29c6cc
Added new kb article export-charts-to-pdf-radspreadprocessing
2 parents 123d88e + 91d4c8b commit 997bea7

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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, sheet, worksheet, xslx
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 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 how to utilize the chart engine that [WinForms RadChartView]({%slug winforms/chartview/overview%}) control offers and ensure charts are properly 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]({%slug radspreadprocessing-features-charts-pdf-export%}) 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]({%slug ({%slug radspreadsheet-overview%})%}) control (that supports charts) internally uses the WinForms RadChartView for visualization and it provides a convenient API for image creation having the [FloatingChartShape]({%slug radspreadprocessing-features-charts-using-charts%}) from the SpreadProcessing model.
29+
30+
3. **Export the Excel to PDF**: Use the [PdfFormatProvider]({%slug radspreadprocessing-formats-and-conversion-pdf-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+
using System.IO;
36+
using Telerik.Windows.Documents.Fixed.Model.Editing;
37+
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.Export;
38+
using Telerik.Windows.Documents.Spreadsheet.Model.Shapes;
39+
40+
public class WinFormsPdfChartImageRenderer : IPdfChartRenderer
41+
{
42+
public WinFormsPdfChartImageRenderer()
43+
{
44+
}
45+
46+
public void RenderChart(FixedContentEditor editor, FloatingChartShape chartShape)
47+
{
48+
string filePath = @"exportedChart.png";
49+
50+
System.Drawing.Size size = new System.Drawing.Size((int)(chartShape.Width), (int)(chartShape.Height + 10));
51+
System.Drawing.Image chartImage = Telerik.WinForms.Controls.Spreadsheet.Layers.ChartModelToImageConverter.GetImageFromFloatingChartShape(chartShape, size);
52+
53+
chartImage.Save(filePath);
54+
using (FileStream fs = new FileStream(filePath, FileMode.Open))
55+
{
56+
editor.DrawImage(fs);
57+
}
58+
File.Delete(filePath);
59+
}
60+
}
61+
```
62+
63+
Using the custom chart renderer:
64+
65+
```csharp
66+
Workbook workbook;
67+
IWorkbookFormatProvider xlsxFormatProvider = new XlsxFormatProvider();
68+
69+
using (Stream input = new FileStream("spreadsheetWithChart.xlsx", FileMode.Open))
70+
{
71+
workbook = xlsxFormatProvider.Import(input);
72+
}
73+
74+
WorksheetPageSetup pageSetup = workbook.ActiveWorksheet.WorksheetPageSetup;
75+
pageSetup.PaperType = PaperTypes.A4;
76+
77+
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
78+
pdfFormatProvider.ExportSettings.ChartRenderer = new WinFormsPdfChartImageRenderer(); // new WpfPdfChartImageRenderer();
79+
80+
string outputFilePath = "spreadsheetWithChart.pdf";
81+
File.Delete(outputFilePath);
82+
using (Stream output = File.OpenWrite(outputFilePath))
83+
{
84+
pdfFormatProvider.Export(workbook, output);
85+
}
86+
87+
var psi = new ProcessStartInfo()
88+
{
89+
FileName = outputFilePath,
90+
UseShellExecute = true
91+
};
92+
Process.Start(psi);
93+
```
94+
95+
### Required Assemblies
96+
97+
* Telerik.WinControls.RadSpreadsheet
98+
* Telerik.WinControls
99+
* Telerik.WinControls.ChartView
100+
* Telerik.WinControls.UI
101+
* TelerikCommon
102+
* Telerik.Windows.Documents.Core
103+
* Telerik.Windows.Documents.Fixed
104+
* Telerik.Windows.Documents.Spreadsheet
105+
* Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml
106+
* Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf
107+
108+
## See Also
109+
110+
- [RadSpreadProcessing Overview]({%slug radspreadprocessing-overview%})
111+
- [Export Chart to PDF]({%slug radspreadprocessing-features-charts-pdf-export%})
112+
- [RadChartView for WinForms Overview]({%slug winforms/chartview/overview%})
113+
- [Export Chart to Image in WinForms]({%slug winforms/chartview-/features/export%})
114+
- [WinForms RadSpreadsheet]({%slug radspreadsheet-overview%})

libraries/radspreadprocessing/features/charts/pdf-export.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,4 @@ Now the chart objects in the spreadsheet will be exported along with the other c
8282
## See Also
8383

8484
* [Using PdfFormatProvider]({%slug radspreadprocessing-formats-and-conversion-pdf-pdfformatprovider%})
85+
* [Exporting Spreadsheets with Charts to PDF with RadSpreadProcessing and WinForms RadChartView]({%slug export-charts-to-pdf-radspreadprocessing%})

libraries/radspreadprocessing/formats-and-conversion/pdf/format-and-conversion-settings.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ __PdfExportSettings__ allow controlling how a __Workbook__ is exported to PDF. U
3030

3131
* __PdfFileSettings__: A property of type __Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.PdfExportSettings__ which is a class related to the [RadPdfProcessing library]({%slug radpdfprocessing-overview%}). Thus, the property allows you to control the image quality, encryption, compliance level and other PDF format related properties. More information on the settings is available in the [export settings article for RadPdfProcessing]({%slug radpdfprocessing-formats-and-conversion-pdf-settings%}).
3232

33+
* **ChartRenderer**: A property of type [IPdfChartRenderer](https://docs.telerik.com/devtools/document-processing/api/telerik.windows.documents.spreadsheet.formatproviders.pdf.export.ipdfchartrenderer) that gets or sets the renderer which will be used to render charts. It is not relevant for the .NET Standard version of the Telerik Document Processing libraries. Read more in the [Export Chart to PDF]({%slug radspreadprocessing-features-charts-pdf-export%}) article.
34+
3335

3436
__Example 1__ shows how to export the Entire Workbook without ignoring the __PrintArea__ property in all worksheets.
3537

0 commit comments

Comments
 (0)