首页 / 操作系统 / Linux / 如何使用Aspose.Diagram for .NET将矩形添加到图表中
Aspose.Diagram for .NET允许你操作Visio图表,但在某些情况下,您需要添加新的图状到在您的图表中。在这种情况下,你可以通过Aspose.Diagram for .NET的API来创建新的形状,并将这些形状添加到图表中的形状集合中。本文主要介绍如何添加一个新的矩形到你的图中。Aspose.Diagram 的详细介绍:请点这里 Aspose.Diagram 的下载地址:请点这里添加新的形状需遵循以下步骤:* 查找页面并添加新形状 * 为新的形状设置一个ID * 设置主控形状 * 设置形状的属性查找页面 每个图表包含了网页的集合。你可以循环访问图形页面的集合,并将所需要的页面储存到Page类对象。[C#]//Load a diagramDiagram diagram = new Diagram(Server.MapPath("Drawing1.vdx"));//Get first pagePage page0 = diagram.Pages[0];[Visual Basic]"Load a diagramDim diagram As New Diagram(Server.MapPath("Drawing1.vdx"))"Get first pageDim page0 As Page = diagram.Pages(0)查找主控形状 每个图表包含了主控形状的集合。你可以循环访问主控形状的集合,并将所需要的主控形状储存到主类的对象。[C#]//Get the rectangle masterMaster masterRectangle = null;foreach (Master master in diagram.Masters)if (master.Name == "Rectangle"){masterRectangle = master;break;}[Visual Basic]"Get the rectangle masterDim masterRectangle As Master = NothingFor Each master As Master In diagram.MastersIf master.Name = "Rectangle" ThenmasterRectangle = masterExit ForEnd IfNext master计算下一个ID 你需要一个新的ID并将新图形添加到图表中。你可以循环访问每个页面的形状集合,以查找到最大的ID。在查找到在最大的ID后,你可以轻松地计算出下一个ID。[C#]//Get next shape IDlong nextID = -1L;foreach (Page page in diagram.Pages)foreach (Shape shape in page.Shapes){long temp = GetMaxShapeID(shape);if (temp > nextID)nextID = temp;}nextID++;[Visual Basic]"Get next shape IDDim nextID As Long = -1LFor Each page As Page In diagram.PagesFor Each shape As Shape In page.ShapesDim temp As Long = GetMaxShapeID(shape)If temp > nextID ThennextID = tempEnd IfNext shapeNext pagenextID += 1GetMaxShapeID 方法的代码:[C#]private long GetMaxShapeID(Shape shape){long max = shape.ID;foreach (Shape child in shape.Shapes){long temp = GetMaxShapeID(child);if (temp > max)max = temp;}return max;}[Visual Basic]Private Function GetMaxShapeID(ByVal shape As Shape) As LongDim max As Long = shape.IDFor Each child As Shape In shape.ShapesDim temp As Long = GetMaxShapeID(child)If temp > max Thenmax = tempEnd IfNext childReturn maxEnd Function接下来请看第2页精彩内容:http://www.linuxidc.com/Linux/2013-10/90981p2.htm