在 C# 中创建 Visio 图表

本文讨论如何使用 C# 创建 Visio 图表。它分享了使用 C# 自动创建 Visio 图表的分步过程和工作示例代码。此外,它还介绍了以不同文件格式导出输出 Visio 图表。

在 C# 中创建 Visio 图表的步骤

  1. 安装 Aspose.Diagram API 以创建 Visio 图表
  2. 创建 Diagram 类的实例
  3. 使用模板文件添加母版
  4. 使用 AddShape() 方法插入矩形形状并设置不同的属性
  5. 使用 Save() 方法导出输出 Visio 图表

这些步骤概述了如何在 C# 中创建 Visio。首先,从头开始创建图表并加载主模板。插入形状,同时指定不同的参数,例如尺寸、位置和页码。最后,将输出的 Visio 图表渲染为 VSDX 文件以完成该过程。

使用 C# 以编程方式创建 Visio 图表的代码

using Aspose.Diagram;
class CreateVisioDiagram
{
static void Main()
{
new License().SetLicense("license.lic");// Initialize the Drawing License
// Create a new instance of a diagram
Aspose.Diagram.Diagram diagram = new Aspose.Diagram.Diagram();
// Define the name of the master (template) to be used for creating shapes
string masterName = "Rectangle";
diagram.AddMaster("Basic Shapes.vss", masterName);
// Define the dimensions and position for the new shape
double width = 2, height = 2, pinX = 4.25, pinY = 4.5;
// Add a new rectangle shape to the diagram using the specified master
long rectangleId = diagram.AddShape(pinX, pinY, width, height, masterName, 0);
// Retrieve the shape by its ID for modification
Aspose.Diagram.Shape rectangle = diagram.Pages[0].Shapes.GetShape(rectangleId);
// Set the position of the shape by modifying its PinX and PinY properties
rectangle.XForm.PinX.Value = 5;
rectangle.XForm.PinY.Value = 5;
// Set the type of the shape to indicate it is a standard shape
rectangle.Type = Aspose.Diagram.TypeValue.Shape;
// Add text to the shape
rectangle.Text.Value.Add(new Aspose.Diagram.Txt("Aspose Diagram"));
// Apply a predefined text style to the shape's text
rectangle.TextStyle = diagram.StyleSheets[3];
// Save the modified diagram to a file
diagram.Save("Visio_out.vsdx", Aspose.Diagram.SaveFileFormat.Vsdx);
}
}

您可以使用此代码片段在 C# 中的 Visio 中绘制任何流程图。此示例代码是一个基本版本,但您可以对其进行增强,以绘制其他几种类型的形状和连接器,以满足您的要求。同样,根据您的要求调整输出文件格式,以将生成的绘图呈现为光栅图像、矢量图像、PDF 或其他格式。

本教程涵盖了使用 C# 以编程方式创建 Visio 图表的信息。但是,如果您有兴趣探索 VSD 文件转换,请阅读 在 C# 中将 VSD 转换为 VSDX 上的文章。

 简体中文