Embedding visual data directly into Word files can turn static reports into compelling presentations. Conholdate.Total for Java provides a robust SDK that simplifies chart creation and insertion in DOCX documents. In this guide we will walk you through a complete, compilable example that shows how to create Charts in Word documents using Java, configure the chart, and save the result. By the end you’ll be able to automate document generation with dynamic charts for any business reporting scenario.

Full Working Example for Embedding Charts in Word Documents Using Java

This example demonstrates how to build a column chart with five data series and insert it into a Word document.

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Shape;
import com.aspose.words.ChartType;
import com.aspose.words.Chart;
import com.aspose.words.ChartSeriesCollection;

public class CreateChartInWord {
    public static void main(String[] args) {
        // 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.getChart();

        // Get chart series collection.
        ChartSeriesCollection seriesColl = chart.getSeries();
        // Check series count.
        System.out.println(seriesColl.getCount());

        // 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("ColumnsChart.docx");
    }
}

Note: This code example demonstrates the core functionality. Before using it in your project, make sure the output path (ColumnsChart.docx) points at a writable directory, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

How Create Charts in Word Documents Using Java Works

The workflow can be broken down into five clear steps:

  1. Create a Document and DocumentBuilder - The Document class represents the Word file in memory, and DocumentBuilder gives you a fluent API for inserting content, including charts.

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    
  2. Insert a chart shape - insertChart takes a ChartType (e.g., COLUMN) along with a width and height in points, and returns a Shape that hosts the chart on the page.

    Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252);
    
  3. Access the Chart object - The Chart property of the Shape exposes all chart-related options, including its series collection.

    Chart chart = shape.getChart();
    ChartSeriesCollection seriesColl = chart.getSeries();
    System.out.println(seriesColl.getCount());
    

    See the API reference for full details on Chart, ChartSeriesCollection, and related enums.

  4. Clear the default series and add your own - A newly inserted chart already contains placeholder series, so clear() removes them before you add categories and data with add(). Category and value arrays passed to the same add() call must be the same length.

    seriesColl.clear();
    
    String[] categories = new String[] { "AW Category 1", "AW Category 2" };
    
    seriesColl.add("AW Series 1", categories, new double[] { 1, 2 });
    seriesColl.add("AW Series 2", categories, new double[] { 3, 4 });
    
  5. Save the document - Finally, doc.save() writes the DOCX to disk.

    doc.save("ColumnsChart.docx");
    

Understanding each of these steps makes it easy to adapt the example for different chart types, data sources, or document templates.

Getting the Environment Ready

Add the Conholdate Maven repository and the SDK dependency to your pom.xml:

<repositories>
    <repository>
        <id>conholdate-repo</id>
        <name>Conholdate Maven Repository</name>
        <url>https://repository.conholdate.com/repo/</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.conholdate</groupId>
    <artifactId>conholdate-total</artifactId>
    <version>24.9</version>
    <type>pom</type>
</dependency>

Download the latest SDK package from the download page. The library requires Java 8 or higher and runs on any standard JVM. No additional server components are needed.

Fine-Tuning Chart Generation

You can adjust several properties to match your visual style:

  • Chart Type - Change ChartType.COLUMN to ChartType.BAR, ChartType.LINE, ChartType.PIE, ChartType.SCATTER, and other supported types.

    Shape shape = builder.insertChart(ChartType.BAR, 432, 252);
    
  • Chart Size - The second and third arguments to insertChart control the width and height of the chart shape, in points.

    Shape shape = builder.insertChart(ChartType.COLUMN, 500, 300);
    
  • Categories (axis labels) - Build a String[] of category names and reuse it across every series that shares the same categories.

    String[] categories = new String[] { "Jan", "Feb", "Mar" };
    
  • Data Series - Call seriesColl.add() with a series name, the categories array, and a double[] of values to add as many comparative series as you need.

    seriesColl.add("2021", categories, new double[] { 200, 220, 250 });
    

These options let you tailor the chart to any reporting requirement while keeping the code concise.

Inserting a Scatter Chart in a Word Document Using Java

Scatter charts plot pairs of numeric X and Y values rather than categories, which makes them useful for visualizing correlations or distributions. ChartSeriesCollection exposes an overload of add() specifically for this purpose: instead of a categories array, you pass two double[] arrays, one for the X values and one for the Y values.

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Shape;
import com.aspose.words.ChartType;
import com.aspose.words.Chart;

public class InsertScatterChart {
    public static void main(String[] args) {
        // 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.getChart();

        // Use this overload to add series to any type of Scatter charts.
        chart.getSeries().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("ScatterChart.docx");
    }
}

The overall flow mirrors the column chart example: create a Document and DocumentBuilder, insert the chart shape with ChartType.SCATTER, get the Chart from the shape, and add one or more series. Because scatter charts plot X/Y coordinate pairs, there is no separate categories array — each point is defined by matching entries in the X and Y value arrays. Save with doc.save() once all series have been added.

Conclusion

Embedding visual data directly into DOCX files is a powerful way to enhance automated reports. With Conholdate.Total for Java you can create Charts in Word documents using Java in just a few lines of code — including column, bar, line, pie, and scatter charts — customize categories and series, and generate professional‑looking documents on the server side. Remember to obtain a proper license for production use; a temporary license is available on the temporary license page, and full pricing details can be reviewed on the pricing page. Start integrating chart generation today and give your users data‑driven documents that stand out.

FAQs

  • How can I create Charts in Word Documents using Java with Conholdate.Total? Call insertChart on a DocumentBuilder with the desired ChartType, retrieve the Chart from the returned Shape, clear the default series, and add your own categories and data with seriesColl.add(), then save the document. The full code is shown in the example above.

  • What chart types are supported? The SDK supports column, bar, line, pie, scatter, and many other standard chart types through the ChartType enum. Change the enum value passed to insertChart to switch types.

  • How is a scatter chart different from a column or bar chart? A scatter chart plots pairs of numeric X and Y values instead of named categories, so you use the add(name, xValues, yValues) overload of ChartSeriesCollection rather than passing a categories array.

  • Do I need to set a license for chart generation? Yes. A temporary license can be obtained from the temporary license page. For long‑term projects, purchase a full license via the pricing page.

  • Is the SDK compatible with Maven and Gradle builds? Absolutely. Add the Conholdate Maven repository and the conholdate-total dependency to your build file as shown in the Setup section, and the library works with both Maven and Gradle.

Read More