让VSDX图绘制变得更简单!使用Aspose.Diagram快速创建MS Visio VSDX图

为了使VSDX操作自动化,本文为您提供了有关如何在C#中从头创建VSDX图的基础教程。此外,它还介绍了如何从.NET应用程序中在VSDX图中插入页面,形状和文本。

MS Visio是一种流行的应用程序,可让您创建各种图,例如流程图,数据流程图,业务流程模型等。VSDX是MS Visio用于存储图的文件格式。

(安装包仅提供部分功能,并设置限制,如需试用完整功能请申请免费授权。)

17周年庆来啦!整合所有格式API处理控件Aspose.Total永久授权火热促销中,新购乐享85折起!联系客服立马1分钟了解全部!

为了使VSDX操作自动化,本文为您提供了有关如何在C#中从头创建VSDX图的基础教程。此外,它还介绍了如何从.NET应用程序中在VSDX图中插入页面,形状和文本。

  • 使用C#创建MS Visio图
  • 在C#中将Master添加到VSDX图
  • 在C#中的Visio图表中插入页面
  • 在C#中的Visio图表中创建形状
  • 在C#中的Visio页面中添加文本形状

使用C#创建MS Visio VSDX图

首先,让我们从头开始创建一个空的VSDX图。以下是执行此操作的步骤:

  • 创建一个Diagram类的实例。
  • 使用Diagram.Save(Sring fileName,SaveFileFormat.VSDX)方法将文件另存为VSDX。

下面的代码示例演示如何在C#中创建MS Visio VSDX图表。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Diagrams();// Create directory if it is not already present.bool IsExists = System.IO.Directory.Exists(dataDir);if (!IsExists)    System.IO.Directory.CreateDirectory(dataDir);// Initialize a new VisioDiagram diagram = new Diagram();dataDir = dataDir + "CreateDiagram_out.vsdx";// Save in the VSDX formatdiagram.Save(dataDir, SaveFileFormat.VSDX);

在C#中将Master添加到VSDX图

母版用于添加模具,该模具包含将在图中使用的形状的集合。如果要添加母版,则需要VSS模板文件和母版ID。以下是使用Aspose.Diagram向Visio图表添加母版的步骤。

  • 使用Diagram类创建一个新图或加载一个现有图。
  • 使用Diagram.AddMaster(String fileName,Int masterID)方法添加母版。
  • 使用Diagram.AddShape(Double,Double,String,Int)方法将母版中的形状添加到图中。
  • 使用Diagram.Save(Sring fileName,SaveFileFormat.VSDX)方法保存图。

下面的代码示例演示如何使用C#将母版添加到Visio图。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Master();// Load diagramDiagram diagram = new Diagram();// Load stencil to a streamstring templateFileName = dataDir + "NetApp-FAS-series.vss";Stream stream = new FileStream(templateFileName, FileMode.Open);// Add master with stencil file path and master idstring masterName = "FAS80xx rear empty";diagram.AddMaster(templateFileName, 2);// Add master with stencil file path and master namediagram.AddMaster(templateFileName, masterName);// Add master with stencil file stream and master iddiagram.AddMaster(stream, 2);// Adds master to diagram from source diagramDiagram src = new Diagram(templateFileName);diagram.AddMaster(src, masterName);// Add master with stencil file stream and master iddiagram.AddMaster(stream, masterName);// Adds shape with defined PinX and PinY.diagram.AddShape(2.0, 2.0, masterName, 0);diagram.AddShape(6.0, 6.0, masterName, 0);// Adds shape with defined PinX,PinY,Width and Height.diagram.AddShape(7.0, 3.0, 1.5, 1.5, masterName, 0);

在C#中的Visio图表中插入页面

MS Visio图表由一页或多页组成,每页都包含图表。因此,在添加形状之前,您需要使用以下步骤添加页面。

  • 使用Diagram类创建一个新图或加载一个现有图。
  • 使用Diagram.Pages.Count属性检查图是否已包含页面。
  • 如果是这样,请使用Diagram.Pages [index] .ID属性获取最后一页的ID 。
  • 使用Page类创建一个新页面并设置其名称和ID。
  • 使用Diagram.Pages.Add(Page)方法将页面添加到图中。
  • 使用Diagram.Save (Sring fileName,SaveFileFormat.VSDX)方法保存VSDX图。

