克隆幻灯片
在演示文稿中克隆幻灯片
克隆是制作某物精确副本或复制的过程。Aspose.Slides for C++ 也使得可以复制或克隆任何幻灯片,然后将该克隆幻灯片插入到当前或任何其他打开的演示文稿中。幻灯片克隆的过程会创建一个新幻灯片,开发人员可以修改而不改变原始幻灯片。有几个可能的方式来克隆幻灯片:
- 在演示文稿末尾克隆。
- 在演示文稿中的其他位置克隆。
- 在另一个演示文稿的末尾克隆。
- 在另一个演示文稿中的其他位置克隆。
- 在另一个演示文稿中的特定位置克隆。
在 Aspose.Slides for C++ 中,由 Presentation 对象暴露的一组 ISlide 对象提供了 AddClone 和 InsertClone 方法来执行上述类型的幻灯片克隆。
在演示文稿末尾克隆
如果您想克隆一张幻灯片并将其用在同一演示文稿文件中现有幻灯片的末尾,请按照以下步骤使用 AddClone 方法:
- 创建 Presentation 类的实例。
- 通过引用 Presentation 对象暴露的幻灯片集合实例化 ISlideCollection 类。
- 调用 ISlideCollection 对象暴露的 AddClone 方法,并将要克隆的幻灯片作为参数传递给 AddClone 方法。
- 写入修改后的演示文稿文件。
在下面的示例中,我们将一张位于演示文稿第一个位置(零索引)的幻灯片克隆到演示文稿的末尾。
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 templatePath = u"../templates/AddSlides.pptx"; | |
const String outPath = u"../out/CloneWithinSamePresentationToEnd.pptx"; | |
// Instantiate Presentation class | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
// Accessing Slide by ID from collection | |
SharedPtr<ISlideCollection> slides = pres->get_Slides(); | |
// Clone the desired slide at end of same presentation | |
slides->AddClone(pres->get_Slides()->idx_get(0)); | |
// Writing the presentation file | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
在演示文稿中的其他位置克隆
如果您想克隆一张幻灯片并将其用在同一演示文稿文件但在不同位置,请使用 InsertClone 方法:
- 创建 Presentation 类的实例。
- 通过引用 Presentation 对象暴露的 Slides 集合实例化该类。
- 调用 ISlideCollection 对象暴露的 InsertClone 方法,并将要克隆的幻灯片以及新位置的索引作为参数传递给 InsertClone 方法。
- 将修改后的演示文稿作为 PPTX 文件写入。
在下面的示例中,我们将一张位于零索引(位置 1)的幻灯片克隆到演示文稿的索引 1(位置 2)。
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 templatePath = u"../templates/AddSlides.pptx"; | |
const String outPath = u"../out/CloneWithInSamePresentation.pptx"; | |
// Instantiate Presentation class | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
// Accessing Slide by ID from collection | |
SharedPtr<ISlideCollection> slides = pres->get_Slides(); | |
// Clone the desired slide to the specified index in the same presentation | |
slides->InsertClone(2, pres->get_Slides()->idx_get(0)); | |
// Writing the presentation file | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
将幻灯片克隆到另一个演示文稿的末尾
如果您需要从一个演示文稿克隆一张幻灯片并在另一个演示文稿文件中的现有幻灯片末尾使用它:
- 创建包含将要克隆幻灯片的源演示文稿的 Presentation 类的实例。
- 创建包含目标演示文稿的 Presentation 类的实例,该演示文稿将添加幻灯片。
- 通过引用目标演示文稿的 Slides 集合实例化 ISlideCollection 类。
- 调用 ISlideCollection 对象暴露的 AddClone 方法,并将源演示文稿中的幻灯片作为参数传递给 AddClone 方法。
- 写入修改后的目标演示文稿文件。
在下面的示例中,我们将一张来自源演示文稿第一个索引的幻灯片克隆到目标演示文稿的末尾。
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 templatePath = u"../templates/AddSlides.pptx"; | |
const String outPath = u"../out/CloneAtEndOfAnotherPresentation.pptx"; | |
// Instantiate Presentation class | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
//Instantiate target presentation object | |
SharedPtr<Presentation> destPres = MakeObject<Presentation>(); | |
// Accessing Slide by ID from collection | |
SharedPtr<ISlideCollection> slideCollection = destPres->get_Slides(); | |
// Clone the desired slide at end of other presentation | |
slideCollection->AddClone(pres->get_Slides()->idx_get(0)); | |
// Writing the presentation file | |
destPres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
将幻灯片克隆到另一个演示文稿的其他位置
如果您需要从一个演示文稿克隆一张幻灯片并在另一个演示文稿文件中特定位置使用它:
- 创建包含要克隆幻灯片的源演示文稿的 Presentation 类的实例。
- 创建包含将要添加幻灯片的目标演示文稿的 Presentation 类的实例。
- 通过引用目标演示文稿的 Slides 集合实例化 ISlideCollection 类。
- 调用 ISlideCollection 对象暴露的 InsertClone 方法,并将源演示文稿中的幻灯片及所需位置作为参数传递给 InsertClone 方法。
- 写入修改后的目标演示文稿文件。
在下面的示例中,我们将一张源演示文稿中的幻灯片(零索引)克隆到目标演示文稿的索引 1(位置 2)。
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 templatePath = u"../templates/AddSlides.pptx"; | |
const String outPath = u"../out/CloneAtEndOfAnotherPresentation.pptx"; | |
// Instantiate Presentation class | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
//Instantiate target presentation object | |
SharedPtr<Presentation> destPres = MakeObject<Presentation>(); | |
// Accessing Slide by ID from collection | |
SharedPtr<ISlideCollection> slideCollection = destPres->get_Slides(); | |
// Clone the desired slide at end of other presentation | |
slideCollection->AddClone(pres->get_Slides()->idx_get(0)); | |
// Writing the presentation file | |
destPres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
在另一个演示文稿的特定位置克隆幻灯片
如果您需要从一个演示文稿克隆带母版幻灯片的幻灯片并在另一个演示文稿中使用它,您需要先将所需的母版幻灯片从源演示文稿克隆到目标演示文稿。然后,您需要使用该母版幻灯片来克隆带母版的幻灯片。AddClone(ISlide, IMasterSlide) 期望的是来自目标演示文稿的母版幻灯片,而不是来自源演示文稿的。为了克隆带母版的幻灯片,请按照以下步骤操作:
- 创建包含将要克隆幻灯片的源演示文稿的 Presentation 类的实例。
- 创建包含将要克隆幻灯片的目标演示文稿的 Presentation 类的实例。
- 访问要克隆的幻灯片及其母版幻灯片。
- 通过引用目标演示文稿的母版集合实例化 IMasterSlideCollection 类。
- 调用 IMasterSlideCollection 对象暴露的 AddClone 方法,并将源 PPTX 中要克隆的母版作为参数传递给 AddClone 方法。
- 通过设置对目标演示文稿的 Slides 集合的引用实例化 ISlideCollection 类。
- 调用 ISlideCollection 对象暴露的 AddClone 方法,并将源演示文稿中的幻灯片和母版作为参数传递给 AddClone 方法。
- 写入修改后的目标演示文稿文件。
在下面的示例中,我们将带母版的幻灯片(位于源演示文稿的零索引)克隆到目标演示文稿的末尾,使用源幻灯片的母版。
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 templatePath = u"../templates/AddSlides.pptx"; | |
const String outPath = u"../out/CloneToAnotherPresentationWithMaster.pptx"; | |
// Instantiate Presentation class | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
//Instantiate target presentation object | |
SharedPtr<Presentation> destPres = MakeObject<Presentation>(); | |
// Accessing Slide by ID from collection | |
SharedPtr<ISlideCollection> slideCollection = destPres->get_Slides(); | |
// Instantiate ISlide from the collection of slides in source presentation along with | |
// Master slide | |
SharedPtr<ISlide> SourceSlide = pres->get_Slides()->idx_get(0); | |
SharedPtr<IMasterSlide> SourceMaster = SourceSlide->get_LayoutSlide()->get_MasterSlide(); | |
// Clone the desired master slide from the source presentation to the collection of masters in the | |
// Destination presentation | |
SharedPtr<IMasterSlideCollection> masters = destPres->get_Masters(); | |
SharedPtr<IMasterSlide> DestMaster = SourceSlide->get_LayoutSlide()->get_MasterSlide(); | |
// Clone the desired master slide from the source presentation to the collection of masters in the | |
// Destination presentation | |
SharedPtr<IMasterSlide> iSlide = masters->AddClone(SourceMaster); | |
// Clone the desired slide from the source presentation with the desired master to the end of the | |
// Collection of slides in the destination presentation | |
slideCollection->AddClone(SourceSlide, iSlide, true); | |
// Clone the desired slide at end of other presentation | |
slideCollection->InsertClone(1, pres->get_Slides()->idx_get(0)); | |
// Writing the presentation file | |
destPres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
将幻灯片克隆到指定部分
如果您想克隆一张幻灯片并在同一演示文稿文件中的不同部分使用它,请使用 AddClone() 方法, 该方法由 ISlideCollection 接口暴露。Aspose.Slides for C++ 使得可以从第一部分克隆幻灯片,然后将该克隆幻灯片插入到同一演示文稿的第二部分。
以下代码片段展示了如何克隆一张幻灯片并将克隆的幻灯片插入到指定部分。
const String outPath = u"../out/CloneSlideIntoSpecifiedSection.pptx"; | |
auto presentation = MakeObject<Presentation>(); | |
presentation->get_Slides()->idx_get(0)->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 200.0f, 50.0f, 300.0f, 100.0f); | |
presentation->get_Sections()->AddSection(u"Section 1", presentation->get_Slides()->idx_get(0)); | |
auto section2 = presentation->get_Sections()->AppendEmptySection(u"Section 2"); | |
presentation->get_Slides()->AddClone(presentation->get_Slides()->idx_get(0), section2); | |
presentation->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |