Shape Manipulations

Find Shape in Slide

This topic will describe a simple technique to make it easier for developers to find a specific shape on a slide without using its internal Id. It is important to know that PowerPoint Presentation files do not have any way to identify shapes on a slide except an internal unique Id. It seems to be difficult for developers to find a shape using its internal unique Id. All shapes added to the slides have some Alt Text. We suggest developers to use alternative text for finding a specific shape. You can use MS PowerPoint to define the alternative text for objects which you are planning to change in the future.

After setting the alternative text of any desired shape, you can then open that presentation using Aspose.Slides for C++ and iterate through all shapes added to a slide. During each iteration, you can check the alternative text of the shape and the shape with the matching alternative text would be the shape required by you. To demonstrate this technique in a better way, we have created a method, FindShape that does the trick to find a specific shape in a slide and then simply returns that shape.

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/FindShapeInSlide_out.pptx";
const String templatePath = u"../templates/FindingShapeInSlide.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
SharedPtr<IShape> shape = Aspose::Slides::Util::SlideUtil::FindShape(slide, u"Shape1");
if (shape != nullptr)
{
Console::WriteLine(u"Shape Name: " + shape->get_Name());
Console::WriteLine(u"Shape Alternative Tex: " + shape->get_AlternativeText());
}

Clone Shape

To clone a shape to a slide using Aspose.Slides for C++:

  1. Create an instance of the Presentation class.
  2. Obtain the reference of a slide by using its index.
  3. Access the source slide shape collection.
  4. Add a new slide to the presentation.
  5. Clone shapes from the source slide shape collection to the new slide.
  6. Save the modified presentation as a PPTX file.

The example below adds a group shape to a slide.

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/CloneShapes_out.pptx";
const String templatePath = u"../templates/Source Frame.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> sourceShapes = slide->get_Shapes();
SharedPtr<ILayoutSlide> blankLayout = pres->get_Masters()->idx_get(0)->get_LayoutSlides()->GetByType(SlideLayoutType::Blank);
SharedPtr<ISlide> destSlide = pres->get_Slides()->AddEmptySlide(blankLayout);
SharedPtr<IShapeCollection> destShapes = destSlide->get_Shapes();
destShapes->AddClone(sourceShapes->idx_get(1), 50, 150 + sourceShapes->idx_get(0)->get_Height());
destShapes->AddClone(sourceShapes->idx_get(2));
destShapes->InsertClone(0, sourceShapes->idx_get(0), 50, 150);
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Remove Shape

Aspose.Slides for C++ allows developers to remove any shape. To remove the shape from any slide, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Find the shape with specific AlternativeText.
  4. Remove the shape.
  5. Save file to disk.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/RemoveShape_out.pptx";
const String templatePath = u"../templates/ConnectorLineAngle.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> shapes = slide->get_Shapes();
// Now create effect "PathFootball" for existing shape from scratch.
SharedPtr<IAutoShape> autoShape1 = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 40, 150, 50);
SharedPtr<IAutoShape> autoShape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Moon, 160, 40, 150, 50);
String alttext = u"User Defined";
int iCount = slide->get_Shapes()->get_Count();
for (int i = 0; i < iCount; i++)
{
// Accessing the added shape
SharedPtr<Shape> ashape = DynamicCast<Aspose::Slides::Shape>(slide->get_Shapes()->idx_get(i));
if (String::Compare(ashape->get_AlternativeText(), alttext, StringComparison::Ordinal) == 0)
{
slide->get_Shapes()->Remove(ashape);
}
}
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Hide Shape

Aspose.Slides for C++ allows developers to hide any shape. To hide the shape from any slide, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Find the shape with specific AlternativeText.
  4. Hide the shape.
  5. Save file to disk.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/Hidingshapes_out.pptx";
const String templatePath = u"../templates/ConnectorLineAngle.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> shapes = slide->get_Shapes();
// Now create effect "PathFootball" for existing shape from scratch.
SharedPtr<IAutoShape> autoShape1 = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 40, 150, 50);
SharedPtr<IAutoShape> autoShape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Moon, 160, 40, 150, 50);
String alttext = u"User Defined";
int iCount = slide->get_Shapes()->get_Count();
for (int i = 0; i < iCount; i++)
{
// Accessing the added shape
SharedPtr<AutoShape> ashape = DynamicCast<Aspose::Slides::AutoShape>(slide->get_Shapes()->idx_get(i));
if (String::Compare(ashape->get_AlternativeText(), alttext, StringComparison::Ordinal) == 0)
{
ashape->set_Hidden(true);
}
}
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Change Shape Order

Aspose.Slides for C++ allows developers to reorder the shapes. Reordering the shape specifies which shape is on the front or which shape is at the back. To reorder the shape from any slide, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Add a shape.
  4. Add some text in shape’s text frame.
  5. Add another shape with the same co-ordinates.
  6. Reorder the shapes.
  7. Save file to disk.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/ChangeShapeOrder_out.pptx";
const String templatePath = u"../templates/HelloWorld.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Add an AutoShape of Rectangle type
SharedPtr<IAutoShape> ashp = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 150, 75, 150, 50);
ashp->get_FillFormat()->set_FillType(FillType::NoFill);
// Add TextFrame to the Rectangle
ashp->AddTextFrame(u" ");
// Accessing the text frame
SharedPtr<ITextFrame> txtFrame = ashp->get_TextFrame();
// Create the Paragraph object for text frame
SharedPtr<IParagraph> paragraph = txtFrame->get_Paragraphs()->idx_get(0);
// Create Portion object for paragraph
SharedPtr<IPortion> portion = paragraph->get_Portions()->idx_get(0);
portion->set_Text(u"Watermark Text Watermark Text Watermark Text");
//Adding another shape
SharedPtr<IAutoShape> ashape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Triangle, 200, 365, 400, 150);
//Reorder shape
slide->get_Shapes()->Reorder(2, ashape2);
// Save PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Get Interop Shape ID

