DEV Community

AsposeDiagram
AsposeDiagram

Posted on • Edited on

2 1

Manipulate Microsoft Visio file format programmatically in Java

Aspose.Diagram API can be used to open, read, update, change, manipulate and construct Microsoft Visio files and its component parts without the need to install or automate Microsoft Visio application.

Purpose

The purpose of this article is to explain how to manipulate Microsoft Visio file format programmatically in Java.

Demonstration

The article demonstrates how to load Template Microsoft Visio file and import some Master Shapes i.e. Rectangle, Ellipse, Hexagon, Dynamic connector etc. from Stencil Microsoft Visio file and create Normal Shapes out of them and fill them with colors and formatted text and connect them via Connectors and save it as Output Microsoft Visio file.

Install Aspose.Diagram using Maven in Eclipse IDE

In order to execute the sample code, it is recommended that you know how to install Aspose.Diagram using Maven in Eclipse IDE, which has been discussed separately in this article. Please go through it as per your needs.

Microsoft Visio Formats Supported By Aspose.Diagram

Aspose.Diagram supports all of the Microsoft Visio formats. Some of them are listed below.

  • VSD — Drawing
  • VSS — Stencil
  • VST — Template
  • VSDX — OPC/XML Drawing
  • VSSX  OPC/XML Stencil
  • VSTX — OPC/XML Template
  • VSDM — OPC/XML Drawing, macro-enabled
  • VSSM — OPC/XML Stencil, macro-enabled
  • VSTM — OPC/XML Template, macro-enabled

Input Microsoft Visio Files Used Inside Code

This section presents and explains the input Microsoft Visio files that are used inside the sample code.

Template Microsoft Visio File

The sample code manipulates the Template Microsoft Visio File named as templateManipulateVisioFileUsingAsposeDiagram.vsdx using Aspose.Diagram. It is shown in the following screenshot. Please note, you can use any Microsoft Visio file as a template and the code should work fine.

Template Microsoft Visio file to be manipulated using Aspose.Diagram.

Stencil Microsoft Visio File Containing Master Shapes

The code uses Stencil Microsoft Visio File named as SampleMasterShapes.vssx containing the sample Master Shapes and imports them inside the template file using Aspose.Diagram. As you can see inside the screenshot, it contains 4 master shapes i.e.

  • Rectangle
  • Ellipse
  • Hexagon
  • Dynamic connector

Stencil Microsoft Visio file to import Master Shapes using Aspose.Diagram.

Sample Code

Please execute the following Java sample code with the provided input Microsoft Visio files. The code performs the following tasks.

  • Load the template Microsoft Visio file inside the Diagram (i.e. com.aspose.diagram.Diagram) object.
  • Access the Page-1 inside the template Microsoft Visio file.
  • Import Master Shapes from stencil Microsoft Visio file.
  • Add Normal Shapes using Master Shapes and assign them names.
  • Access Normal Shapes by their assigned names.
  • Fill the Normal Shapes with color and add formatted text inside them.
  • Connect the Normal Shapes with each other using Connectors.
  • Save the Diagram object in Microsoft Visio VSDX format.

Code is explained well with useful comments and blocks of comments which will help you to understand it easily.

