Create Charts in Word Documents using C#

create-charts-in-word-documents-using-csharp

As a C# developer, you can insert various types of charts in Word documents programmatically. It helps to graphically present your data and information. In this article, you will learn how to create charts in Word documents using C#.

The following topics are discussed/covered in this article:

C# API to Insert Charts in Word Documents

For inserting charts in DOCX files, we will be using Aspose.Words for .NET API. It allows you to generate, modify, convert, render and print files without utilizing Microsoft Word directly within cross-platform applications. The API enables you to insert various supported chart types in Word documents programmatically.

You can either download the DLL of the API or install it using NuGet.

Install-Package Aspose.Words

Create Columns Charts in Word Documents

You can create columns charts in Word documents programmatically by following the steps given below:

  • Firstly, create a new document using the Document class.
  • Now, create an instance of the DocumentBuilder class with the Document class object.
  • Then, call the DocumentBuilder.InsertChart() method. Pass ChartType as Column, with height and width as input parameters.
  • Get results in the Shape class object.
  • Now, create an instance of the Chart class and assign Shape.Chart object to it. It provides access to the chart properties if this shape has a Chart.
  • Then, get chart series collection in ChartSeriesCollection object.
  • Create category names array.
  • Now, call the ChartSeriesCollection.Add() method to add chart series. Pass name, category array and values as input parameters. Repeat this step to add more series.
  • Finally, call the Document.Save() method with output file path to save the file.

The following code sample shows how to create a columns chart in a Word document using C#.

Create Columns Charts in Word Documents using C#.
Create Columns Charts in Word Documents using C#

Create Scatter Charts in Word Documents using C#

You can insert scatter charts in your Word documents programmatically by following the steps mentioned earlier. However, you need to set ChartType.Scatter in DocumentBuilder.InsertChart() method.

The following code sample shows how to create a scatter chart in a Word document using C#.

Create Scatter Charts in Word Documents using C#.
Create Scatter Charts in Word Documents using C#

Insert Area Charts in Word Documents using C#

You can insert area charts in Word documents programmatically by following the steps mentioned earlier. However, you need to set ChartType.Area in DocumentBuilder.InsertChart() method.

The following code sample shows how to create an area chart in a Word document using C#.

Insert Area Charts in Word Documents using C#.
Insert Area Charts in Word Documents using C#

Insert Bubble Charts in Word Documents using C#

You can insert bubble charts in Word documents programmatically by following the steps mentioned earlier. However, you need to set ChartType.Bubble in DocumentBuilder.InsertChart() method.

The following code sample shows how to create a bubble chart in a Word document using C#.

Insert Bubble Charts in Word Documents using C#.
Insert Bubble Charts in Word Documents using C#

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to create charts in Word documents using C#. Particularly, you have learned how to create column, area, bubble, and scatter charts in Word documents programmatically. Similarly, you can create other types of charts. You can learn more about Aspose.Words for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also