Insert a Text Shape and Modify ActiveX Control Properties in the Visio Drawing using Aspose.Diagram

« Back to Blog List | By Ian Hucklesby | 15 Aug 2017
Aspose.Diagram for Java

Adding a New Shape in Visio

Aspose.Diagram for Java allows you to manipulate Microsoft Visio diagrams in different ways. One of the things you can do is add new shapes to a diagrams. Aspose.Diagram for Java lets you to add a new shape to a diagram. The added shape can also be customized using Aspose.Diagram for Java.

This topic describes how to add a new rectangle shape to a diagram.

Use Aspose.Diagram for Java's API to create new shapes and then add these shapes to a diagram's shapes collection.

To add a new shape:
1. Find the page - Each Visio diagram contains a collection of pages. Developers may retrieve the page by page ID or Name and store the required page in the Page class object.
2. Add the require Master of a Shape - Each Visio diagram contains a collection of masters. Developers may add a Master (by ID or Name) from the existing stencil file (by direct path or file stream).
3. Add shape in the Visio diagram - Developers can place a new shape in the Visio diagram by page index (starting from 0), master name, PinX, PinY, height (optional) and width (optional).
4. Set shape properties - AddShape method of the Diagram class returns the shape ID. Developers can retrieve shape from a Visio diagram by using this ID, and then set its properties, e.g. color, position, alignment and text.

 

Add Programming Sample

The code snippet below shows how to do each step.

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(AddingNewShape.class);
//Load a diagram
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
// get page by name
Page page = diagram.getPages().getPage("Page-2");

// Add master with stencil file path and master id
String masterName = "Rectangle";
// Add master with stencil file path and master name
diagram.addMaster(dataDir + "Basic Shapes.vss", masterName);

// page indexing starts from 0
int PageIndex = 1;
double width = 2, height = 2, pinX = 4.25, pinY = 4.5;
//Add a new rectangle shape
long rectangleId = diagram.addShape(pinX, pinY, width, height, masterName, PageIndex);

// set shape properties
Shape rectangle = page.getShapes().getShape(rectangleId);
rectangle.getXForm().getPinX().setValue(5);
rectangle.getXForm().getPinY().setValue(5);
rectangle.setType(TypeValue.SHAPE);
rectangle.getText().getValue().add(new Txt("Aspose Diagram"));
rectangle.setTextStyle(diagram.getStyleSheets().get(3));
rectangle.getLine().getLineColor().setValue("#ff0000");
rectangle.getLine().getLineWeight().setValue(0.03);
rectangle.getLine().getRounding().setValue(0.1);
rectangle.getFill().getFillBkgnd().setValue("#ff00ff");
rectangle.getFill().getFillForegnd().setValue("#ebf8df");

diagram.save(dataDir + "AddShape_Out.vsdx", SaveFileFormat.VSDX);
System.out.println("Shape has been added.");

Retrieving Shape Information
Working with Diagrams explains how to create diagrams, add shapes and connectors, and then how to retrieve information about diagram elements such as pages, masters, connectors and fonts. This article looks at how to retrieve information about shapes in a diagram.
Each shape in a diagram has an ID and a name. The ID is important when programming with Visio: it is the main method for accessing a shape. Each shape also retains information about what master (stencil) it is made from.
A Shape is an object in a Visio drawing. The Shapes property, exposed by the Page class, supports a collection of Aspose.Diagram.Shape objects. The Shapes property can be used to retrieve information about a shape.
In the console window below, for example, you can see information output for a diagram that contained four shapes: two terminators, a process and a dynamic connector. Each has a unique ID as well as the name of the master (stencil) shape.

A console window showing shape information

To retrieve Visio page information:
1. Loads a diagram.
2. Sets up a foreach loop to loop through all the shapes in the diagram.
3. Displays shape information.
Retrieve Programming Sample
The following piece of code retrieve the shape information from a Visio diagram.

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RetrieveShapeInfo.class);

//Load diagram
Diagram diagram = new Diagram(dataDir+ "RetrieveShapeInfo.vsd");

