Update shape properties in VSTO and Aspose.Diagram
Contents
[
Hide
]
VSTO
VSTO lets you program with Microsoft Visio files. To update shape properties:
- Create a Visio application object.
- Make the application object invisible.
- Open an existing Visio VSD file.
- Find the required shape.
- Update the shape properties (text, text style, position and size).
- Save the file as 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
When programming with Aspose.Diagram, you do not need Microsoft Visio on the machine, and you can work independently of Microsoft Office Automation. To update shape properties with Aspose.Diagram for .NET:
- Open an existing Visio VSD file.
- Find the required shape.
- Update the shape properties (text, text style, position and size).
- Save the file as 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);
}