We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy shape from one Visio file to another visio file


I have a .net web application spitting out a visio file.

My requirement is that I have a template visio file that i use to construct a new visio diagram. Based on users input i chose which pages in the template file should be present in the new one. So basically i need to copy all the shapes from one page in template file and paste it in a new page in another visio file. Is this something that can be done using Aspose.Diagram?

Need to know this Asap please!
Hi Swetha,

Well, you can load a Visio diagram in the first Diagram class object, reference its page to the second Diagram class object by calling PageCollection.add method, modify the page content of second Diagram class object, remove the first empty page of second Diagram class object and then save it in the supported file format. You don't need to save the original diagram because you have modified it. Please check this sample code:

[.NET, C#]
// The path to the documents directory. string dataDir = "C:\\temp\\";
// Call the diagram constructor to load diagram from a VSD file Diagram originalDiagram = new Diagram(dataDir + "Drawing1.vsd");
// initialize the new visio diagram Diagram temp1 = new Diagram();

// add all masters from the source Visio diagram foreach(Master master in originalDiagram.Masters) temp1.AddMaster(originalDiagram, master.Name);

// get the page object from the original diagram Aspose.Diagram.Page SrcPage = originalDiagram.Pages.GetPage("Page-1"); // set page name SrcPage.Name = "new page"; int MaxTemp1PageId = GetMaxPageID(temp1); // set page id SrcPage.ID = MaxTemp1PageId; // add reference of the original diagram page temp1.Pages.Add(SrcPage);
// remove first empty page temp1.Pages.Remove(temp1.Pages[0]);
// Get an element from the first cloned page and color it with Red Shape circleShape = temp1.Pages.GetPage("new page").Shapes[2]; circleShape.Line.LineColor.Value = "FF0000"; circleShape.Line.LineWeight.Value = 0.03;
// save diagram in PNG format temp1.Save(dataDir + "Temp1.vdx", SaveFileFormat.VDX);

private static int GetMaxPageID(Diagram diagram) { int max = diagram.Pages[0].ID; for (int i = 1; i < diagram.Pages.Count; i++) { if (max < diagram.Pages[i].ID) max = diagram.Pages[i].ID; } return max; }

We hope, this helps. Please let us know in case of any confusion or questions.

This works if I’m just copying to a blank page. But what if I have to connect the selected shapes from one visio file and auto connect to shapes that are present in a different visio file (Ex: temp1 has a page that has few shapes already and i want to autoconnect the copied shapes and paste them)?

Hi Swetha,

Thank you for the details. Please provide us your complete scenario details along with source Visio diagram and resultant diagram. We’ll check and write the sample code for your convenience.

Hi Imran,

I basically want to
1. Create a new page in the Finaldocument.vsd
2. Copy/paste shapes from one page of the template.vsd into the new page that is created above
3. Copy/paste shapes from another page in the template and connect them to the last shape of whatever that was pasted in the 2nd step.
So far I was able to go as far as 2nd step as it is just adding shapes to a blank page that is newly created. I appreciate some help to get #3 done. I’m zipping up my files for your reference.
Also , my web application runs on .net framework 4.5. So, does Aspose.Diagram work for that?

Hi Swetha,

Thank you for providing the source Visio diagrams. We’re working over your query and will get back to you soon.

Hi Swetha,

Thank you for being patient. Please use the sample code below:

[.NET, C#]
// load Visio diagram
Diagram finalDrawing = new Diagram(@“C:\AsposeSpike\FinalVisioDocument.vsd”);
// Get max page ID
int MaxPageId = GetMaxPageID(finalDrawing);

// Initialize a new page object
Aspose.Diagram.Page newPage = new Aspose.Diagram.Page();
// Set name
newPage.Name = “new page”;
// Set page ID
newPage.ID = MaxPageId + 1;
// add page

// load Visio diagram
Diagram templateDrawing = new Diagram(@“C:\AsposeSpike\Template.vsd”);
// get page
Aspose.Diagram.Page page1 = templateDrawing.Pages.GetPage(“Page Name 1”);
// get shape by index
Shape shape1 = page1.Shapes.GetShape(11);
// set shape name
shape1.Name = “New shape 1”;
// set shape id
shape1.ID = GetMaxShapeID(page1);
// get page
Aspose.Diagram.Page page2 = templateDrawing.Pages.GetPage(“Page Name 2”);
// get shape by index
Shape shape2 = page2.Shapes.GetShape(5);
// set shape name
shape2.Name = “New shape 2”;
// set shape id
shape2.ID = GetMaxShapeID(page2);

// add shapes
finalDrawing.Pages.GetPage(“new page”).Shapes.Add(shape1);
finalDrawing.Pages.GetPage(“new page”).Shapes.Add(shape2);

// get page index of the new page
int pageIndex = 0;
for (int index = 0; index < finalDrawing.Pages.Count; index++ )
if (finalDrawing.Pages[index].Name == “new page”)
pageIndex = index;

// connect both shapes
Shape connector1 = new Shape();
long connecter1Id = finalDrawing.AddShape(connector1, “Dynamic connector”, pageIndex);
finalDrawing.Pages[0].ConnectShapesViaConnector(shape1.ID, ConnectionPointPlace.Left, shape2.ID, ConnectionPointPlace.Right, connecter1Id);

// Save diagram
finalDrawing.Save(@“C:\AsposeSpike\Output.vdx”, SaveFileFormat.VDX);

private static int GetMaxPageID(Diagram diagram) { int max = diagram.Pages[0].ID; for (int i = 1; i < diagram.Pages.Count; i++) { if (max < diagram.Pages[i].ID) max = diagram.Pages[i].ID; } return max; }
private static long GetMaxShapeID(Aspose.Diagram.Page page) { long max = page.Shapes[0].ID;
for (int i = 1; i < page.Shapes.Count; i++) { if (max < page.Shapes[i].ID) max = page.Shapes[i].ID; } return max; }

We hope, this helps. Please let us know in case of any ambiguity or questions.

Hi Imram,

Thanks for the reply. But I’m not looking to copy just one shape. It is a group of shapes together that I want to copy/paste. If you look at the source diagrams that I provided, there are group of shapes in each page that I needed to Copy and Paste them into a new page in finalDrawing.
So i’m looking for something similar to Selection object or Group objects to work with.
Sorry for any confusion.

Hi Swetha,

Thank you for the inquiry. Please note, the available shapes in the template drawing are not marked as a group shape. Otherwise, the Add method of ShapeColllection class allows to place a group shape on the new page of the resulting diagram. There is no way to group shapes together to move them all at once using Aspose.Diagram library. We have already logged this feature request under ticket id DIAGRAMNET-50072 and the good news for you is that it has been marked as resolved. If there is no issue in the QA phase, then this fix will be included in the next version of Aspose.Diagram for .NET 5.8.0. We’ll inform you via this forum thread as soon as the new release is published.

The issues you have found earlier (filed as DIAGRAMNET-50072) have been fixed in Aspose.Diagram for .NET 5.8.0.

This message was posted using Notification2Forum from Downloads module by Aspose Notifier.