更新 VSTO 和 Aspose.Diagram 中的形状属性
Contents
[
Hide
]
VSTO
VSTO 允许您使用 Microsoft Visio 文件进行编程。要更新形状属性:
- 创建一个 Visio 应用程序对象。
- 使应用程序对象不可见。
- 打开现有的 Visio VSD 文件。
- 找到所需的形状。
- 更新形状属性(文本、文本样式、位置和大小)。
- 将文件另存为 VDX。
Visio.Application vsdApp = null;
Visio.Document vsdDoc = null;
try
{
//Create Visio Application Object
vsdApp = Application;
//Make Visio Application Invisible
vsdApp.Visible = false;
//Create a document object and load a diagram
vsdDoc = vsdApp.Documents.Open( "Drawing.vsd");
//Create page object to get required page
Visio.Page page = vsdApp.ActivePage;
//Create shape object to get required shape
Visio.Shape shape = page.Shapes["Process1"];
//Set shape text and text style
shape.Text = "Hello World";
shape.TextStyle = "CustomStyle1";
//Set shape's position
shape.get_Cells("PinX").ResultIU = 5;
shape.get_Cells("PinY").ResultIU = 5;
//Set shape's height and width
shape.get_Cells("Height").ResultIU = 2;
shape.get_Cells("Width").ResultIU = 3;
//Save file as VDX
vsdDoc.SaveAs("Drawing1.vdx");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//Close active document and quit
vsdDoc.Close();
vsdApp.Quit();
}
Aspose.Diagram
用Aspose.Diagram编程时,机器上不需要Microsoft Visio,可以独立于Microsoft Office自动化工作。要使用 Aspose.Diagram for .NET 更新形状属性:
- 打开现有的 Visio VSD 文件。
- 找到所需的形状。
- 更新形状属性(文本、文本样式、位置和大小)。
- 将文件另存为 VDX。
//Save the uploaded file as PDF
Diagram diagram = new Diagram("Drawing1.vsd");
//Find a particular shape and update its properties
foreach (Aspose.Diagram.Shape shape in diagram.Pages[0].Shapes)
{
if (shape.Name.ToLower() == "process1")
{
shape.Text.Value.Clear();
shape.Text.Value.Add(new Txt("Hello World"));
//Find custom style sheet and set as shape's text style
foreach (StyleSheet styleSheet in diagram.StyleSheets)
{
if (styleSheet.Name == "CustomStyle1")
{
shape.TextStyle = styleSheet;
}
}
//Set horizontal and vertical position of the shape
shape.XForm.PinX.Value = 5;
shape.XForm.PinY.Value = 5;
//Set height and width of the shape
shape.XForm.Height.Value = 2;
shape.XForm.Width.Value = 3;
}
}
//Save shape as VDX
diagram.Save("Drawing1.vdx", SaveFileFormat.VDX);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}