Home > Code > C# > How to Convert a Visio Shape to Other File Formats inside .NET Applications

How to Convert a Visio Shape to Other File Formats inside .NET Applications

by sher azam   on Feb 03, 2016   Category: C#   |  Views: 546    |  Points: 25   |  Gold 


This tutorial shows how .NET developers can convert a Visio shape to 0ther file formats inside .NET applications using Aspose.Diagram for .NET. Aspose.Diagram for .NET API allows developers to convert a single Visio shape to any other supported file format. In this article, we remove all other Visio shapes from the page and customize page setting according to the source Shape size. Developers can convert a Visio shape to PDF, HTML, Image, SVG, and SWF by specifying the Visio save options. This example code work as follows:

• Load a source Visio.
• Get a particular page.
• Remove the background page.
• Build a hash table of all shapes holding the ids and names.
• Iterate through the hash table
• Remove all shapes from the Visio page, except the particular one.
• Set the page size.
• Save the Visio page in any supported file format.

The code snippets below show the complete source code for adding new shapes to a Visio diagram.

[C# Code Sample]

double shapeWidth = 0;
double shapeHeight = 0;

// load Visio diagram
Diagram srcVisio = new Diagram("C:/temp/MyVisio.vsd");
// get Visio page
Aspose.Diagram.Page srcPage = srcVisio.Pages[1];
// remove background page
srcPage.BackPage = null;

// get hash table of shapes, it holds id and name
Hashtable remShapes = new Hashtable();
//Hashtable<Long, String> remShapes = new Hashtable<Long, String>();
foreach (Aspose.Diagram.Shape shape in srcPage.Shapes)
// for the normal shape
remShapes.Add(shape.ID, shape.Name);

// iterate through the hash table
foreach (DictionaryEntry shapeEntry in remShapes)
{
long key = (long)shapeEntry.Key;
string val = (string)shapeEntry.Value;
Aspose.Diagram.Shape shape = srcPage.Shapes.GetShape(key);
// check of the shape name
if (val.Equals("GroupShape1"))
{
// move shape to the origin corner
shapeWidth = shape.XForm.Width.Value;
shapeHeight = shape.XForm.Height.Value;
shape.MoveTo(shapeWidth * 0.5, shapeHeight * 0.5);
// trim page size
srcPage.PageSheet.PageProps.PageWidth.Value = shapeWidth;
srcPage.PageSheet.PageProps.PageHeight.Value = shapeHeight;
}
else
{
// remove shape from the Visio page and hash table
srcPage.Shapes.Remove(shape);
}
}
remShapes.Clear();

// specify saving options
Aspose.Diagram.Saving.PdfSaveOptions opts = new Aspose.Diagram.Saving.PdfSaveOptions();
// set page count to save
opts.PageCount = 1;
// set starting index of the page
opts.PageIndex = 1;
// save it
srcVisio.Save("C:/temp/Output.pdf", opts);


[VB.NET Code Sample]

Dim shapeWidth As Double = 0
Dim shapeHeight As Double = 0

' load Visio diagram
Dim srcVisio As New Diagram("C:/temp/MyVisio.vsd")
' get Visio page
Dim srcPage As Aspose.Diagram.Page = srcVisio.Pages(1)
' remove background page
srcPage.BackPage = Nothing

' get hash table of shapes, it holds id and name
Dim remShapes As New Hashtable()
'Hashtable<Long, String> remShapes = new Hashtable<Long, String>();
For Each shape As Aspose.Diagram.Shape In srcPage.Shapes
' for the normal shape
remShapes.Add(shape.ID, shape.Name)
Next

' iterate through the hash table
For Each shapeEntry As DictionaryEntry In remShapes
Dim key As Long = CLng(shapeEntry.Key)
Dim val As String = DirectCast(shapeEntry.Value, String)
Dim shape As Aspose.Diagram.Shape = srcPage.Shapes.GetShape(key)
' check of the shape name
If val.Equals("GroupShape1") Then
' move shape to the origin corner
shapeWidth = shape.XForm.Width.Value
shapeHeight = shape.XForm.Height.Value
shape.MoveTo(shapeWidth * 0.5, shapeHeight * 0.5)
' trim page size
srcPage.PageSheet.PageProps.PageWidth.Value = shapeWidth
srcPage.PageSheet.PageProps.PageHeight.Value = shapeHeight
Else
' remove shape from the Visio page and hash table
srcPage.Shapes.Remove(shape)
End If
Next
remShapes.Clear()

' specify saving options
Dim opts As New Aspose.Diagram.Saving.PdfSaveOptions()
' set page count to save
opts.PageCount = 1
' set starting index of the page
opts.PageIndex = 1
' save it
srcVisio.Save("C:/temp/Output.pdf", opts)

Overview: Aspose.Diagram for .NET

Aspose.Diagram is a class library for working with MS Visio files & is a pure .NET alternate for MS Visio Object Model. It enables developers to work with VSD & VDX files on ASP.NET web applications, web services & Windows applications. It makes use of the advanced functionality of Visio's services to manipulate Visio docs on a server. Developer can open files & manipulate the elements of the diagram, from lines and fills, to more complex elements, and then export to native Visio formats or XML.

More about Aspose.Diagram for .NET

- Homepage of Aspose.Diagram for .NET: http://www.aspose.com/.net/diagram-component.aspx

-Download Aspose.Diagram for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.diagram-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts