This quick tutorial provides information about how to convert Excel chart to PDF in C# where Excel files can be an XLSX, XLS, ODS or in any other format supported by MS Excel. It guides by providing instructions to establish the environment, a step-by-step procedure to understand the process, and a running sample code. You can export Excel chart to PDF in C# in any of the operating systems like Windows, Linux, or macOS without installing any other third-party tool.
Steps to Transform Excel Chart to PDF in C#
- Configure the project to add Aspose.Cells from Nuget package manager to convert chart to PDF
- Load the source Excel file into the Workbook object having a few charts in it
- Get a reference to the selected worksheet with charts
- Get the reference to the desired chart from the charts collection
- Call the ToPdf function from the Chart class to convert the chart to PDF
- Set output PDF page size and chart alignment in it
- Save the PDF on disk or to a stream
These steps provide a complete guide to convert Excel graph to PDF in C#. Instructions set include configuration of the project, loading the target workbook, selecting the required chart from a specific sheet, and then saving it as a PDF. It also assists in configuring the chart in the output PDF.
Code to Render Excel chart to PDF in C#
This code uses Chart.ToPdf() function that has multiple overloads. If you do not want to configure the chart in the output PDF, then just provide the output PDF file name in the Chart.ToPdf() function. However during the process to render Excel chart to PDF in C# if you want to set the output PDF page size and horizontal/vertical alignment of the chart, use the relevant overload that takes the size in inches and provides an appropriate enumerator to set the alignment.
This tutorial has elaborated the process to create PDF from an Excel chart. If you are interested in converting the Excel chart to an image, refer to the article on how to convert Excel to image in C#.