Other than providing different APIs for adding different kind of shapes in slides, Aspose.Slides supports a workaround to add new shapes in slides using Serialization and in this technical tip, we are going to show you how. We will carry out the following steps.

- Create some shape using MS-PowerPoint and save the presentation.
- Read the presentation and its shape using Aspose.Slides.
- Serialize the shape.
- Create a new presentation and add the serialized shape in it.

Code Example

[C#]

//Read the source presentation
Presentation srcPres = new Presentation("c:\\source.ppt");

//Read the shape
Slide srcSld = srcPres.GetSlideByPosition(1);
Shape srcShape = srcSld.Shapes[0];

//Serialize the shape
MemoryStream ms = new MemoryStream();
srcShape.Serialize(ms);

//Create a target presentation
Presentation targetPres = new Presentation();

//Add a serialized shape inside the target presentation
Slide targetSld = targetPres.GetSlideByPosition(1);
ms.Position = 0;
targetSld.Shapes.Add(ms);

//Write the target presentation on disk
targetPres.Write("c:\\target.ppt");

[VB.NET]

'Read the source presentation
Dim srcPres As Presentation = New Presentation("c:\source.ppt")

'Read the shape
Dim srcSld As Slide = srcPres.GetSlideByPosition(1)
Dim srcShape As Shape = srcSld.Shapes(0)

'Serialize the shape
Dim ms As MemoryStream = New MemoryStream()
srcShape.Serialize(ms)

'Create a target presentation
Dim targetPres As Presentation = New Presentation()

'Add a serialized shape inside the target presentation
Dim targetSld As Slide = targetPres.GetSlideByPosition(1)
ms.Position = 0
targetSld.Shapes.Add(ms)

'Write the target presentation on disk
targetPres.Write("c:\target.ppt")

[Java]

//Read the source presentation
Presentation srcPres = new Presentation(new FileInputStream("c:\\source.ppt"));

//Read the shape
Slide srcSld = srcPres.getSlideByPosition(1);
com.aspose.slides.Shape srcShape = srcSld.getShapes().get(0);

//Serialize the shape
ByteArrayOutputStream bout=new ByteArrayOutputStream();
srcShape.serialize(bout);

//Create a target presentation
Presentation targetPres = new Presentation();

//Add a serialized shape inside the target presentation
Slide targetSld = targetPres.getSlideByPosition(1);
ByteArrayInputStream bin=new ByteArrayInputStream(bout.toByteArray());
targetSld.getShapes().add(bin);

//Write the target presentation on disk
targetPres.write(new FileOutputStream("c:\\target.ppt"));

More about Aspose.Slides

- Homepage of Aspose.Slides.
- Read more technical tips by Aspose.Slides.

Contact Information
Suite 119, 272 Victoria Avenue
Chatswood, NSW, 2067
Australia
Aspose - The .NET and Java component publisher
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.94651