下面的代码示例演示如何使用C#在Visio VSDX图中添加页面。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_VisioPages();// Load diagramDiagram diagram = new Diagram(dataDir + "Drawing1.vsdx");// It calculates max page idint max = 0;if (diagram.Pages.Count != 0)    max = diagram.Pages[0].ID;for (int i = 1; i < diagram.Pages.Count; i++) { if (max < diagram.Pages[i].ID) max = diagram.Pages[i].ID; } // Set max page ID int MaxPageId = max; // Initialize a new page object Page newPage = new Page(); // Set name newPage.Name = "new page"; // Set page ID newPage.ID = MaxPageId + 1; // Or try the Page constructor // Page newPage = new Page(MaxPageId + 1); // Add a new blank page diagram.Pages.Add(newPage); // Save diagram diagram.Save(dataDir + "InsertBlankPage_out.vsdx", SaveFileFormat.VSDX);

使用C#在VSDX图中创建形状

形状是Visio图的基础。MS Visio支持各种形状,可以在各个领域中创建图表。以下步骤显示了如何在Visio图表中插入形状。

  • 使用Diagram类创建一个新图或加载一个现有图。
  • 创建页面或在Page对象中获取所需的页面。
  • 使用Diagram.AddMaster(String fileName,Int masterID)方法将母版添加到图中。
  • 使用Diagram.AddShape(pinX,pinY,width,height,masterName,PageIndex)方法添加新的矩形形状。
  • 存储由Diagram.AddShape()方法返回的形状ID 。
  • 使用Page.Shapes.GetShape(long ID)方法检索Shape对象中新添加的形状。
  • 设置形状的属性,例如文本,颜色等。
  • 使用Diagram.Save (Sring fileName,SaveFileFormat.VSDX)方法保存VSDX图。

下面的代码示例演示如何使用C#在VSDX图中添加形状。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_Shapes();// Load a diagramDiagram diagram = new Diagram(dataDir + "Drawing1.vsdx");// Get page by namePage page = diagram.Pages.GetPage("Page-2");// Add master with stencil file path and master namestring masterName = "Rectangle";diagram.AddMaster(dataDir + "Basic Shapes.vss", masterName);    // Page indexing starts from 0int PageIndex = 1;double width = 2, height = 2, pinX = 4.25, pinY = 4.5;// Add a new rectangle shapelong rectangleId = diagram.AddShape(pinX, pinY, width, height, masterName, PageIndex);    // Set shape propertiesShape rectangle = page.Shapes.GetShape(rectangleId);rectangle.XForm.PinX.Value = 5;rectangle.XForm.PinY.Value = 5;rectangle.Type = TypeValue.Shape;rectangle.Text.Value.Add(new Txt("Aspose Diagram"));rectangle.TextStyle = diagram.StyleSheets[3];rectangle.Line.LineColor.Value = "#ff0000";rectangle.Line.LineWeight.Value = 0.03;rectangle.Line.Rounding.Value = 0.1;rectangle.Fill.FillBkgnd.Value = "#ff00ff";rectangle.Fill.FillForegnd.Value = "#ebf8df";diagram.Save(dataDir + "AddShape_out.vsdx", SaveFileFormat.VSDX);Console.WriteLine("Shape has been added.");

在C#中的Visio页面中添加文本形状

在各种情况下,您还需要向Visio图中添加文本。为此,您可以按照以下步骤操作。

  • 使用Diagram类创建一个新图或加载一个现有图。
  • 使用Diagram.Pages [0] .AddText(PinX,PinY,Width,Height,“ Test text”)方法将文本添加到特定页面。
  • 使用Diagram.Save (Sring fileName,SaveFileFormat.VSDX)方法保存VSDX图。

下面的代码示例演示如何使用C#在VSDX图中添加文本。

// For complete examples and data files, please go to https://github.com/aspose-diagram/Aspose.Diagram-for-.NET// The path to the documents directory.string dataDir = RunExamples.GetDataDir_ShapeText();// Create a new diagramDiagram diagram = new Diagram();// Set parameters and add text to a Visio pagedouble PinX = 1, PinY = 1, Width = 1, Height = 1;          diagram.Pages[0].AddText(PinX, PinY, Width, Height, "Test text");// Save diagramdiagram.Save(dataDir + "InsertTextShape_out.vsdx", SaveFileFormat.VSDX);


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年11月1日
下一篇 2020年11月1日

相关推荐

发表回复

登录后才能评论