Aspose.Slides for C++ allows developers to get a unique shape identifier in slide scope in contrast to the UniqueId property, which allows obtaining a unique identifier in presentation scope. Property OfficeInteropShapeId was added to IShape interfaces and Shape class respectively. The value returned by OfficeInteropShapeId property corresponds to the value of the Id of the Microsoft.Office.Interop.PowerPoint.Shape object. Below is the sample code is given.

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/FindShapeInSlide_out.pptx";
const String templatePath = u"../templates/FindingShapeInSlide.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
long officeInteropShapeId = 0;
// Getting unique shape identifier in slide scope
// officeInteropShapeId = pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0)->get_OfficeInteropShapeId();
Console::WriteLine(u"Office Interop Shape ID: " + officeInteropShapeId);

Set AlternativeText Property

Aspose.Slides for C++ allows developers to set AlternateText of any shape. To set the AlternateText of a shape, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Access the first slide.
  3. Add any shape to the slide.
  4. Do some work with the newly added shape.
  5. Traverse through shapes to find a shape.
  6. Set the AlternativeText.
  7. Save file to disk.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/SetAlternativeText_out.pptx";
const String templatePath = u"../templates/ConnectorLineAngle.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Accessing shapes collection for selected slide
SharedPtr<IShapeCollection> shapes = slide->get_Shapes();
// Now create effect "PathFootball" for existing shape from scratch.
SharedPtr<IAutoShape> autoShape1 = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 40, 150, 50);
SharedPtr<IAutoShape> autoShape2 = slide->get_Shapes()->AddAutoShape(ShapeType::Moon, 160, 40, 150, 50);
String alttext = u"User Defined";
int iCount = slide->get_Shapes()->get_Count();
for (int i = 0; i < iCount; i++)
{
// Accessing the added shape
SharedPtr<IShape> shape = slide->get_Shapes()->idx_get(i);
SharedPtr<AutoShape> ashape = DynamicCast<Aspose::Slides::AutoShape>(shape);
if (ashape != nullptr)
{
ashape->set_AlternativeText (u"User Defined " +(i+1));
}
}
//Write the PPTX to Disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Access Layout Formats for Shape

Aspose.Slides for C++ allows developers to access layout formats for a shape. This article demonstrates how you can access FillFormat and LineFormat properties for a shape.

Below is the sample code is given.

// The path to the documents directory.
const String outPath = u"../templates/pres.pptx";
//Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
for (int x = 0; x < pres->get_LayoutSlides()->get_Count(); x++)
{
SharedPtr<IShapeCollection> shapeCollection = pres->get_LayoutSlides()->idx_get(x)->get_Shapes();
for (int i = 0; i < shapeCollection->get_Count(); i++) {
SharedPtr<IShape> shape = shapeCollection->idx_get(i);
System::Console::WriteLine(shape->get_FillFormat());
}
for (int j = 0; j < shapeCollection->get_Count(); j++) {
SharedPtr<IShape> shape = shapeCollection->idx_get(j);
System::Console::WriteLine(shape->get_LineFormat());
}
}

Render Shape as SVG

Now Aspose.Slides for C++ support for rendering a shape as svg. WriteAsSvg method (and its overload) has been added to Shape class and IShape interface. This method allows to save content of the shape as an SVG file. Code snippet below shows how to export slide’s shape to an SVG file.

String outSvgFileName = u"SingleShape.svg";

auto pres = System::MakeObject<Presentation>(u"TestExportShapeToSvg.pptx");

auto stream = System::MakeObject<FileStream>(outSvgFileName, FileMode::Create, FileAccess::Write);
pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0)->WriteAsSvg(stream);

Shapes Alignment

Aspose.Slides allows to align shapes either relative to the slide margins or relative to each other. For this purpose, an overloaded SlidesUtil.AlignShapes() method has been added. The ShapesAlignmentType enumeration defines possible alignment options.

Example 1

Source code below aligns shapes with indices 1, 2 and 4 along the top border of the slide.

SharedPtr<Presentation> pres = System::MakeObject<Presentation>(u"example.pptx");

SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
SharedPtr<IShape> shape1 = slide->get_Shapes()->idx_get(1);
SharedPtr<IShape> shape2 = slide->get_Shapes()->idx_get(2);
SharedPtr<IShape> shape3 = slide->get_Shapes()->idx_get(4);
SlideUtil::AlignShapes(ShapesAlignmentType::AlignTop, true, pres->get_Slides()->idx_get(0), 
System::MakeArray<int32_t>(
    {
        slide->get_Shapes()->IndexOf(shape1),
        slide->get_Shapes()->IndexOf(shape2),
        slide->get_Shapes()->IndexOf(shape3)
    }));

Example 2

The example below shows how to align the entire collection of shapes relative to the very bottom shape in the collection.

SharedPtr<Presentation> pres = MakeObject<Presentation>(u"example.pptx");
SlideUtil::AlignShapes(ShapesAlignmentType::AlignBottom, false, pres->get_Slides()->idx_get(0)->get_Shapes());