We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert Excel chart to image and PDF using C#.NET

Charts are visually appealing and create more impact on intended audiences; they can give the users flexibility to see and visualize results in graphical format to evaluate their current and future data in better way. Similarly, we can convey more meanings by taking picture of charts or saving in PDF format.

Some scenarios demand for conversion of Excel charts to images and PDF format. The rendered picture of chart can be included in online pages, MS Word documents, PDF documents and PowerPoint presentations, etc. Aspose.Cells for .NET supports to convert the Excel Charts to images and PDF formats without requiring any additional tools or applications. Aspose.Cells for .NET allows you to convert charts in Excel spreadsheets into various popular image formats. The supported output image formats includes BMP, EMF, GIF, JPEG, PNG, SVG, TIFF, WMF, etc.

Creating a new Pie Chart and Converting to Image and PDF

In order to provide rendering support, the Chart class has exposed ToImage & ToPdf methods with a variety of overloads to best suit the application requirements. See the following sample code on how to create a Pie chart in a new workbook and render to image and PDF format for your reference:

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its index to WorksheetCollection
Worksheet worksheet = workbook.Worksheets[sheetIndex];

// Setting the sheet name
worksheet.Name = "Products";

// Adding sample values to cells

// Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pie3D, 5, 5, 17, 12);
// Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
// Adding Series Collection (chart data source) to the chart ranging from "B1" cell to "B3"
chart.NSeries.Add("B1:B3", true);
// Setting CategoryData.
chart.NSeries.CategoryData = "A1:A3";

// Setting the DataLabels in the chart
DataLabels datalabels;
for (int i = 0; i < chart.NSeries.Count; i++)
    datalabels = chart.NSeries[i].DataLabels;
    datalabels.ShowPercentage = true;

// Create an instance of ImageOrPrintOptions and set a few properties
ImageOrPrintOptions options = new ImageOrPrintOptions()
   VerticalResolution = 200,
   HorizontalResolution = 200,
   SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
// Convert chart to image with additional settings
chart.ToImage("e:\\test2\\chartPNG_out.png", options);
// Convert chart to PDF

Pie chart image is generated (as shown below)

Rendering Chart in Existing Excel File to Image

Here you can see on how to convert the chart in an existing Excel spreadsheet to image format:

// Instantiating a Workbook object
Workbook workbook = new Workbook(filePath);
//Get the first worksheet.
Worksheet worksheet = workbook.Worksheets[0];

// Accessing the first instance of the existing chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[0];
// Create an instance of ImageOrPrintOptions and set a few properties
ImageOrPrintOptions options = new ImageOrPrintOptions()
    ImageFormat = ImageFormat.Emf,
    TextRenderingHint = TextRenderingHint.ClearTypeGridFit
// Convert chart to image with additional settings
chart.ToImage("chartEmf_out.emf", options);

For more details on converting Excel files to PDF, Sheet to image, rendering chart to image and chart to PDF features, you may follow the document links below:

You may also download the latest demos for your complete reference.