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
- Create Columns Charts in Word Documents
- Create Scatter Charts in Word Documents using C#
- Insert Area Charts in Word Documents using C#
- Insert Bubble Charts in Word Documents using C#
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 a document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Add chart with default data. You can specify different chart types and sizes. | |
Shape shape = builder.InsertChart(ChartType.Column, 432, 252); | |
// Chart property of Shape contains all chart related options. | |
Chart chart = shape.Chart; | |
// Get chart series collection. | |
ChartSeriesCollection seriesColl = chart.Series; | |
// Check series count. | |
Console.WriteLine(seriesColl.Count); | |
// Delete default generated series. | |
seriesColl.Clear(); | |
// Create category names array, in this example we have two categories. | |
string[] categories = new string[] { "AW Category 1", "AW Category 2" }; | |
// Adding new series. Please note, data arrays must not be empty and arrays must be the same size. | |
seriesColl.Add("AW Series 1", categories, new double[] { 1, 2 }); | |
seriesColl.Add("AW Series 2", categories, new double[] { 3, 4 }); | |
seriesColl.Add("AW Series 3", categories, new double[] { 5, 6 }); | |
seriesColl.Add("AW Series 4", categories, new double[] { 7, 8 }); | |
seriesColl.Add("AW Series 5", categories, new double[] { 9, 10 }); | |
// Save the document | |
doc.Save(@"C:\Files\Words\ColumnsChart.docx"); |
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 a new document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Scatter chart. | |
Shape shape = builder.InsertChart(ChartType.Scatter, 432, 252); | |
Chart chart = shape.Chart; | |
// Use this overload to add series to any type of Scatter charts. | |
chart.Series.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 }); | |
// Save the document | |
doc.Save(@"C:\Files\Words\ScatterChart.docx"); |
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#.
// Create a new document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Area chart. | |
Shape shape = builder.InsertChart(ChartType.Area, 432, 252); | |
Chart chart = shape.Chart; | |
// Use this overload to add series to any type of Area, Radar and Stock charts. | |
chart.Series.Add("AW Series 1", new DateTime[] { | |
new DateTime(2002, 05, 01), | |
new DateTime(2002, 06, 01), | |
new DateTime(2002, 07, 01), | |
new DateTime(2002, 08, 01), | |
new DateTime(2002, 09, 01)}, | |
new double[] { 32, 32, 28, 12, 15 }); | |
// Save the document | |
doc.Save(@"C:\Files\Words\AreaChart.docx"); |
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#.
// Create a new document | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert Bubble chart. | |
Shape shape = builder.InsertChart(ChartType.Bubble, 432, 252); | |
Chart chart = shape.Chart; | |
// Use this overload to add series to any type of Bubble charts. | |
chart.Series.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 }, new double[] { 10, 4, 8 }); | |
// Save the document | |
doc.Save(@"C:\Files\Words\BubbleChart.docx"); |
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.