Over a million developers have joined DZone.

How to Add New Shapes to a Visio Diagram inside Java Applications

·
This technical tip shows how Java developers can add new shapes to a Visio diagram inside their own applications using Aspose.Diagram for Java.  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 diagram. 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 article describes how to add a new rectangle shape to a diagram. Use Aspose.Diagram for Java API to create new shapes and then add these shapes to a diagram's shapes collection.

To add a new shape:

  • Find the page you want to add a new shape to.
  • Set master shape.
  • Set a new ID for the shape.
  • Set shape properties.
//Load a diagram
Diagram diagram = new Diagram("C:\\Drawing1.vdx");

//Get first page
if (diagram.getPages().getCount() == 0)
{
    JOptionPane.showConfirmDialog(null, "The diagram does not contain pages.");
    return;
}
Page page0 = diagram.getPages().getPage(0);
//Get the rectangle master
Master masterRectangle = null;
for (Master master : (Iterable) diagram.getMasters())
    if (master.getName() == "Rectangle")
    {
        masterRectangle = master;
        break;
    }
if (masterRectangle == null)
{
    JOptionPane.showConfirmDialog(null, "The diagram does not contain rectangle's master.");
    return;
}
//Get the next shape ID
long nextID;
for (Page page : (Iterable)diagram.getPages())
{
    for (Shape shape : (Iterable) page.getShapes())
    {
        long temp = GetMaxShapeID(shape);
        if (temp > nextID)
            nextID = temp;
    }
    nextID++;
}
//Set shape properties and add it in the shapes' collection
Shape rectangle = new Shape();
rectangle.setMaster(masterRectangle);
rectangle.setMasterShape(masterRectangle.getShapes().getShape(0));
rectangle.setID(nextID);
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().getStyleSheet(3));
rectangle.getLine().getLineColor().setValue(page0.getShapes().getShape(1).getFill().getFillForegnd().getValue());
rectangle.getLine().getLineWeight().setValue(0.03041666666666667);
rectangle.getLine().getRounding().setValue(0.1);
rectangle.getFill().getFillBkgnd().setValue(page0.getShapes().getShape(0).getFill().getFillBkgnd().getValue());
rectangle.getFill().getFillForegnd().setValue("#ebf8df");
page0.getShapes().add(rectangle);
diagram.save("C:\\output.vdx", SaveFileFormat.VDX);
JOptionPane.showConfirmDialog(null, "Shape has been added.");

private long GetMaxShapeID(Shape shape)
{
    long max = shape.getID();
    for (Shape child : (Iterable)shape.getShapes())
    {
        long temp = GetMaxShapeID(child);
        if (temp > max)
            max = temp;
    }
    return max;
}

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}