形状操作
在幻灯片中查找形状
本主题将描述一种简单的技术,以帮助开发人员无需使用内部 Id 即可找到幻灯片上的特定形状。重要的是要知道,PowerPoint 演示文稿文件没有其他方法来识别幻灯片上的形状,除了内部唯一 Id。开发人员似乎很难使用其内部唯一 Id 找到形状。所有添加到幻灯片的形状都有一些替代文本。我们建议开发人员使用替代文本来查找特定形状。您可以使用 MS PowerPoint 为您计划在未来更改的对象定义替代文本。
在设置任何所需形状的替代文本后,您可以使用 Aspose.Slides for C++ 打开该演示文稿并遍历添加到幻灯片的所有形状。在每次迭代中,您可以检查形状的替代文本,具有匹配替代文本的形状将是您所需的形状。为了更好地演示这种技术,我们创建了一个方法,FindShape,用于查找幻灯片中的特定形状,然后简简单单返回该形状。
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()); | |
} | |
克隆形状
要使用 Aspose.Slides for C++ 将形状克隆到幻灯片:
- 创建 Presentation 类的实例。
- 使用其索引获取幻灯片的引用。
- 访问源幻灯片的形状集合。
- 向演示文稿添加新幻灯片。
- 从源幻灯片形状集合克隆形状到新幻灯片。
- 将修改后的演示文稿保存为 PPTX 文件。
下面的示例将组形状添加到幻灯片中。
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); | |
删除形状
Aspose.Slides for C++ 允许开发人员删除任何形状。要从任何幻灯片中删除形状,请遵循以下步骤:
- 创建 Presentation 类的实例。
- 访问第一张幻灯片。
- 找到具有特定替代文本的形状。
- 删除该形状。
- 将文件保存到磁盘。
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); | |
隐藏形状
Aspose.Slides for C++ 允许开发人员隐藏任何形状。要隐藏任何幻灯片中的形状,请遵循以下步骤:
- 创建 Presentation 类的实例。
- 访问第一张幻灯片。
- 找到具有特定替代文本的形状。
- 隐藏该形状。
- 将文件保存到磁盘。
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); | |
更改形状顺序
Aspose.Slides for C++ 允许开发人员重新排列形状。重新排列形状指定哪个形状在前面或哪个形状在后面。要从任何幻灯片中重新排列形状,请遵循以下步骤:
- 创建 Presentation 类的实例。
- 访问第一张幻灯片。
- 添加一个形状。
- 在形状的文本框中添加一些文本。
- 使用相同的坐标添加另一个形状。
- 重新排列形状。
- 将文件保存到磁盘。
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); | |
获取互操作形状 ID
Aspose.Slides for C++ 允许开发人员在幻灯片范围内获取唯一形状标识符,与允许在演示文稿范围内获取唯一标识符的 UniqueId 属性相反。 OfficeInteropShapeId 属性已分别添加到 IShape 接口和 Shape 类中。 OfficeInteropShapeId 属性返回的值对应于 Microsoft.Office.Interop.PowerPoint.Shape 对象的 Id 的值。下面给出了示例代码。
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); | |
设置替代文本属性
Aspose.Slides for C++ 允许开发人员设置任何形状的替代文本。要设置形状的替代文本,请遵循以下步骤:
- 创建 Presentation 类的实例。
- 访问第一张幻灯片。
- 向幻灯片添加任何形状。
- 对新添加的形状进行一些操作。
- 遍历形状以查找形状。
- 设置替代文本。
- 将文件保存到磁盘。
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); | |
访问形状的布局格式
Aspose.Slides for C++ 允许开发人员访问形状的布局格式。 本文演示了如何访问形状的 FillFormat 和 LineFormat 属性。
下面给出了示例代码。
// 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()); | |
} | |
} | |
将形状渲染为 SVG
现在 Aspose.Slides for C++ 支持将形状渲染为 SVG。 WriteAsSvg 方法(及其重载)已添加到 Shape 类和 IShape 接口中。 此方法允许将形状的内容保存为 SVG 文件。 下面的代码片段显示了如何将幻灯片的形状导出为 SVG 文件。
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);
形状对齐
Aspose.Slides 允许按幻灯片边距或相互之间对齐形状。为此,添加了重载的 SlidesUtil.AlignShapes() 方法。 ShapesAlignmentType 枚举定义了可能的对齐选项。
示例 1
下面的源代码将索引 1、2 和 4 的形状沿幻灯片的上边界对齐。
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)
}));
示例 2
下面的示例演示了如何将整个形状集合相对于集合中的最后一个形状进行对齐。
SharedPtr<Presentation> pres = MakeObject<Presentation>(u"example.pptx");
SlideUtil::AlignShapes(ShapesAlignmentType::AlignBottom, false, pres->get_Slides()->idx_get(0)->get_Shapes());