How can I group images together in Apose.Slides
Example I have a map of South America and I want to fit images of seperate countries together to represent how they border each other. The need to have each country seperate is so I can control individual properties for each like fill color.
I really did not find those links helpful … can you give me an example of how to do this … or how can you specify the positionon the slide for an image in inches … Aspose uses a value in pixels in the code
Dear southcom,
Here, I have written a new code sample for illustrating how to add new shapes inside the existing group shape. In the source presentation srcShapes.ppt, there is one group shape which I have created by grouping two small lines, I have also set its alternative text by right clicking, selecting format shape and then selecting web tab. Then there are three pictures, I have also set there alternative text
In the code example, I find my group shape using alternative text then add all those shapes inside it, whose alternative text has also been set by me. Then I delete all shapes except group shape.
Finally I align the shapes inside the group shape vertically.
Here is the VB.NET code, source presentation and output presentation generated by this code is also attached.
---------------------------------------------------------------------------------------------------
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Aspose.Slides
Module Module1
Const BASEDIR As String = "c:\dump\"
Sub Main()
GroupingShapes()
End Sub
Sub GroupingShapes()
'The presentation that contains pictures to be grouped and a single groupShape
Dim srcPres As Presentation = New Presentation(BASEDIR & "srcShapes.ppt")
Dim srcSld As Slide = srcPres.GetSlideByPosition(1)
'Get the reference to a group shape using alternative text
Dim gpShp As GroupShape = srcSld.FindShape("MyGroupShape")
'serialize all shapes (which are to be grouped) and add them into group shape
For Each shp As Shape In srcSld.Shapes
If shp.AlternativeText = "AddInGroupShape" Then
Dim ms As MemoryStream = New MemoryStream
shp.Serialize(ms)
ms.Position = 0
gpShp.Shapes.Add(ms)
End If
Next
'Get all other shapes to be deleted
Dim shps(srcSld.Shapes.Count - 1) As Shape
Dim shpCount As Integer = 0
For Each shp As Shape In srcSld.Shapes
If shp.AlternativeText <> "MyGroupShape" Then
shps(shpCount) = shp
shpCount += 1
End If
Next
'Delete all other shapes
For i As Integer = 0 To shpCount - 1
srcSld.Shapes.Remove(shps(i))
Next
'Align the shapes vertically in groupshape
Dim prevY As Integer = 0
For Each shp As Shape In gpShp.Shapes
If shp.AlternativeText = "AddInGroupShape" Then
shp.X = 400
shp.Y = prevY
prevY += shp.Height
End If
Next
'Remove first two shapes we used to create a group shape in PowerPoint
gpShp.Shapes.RemoveAt(0)
gpShp.Shapes.RemoveAt(0)
srcPres.Write(BASEDIR & "outGroupingShapes.ppt")
End Sub
End Module
---------------------------------------------------------------------------------------------------
There are 576 pixels per inch, so multiply your inches to 576 to get number of pixels.
Now, with latest Aspose.Slides for .NET API you can group any shapes available on slide. They can be normal auto shapes, picture frames, charts, Ole frames, Video frames, Audio frames, tables and SmartArt shapes. In the following example, I have shared the code for adding different shapes on slide and how to group them together.
// Instantiate Prseetation class
using (Presentation pres = new Presentation())
{
// Get the first slide
ISlide sld = pres.Slides[0];
// Accessing the shape collection of slides
IShapeCollection slideShapes = sld.Shapes;
// Adding a group shape to the slide
IGroupShape groupShape = slideShapes.AddGroupShape();
// Adding shapes inside added group shape
groupShape.Shapes.AddAutoShape(ShapeType.Rectangle, 300, 100, 100, 100);
groupShape.Shapes.AddAutoShape(ShapeType.Rectangle, 500, 100, 100, 100);
groupShape.Shapes.AddAutoShape(ShapeType.Rectangle, 300, 300, 100, 100);
groupShape.Shapes.AddAutoShape(ShapeType.Rectangle, 500, 300, 100, 100);
// Adding group shape frame
groupShape.Frame = new ShapeFrame(100, 300, 500, 40, NullableBool.False, NullableBool.False, 0);
// Write the PPTX file to disk
pres.Save("GroupShape_out.pptx", SaveFormat.Pptx);
}