for (com.aspose.diagram.Shape shape : (Iterable<Shape>) diagram.getPages().getPage(0).getShapes())
{
//Display information about the shapes
System.out.println("\nShape ID : " + shape.getID());
System.out.println("Name : " + shape.getName());
System.out.println("Master Shape : " + shape.getMaster().getName());
}

Copy Shapes from an Existing Visio
Aspose.Diagram for Java API allows developers to copy shapes from the source Visio page to the new Visio diagram page. It also supports copying group shapes. This article describes how to copy all shapes from the the source diagram page.
To copy shapes, developers should also copy source PageSheet and source Visio themes to preserve shape fill style and other formatting properties.
This example work as follows:
1. Load a source Visio.
2. Initialize a new Visio
3. Add masters and themes from the source Visio.
4. Get page from the source Visio.
5. Copy its PageSheet to the new Visio Page.
6. Iterate through the shapes of the source Visio page.
7. Set its new id and add to the new Visio page.
8. Save the new Visio in the local storage.
Copy Programming Sample

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CopyShape.class);
// load a source Visio
Diagram srcVisio = new Diagram(dataDir + "Drawing1.vsdx");

// initialize a new Visio
Diagram newDiagram = new Diagram();

// add all masters from the source Visio diagram
MasterCollection originalMasters = srcVisio.getMasters();
for (Master master : (Iterable<Master>) originalMasters)
newDiagram.addMaster(srcVisio, master.getName());

// get the page object from the original diagram
Page SrcPage = srcVisio.getPages().getPage("Page-1");
// copy themes from the source diagram
newDiagram.copyTheme(srcVisio);
// copy pagesheet of the source Visio page
newDiagram.getPages().get(0).getPageSheet().copy(SrcPage.getPageSheet());

// copy shapes from the source Visio page
for (Shape shape :(Iterable<Shape>) SrcPage.getShapes())
{
newDiagram.getPages().get(0).getShapes().add(shape);
}
// save the new Visio
newDiagram.save(dataDir + "CopyShapes_Out.vsdx", SaveFileFormat.VSDX);

Reading Visio Shape Data
The Props collection exposed by the Shape class supports the Aspose.Diagram.Prop object. The property can be used to read a shape's data (custom properties).
Read All Shape Properties
To identify custom properties in Microsoft Visio:
1. In a diagram, right-click a shape.
2. Select Data, then Shape Data from the menu.
Any existing properties are listed in the dialog.

 

Read Programming Sample
The code snippets below reads shape data (custom properties).

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ReadAllShapeProps.class);

// call a Diagram class constructor to load the VSDX diagram
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
// get page by name
Page page = diagram.getPages().getPage("Page-3");

for (Shape shape :(Iterable<Shape>) page.getShapes())
{
if (shape.getName() == "Process1")
{
for (Prop property :(Iterable<Prop>) shape.getProps())
{
System.out.println(property.getLabel().getValue() + ": " + property.getValue().getVal());
}
break;
}
}

Read a Shape Property by Name
The code snippet below reads a shape property by name (custom property).
Read by Name Programming Sample

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ReadShapePropByName.class);
// call a Diagram class constructor to load the VSDX diagram
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
// get page by name
Page page = diagram.getPages().getPage("Page-3");

for (Shape shape :(Iterable<Shape>) page.getShapes())
{
if (shape.getName() == "Process1")
{
Prop property = shape.getProps().getProp("Name1");
System.out.println(property.getLabel().getValue() + ": " + property.getValue().getVal());
}
}

Retrieve the Parent Shape of a Sub-Shape
Aspose.Diagram for Java allows developers to retrieve the parent shape of a sub-shape.
Get the Parent Shape
The Shape class offers getParentShape property to retrieve the parent shape.
Get the Parent Shape Programming Sample

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(RetrieveTheParentShape.class) + "Shapes\\";

// Call a Diagram class constructor to load the VSD diagram
Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx");
// get a sub-shape by page name, group shape ID, and then sub-shape ID
Shape shape = diagram.getPages().getPage("Page-3").getShapes().getShape(13).getShapes().getShape(2);
Shape parentShape = shape.getParentShape();
System.out.println("Parent Shape's Properties:");
System.out.println("Shape ID: " + parentShape.getID());
System.out.println("Shape Name: " + parentShape.getName());
System.out.println("Shape Type: " + parentShape.getType());