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. <br/> <br/> To add a new shape: <br/> <br/> • Find the page you want to add a new shape to. <br/> • Set master shape. <br/> • Set a new ID for the shape. <br/> • Set shape properties. <br/>
//your code here... //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<Master>) 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<Page>)diagram.getPages()) { for (Shape shape : (Iterable<Shape>) 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>)shape.getShapes()) { long temp = GetMaxShapeID(child); if (temp > max) max = temp; } return max; }

Url: http://www.aspose.com/java/diagram-component.aspx

Language: Java | User: Sheraz Khan | Created: Aug 13, 2014 | Tags: Enhanced Project Data Rendering, Render Graphical Column Indicators, Gantt Chart Lines in Gradient, writing project data to output PDF Enhanced Project Data Rendering Render Graphical Column Indicators Gantt Chart Lines in Gradient reading MPP 2007 file format add new shapes to Visio diagram manipulate Microsoft Visio diagrams add shape to diagram in Java Java Visio Component Java Visio API Convert Visio Files to PDF export Visio Files to XML