Creating a Visio diagram directly from Java code can streamline documentation workflows and eliminate manual drawing effort. Conholdate.Total for Java provides a robust SDK that enables you to generate Visio files programmatically. In this guide we will create a Visio Diagram in Java, add a shape from a master template, position it, and apply text styling. By the end you will have a ready‑to‑open VSDX file that can be edited further in Microsoft Visio.

The Visio Diagram Creation Requirements

Enterprises often need to produce technical schematics, flowcharts, or network diagrams automatically from data sources. Java developers building reporting dashboards, configuration tools, or CI pipelines frequently face the requirement to generate Visio files without manual intervention. The solution must support master‑based shape creation, precise positioning, and text styling that matches existing Visio stencils. Manual drawing in Visio is time‑consuming and error‑prone, especially when diagrams must be regenerated on each build.

The Approach: Visio Diagram Generation With Conholdate.Total

Conholdate.Total for Java offers a dedicated diagram API that works with the VSDX format. It lets you load master stencils, add shapes from those masters, reposition shapes through their XForm properties, attach text, and apply predefined text styles, all through simple Java calls. Documentation for the diagram classes is available at the official documentation, and the full API reference can be explored at the API reference. Using this library you can automate the entire Visio creation pipeline on a server or desktop environment.

Building the Solution: Create Visio Diagram in Java

The following steps walk you through the complete process, from Maven setup to saving the final VSDX file.

Add Maven Dependency and Import Classes

First, configure the Conholdate Maven repository and add 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>

The diagram classes used in this tutorial live under the com.aspose.diagram package, which ships as part of the Conholdate.Total for Java bundle:

import com.aspose.diagram.Diagram;
import com.aspose.diagram.Shape;
import com.aspose.diagram.TypeValue;
import com.aspose.diagram.Txt;
import com.aspose.diagram.SaveFileFormat;

Initialize the Diagram and Load a Master Shape

Create a new Diagram instance, then load a stencil file and pick the master you want to base your shape on. Here we use the built‑in “Rectangle” master from the “Basic Shapes.vss” stencil.

// Create a new instance of a diagram
Diagram diagram = new Diagram();

// Define the name of the master (template) to be used for creating shapes
String masterName = "Rectangle";
diagram.addMaster("Basic Shapes.vss", masterName);

Add a Shape from the Master and Set Its Position

Add a shape to the diagram using the master, then look the shape up by the ID that addShape returns so you can adjust its position.

// Define the dimensions and position for the new shape
double width = 2, height = 2, pinX = 4.25, pinY = 4.5;

// Add a new rectangle shape to the diagram using the specified master
long rectangleId = diagram.addShape(pinX, pinY, width, height, masterName, 0);

// Retrieve the shape by its ID for modification
Shape rectangle = diagram.getPages().get(0).getShapes().getShape(rectangleId);

// Set the position of the shape by modifying its PinX and PinY properties
rectangle.getXForm().getPinX().setValue(5);
rectangle.getXForm().getPinY().setValue(5);

// Set the type of the shape to indicate it is a standard shape
rectangle.setType(TypeValue.SHAPE);

Add Text and Apply a Text Style

Attach a text run to the shape and apply one of the diagram’s predefined style sheets so the label matches the rest of the document.

// Add text to the shape
rectangle.getText().getValue().add(new Txt("Aspose Diagram"));

// Apply a predefined text style to the shape's text
rectangle.setTextStyle(diagram.getStyleSheets().get(3));

Save the Diagram to VSDX

Finally, write the diagram to a VSDX file that can be opened in Microsoft Visio.

diagram.save("Visio_out.vsdx", SaveFileFormat.VSDX);

Create Visio Diagram in Java - Full Working Sample - Complete Code Example

The following code demonstrates the entire workflow from start to finish.

import com.aspose.diagram.Diagram;
import com.aspose.diagram.Shape;
import com.aspose.diagram.TypeValue;
import com.aspose.diagram.Txt;
import com.aspose.diagram.SaveFileFormat;

public class CreateVisioDiagram {
    public static void main(String[] args) {
        try {
            // Create a new instance of a diagram
            Diagram diagram = new Diagram();

            // Define the name of the master (template) to be used for creating shapes
            String masterName = "Rectangle";
            diagram.addMaster("Basic Shapes.vss", masterName);

            // Define the dimensions and position for the new shape
            double width = 2, height = 2, pinX = 4.25, pinY = 4.5;

            // Add a new rectangle shape to the diagram using the specified master
            long rectangleId = diagram.addShape(pinX, pinY, width, height, masterName, 0);

            // Retrieve the shape by its ID for modification
            Shape rectangle = diagram.getPages().get(0).getShapes().getShape(rectangleId);

            // Set the position of the shape by modifying its PinX and PinY properties
            rectangle.getXForm().getPinX().setValue(5);
            rectangle.getXForm().getPinY().setValue(5);

            // Set the type of the shape to indicate it is a standard shape
            rectangle.setType(TypeValue.SHAPE);

            // Add text to the shape
            rectangle.getText().getValue().add(new Txt("Aspose Diagram"));

            // Apply a predefined text style to the shape's text
            rectangle.setTextStyle(diagram.getStyleSheets().get(3));

            // Save the modified diagram to a file
            diagram.save("Visio_out.vsdx", SaveFileFormat.VSDX);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, 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.

Deployment Considerations for Visio Diagram Projects

The SDK runs on any Java 8+ runtime, making it suitable for backend services, batch jobs, or desktop utilities. When generating large diagrams, monitor heap usage and consider reusing a single Diagram instance for multiple pages to reduce memory overhead. A valid license is required for production; you can obtain a temporary license for testing from the temporary license page and purchase a full license via the pricing page. Ensure the output directory is writable by the process that executes the code.

Conclusion

Programmatically creating Visio diagrams in Java becomes straightforward with Conholdate.Total for Java. By following the steps above you can load a master stencil, generate VSDX files, position shapes precisely, and apply consistent text styling. Remember to acquire a proper production license and test the generated diagrams in Microsoft Visio to verify layout fidelity. With the SDK in place, you can automate diagram creation across a wide range of enterprise scenarios.

FAQs

  • How can I create a Visio Diagram in Java using a master template?
    Use the diagram classes provided by Conholdate.Total for Java, load a stencil with addMaster, add a shape from that master with addShape, and save the file as VSDX.

  • What file format does the SDK generate for Visio diagrams?
    The SDK outputs the modern VSDX format, which is fully compatible with Microsoft Visio.

  • Do I need a license to run the generated Visio files in production?
    Yes. Obtain a production license from the pricing page and you may use a temporary license during development.

  • Can I integrate this diagram generation into a web service?
    Absolutely. The SDK is a pure Java library, so you can call it from any Java‑based backend such as Spring Boot or Jakarta EE.

Read More