article

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

Email
Submitted on: 2/3/2016 8:18:51 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 1012
 
     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 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 remShapes = new Hashtable();
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 remShapes = new Hashtable();
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.


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.