// Directory path for input and output documents.
String dirPath = "D:\\Download\\";
// Load the template Microsoft Visio file in Aspose Diagram object.
Diagram dg = new Diagram(dirPath + "templateManipulateVisioFileUsingAsposeDiagram.vsdx");
// Access the Page-1 of the template Visio file.
Page pg = dg.getPages().getPage("Page-1");
// Add Master Shapes into Aspose Diagram object from SampleMasterShapes Stencil.
dg.addMaster(dirPath + "SampleMasterShapes.vssx", "Rectangle");
dg.addMaster(dirPath + "SampleMasterShapes.vssx", "Ellipse");
dg.addMaster(dirPath + "SampleMasterShapes.vssx", "Hexagon");
dg.addMaster(dirPath + "SampleMasterShapes.vssx", "Dynamic connector");
/*--------------------------------------------------------
START: Add Shapes
--------------------------------------------------------*/
// Add Rectangle shape from master Rectangle shape and assign it name.
long shId = dg.addShape(2.70, 7.54, 1.5, 1, "Rectangle", 0);
pg.getShapes().getShape(shId).setName("shpRect");
// Add Ellipse shape from master Ellipse shape and assign it name.
shId = dg.addShape(2.70, 5.1, 2.25, 1.5, "Ellipse", 0);
pg.getShapes().getShape(shId).setName("shpEllipse");
// Add Hexagon shape from master Hexagon shape and assign it name.
shId = dg.addShape(6.3, 5.1, 1.5, 1.5, "Hexagon", 0);
pg.getShapes().getShape(shId).setName("shpHexagon");
// Add first "Dynamic connector" shape from master "Dynamic connector" shape and assign it name.
shId = dg.addShape(4.3, 3.1, 1.5, 1.5, "Dynamic connector", 0);
pg.getShapes().getShape(shId).setName("shpDynConnect1");
// Add second "Dynamic connector" shape from master "Dynamic connector" shape and assign it name.
shId = dg.addShape(4.3, 3.1, 1.5, 1.5, "Dynamic connector", 0);
pg.getShapes().getShape(shId).setName("shpDynConnect2");
/*--------------------------------------------------------
END: Add Shapes
--------------------------------------------------------*/
// Access all added shapes by their assigned names.
Shape shpRect = pg.getShapes().getShape("shpRect");
Shape shpEllipse = pg.getShapes().getShape("shpEllipse");
Shape shpHexagon = pg.getShapes().getShape("shpHexagon");
Shape shpDynConnect1 = pg.getShapes().getShape("shpDynConnect1");
Shape shpDynConnect2 = pg.getShapes().getShape("shpDynConnect2");
/*--------------------------------------------------------
START: Fill and Format Rectangle Shape
--------------------------------------------------------*/
// Fill the Rectangle shape with Red color.
shpRect.getFill().getFillForegnd().setValue("#ff0000");
// Add text inside the shape.
shpRect.getText().getValue().add(new Cp(0));
shpRect.getText().getValue().add(new Txt("Structure"));
// Format text of the shape.
shpRect.getChars().add(new com.aspose.diagram.Char());
shpRect.getChars().get(0).setIX(0);
// Set text shape color as White.
shpRect.getChars().get(0).getColor().setValue("#FFFFFF");
// Set text style as Bold.
shpRect.getChars().get(0).getStyle().setValue(StyleValue.BOLD);
// Set text size as 0.2.
shpRect.getChars().get(0).getSize().setValue(0.2);
/*--------------------------------------------------------
END: Fill and Format Rectangle Shape
--------------------------------------------------------*/
/*--------------------------------------------------------
START: Fill and Format Ellipse Shape
--------------------------------------------------------*/
// Fill the Ellipse shape with Green color.
shpEllipse.getFill().getFillForegnd().setValue("#00ff00");
// Add text inside the shape.
shpEllipse.getText().getValue().add(new Cp(0));
shpEllipse.getText().getValue().add(new Txt("Role"));
// Format text of the shape.
shpEllipse.getChars().add(new com.aspose.diagram.Char());
shpEllipse.getChars().get(0).setIX(0);
// Set text shape color as Black.
shpEllipse.getChars().get(0).getColor().setValue("#000000");
// Set text style as Bold.
shpEllipse.getChars().get(0).getStyle().setValue(StyleValue.BOLD);
// Set text size as 0.2.
shpEllipse.getChars().get(0).getSize().setValue(0.2);
/*--------------------------------------------------------
END: Fill and Format Ellipse Shape
--------------------------------------------------------*/
/*--------------------------------------------------------
START: Fill and Format Hexagon Shape
--------------------------------------------------------*/
// Fill the Ellipse shape with Yellow color.
shpHexagon.getFill().getFillForegnd().setValue("#ffff00");
// Add text inside the shape.
shpHexagon.getText().getValue().add(new Cp(0));
shpHexagon.getText().getValue().add(new Txt("Incentive"));
// Format text of the shape.
shpHexagon.getChars().add(new com.aspose.diagram.Char());
shpHexagon.getChars().get(0).setIX(0);
// Set text shape color as Black.
shpHexagon.getChars().get(0).getColor().setValue("#000000");
// Set text style as Bold.
shpHexagon.getChars().get(0).getStyle().setValue(StyleValue.BOLD);
// Set text size as 0.2.
shpHexagon.getChars().get(0).getSize().setValue(0.2);
/*--------------------------------------------------------
END: Fill and Format Hexagon Shape
--------------------------------------------------------*/
// Connect Rectangle shape with Ellipse shape using first "Dynamic connector".
pg.connectShapesViaConnector(shpRect, ConnectionPointPlace.BOTTOM, shpEllipse,
ConnectionPointPlace.TOP, shpDynConnect1);
// Connect Hexagon shape with Ellipse shape using second "Dynamic connector".
pg.connectShapesViaConnector(shpHexagon, ConnectionPointPlace.TOP, shpEllipse,
ConnectionPointPlace.RIGHT, shpDynConnect2);
// Save the Aspose Diagram object into Microsoft Visio VSDX format.
dg.save(dirPath + "outputManipulateVisioFileUsingAsposeDiagram.vsdx", SaveFileFormat.VSDX);

Output Visio File Generated By Aspose.Diagram

After successful execution of sample code, it generates the Output Microsoft Visio File named as outputManipulateVisioFileUsingAsposeDiagram.vsdx using Aspose.Diagram as shown in this screenshot.

This implies that you can manipulate Microsoft Visio Documents using Aspose.Diagram efficiently and conveniently.

Output Microsoft Visio file generated using Aspose.Diagram.

See also

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more