使用 C++ 在 PowerPoint 演示文稿中處理形狀

Microsoft PowerPoint 使您能夠向演示文稿添加形狀。形狀在顯示數據流或顯示流程的不同階段等場景中很有用。您可以使用橢圓、直線、矩形等形狀,並使用連接器將它們連接起來。您可能會發現自己處於必須以編程方式向 PowerPoint 幻燈片添加形狀的場景中。為此,本文將教您如何使用 C++ 在 PowerPoint 演示文稿中處理形狀。

用於在 PowerPoint 演示文稿中處理形狀的 C++ API

Aspose.Slides for C++ 是一個原生 C++ 庫,支持創建、讀取和操作 PowerPoint 文件。 API 還支持在 PowerPoint 演示文稿中使用形狀。您可以通過 NuGet 安裝 API 或直接從 下載 部分下載。

PM> Install-Package Aspose.Slides.Cpp

向 PowerPoint 幻燈片添加形狀

要添加形狀,請使用 API 提供的 ISlide->getShapes()->AddAutoShape() 方法。以下是將形狀添加到 PowerPoint 幻燈片的步驟。

以下是使用 C++ 向 PowerPoint 幻燈片添加形狀的示例代碼。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\SamplePresentation4.pptx";
const String outputFilePath = u"OutputDirectory\\AddShapePresentation.pptx";

// 加載演示文稿文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 獲取第一張幻燈片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 添加形狀
SharedPtr<IAutoShape> ellipse = slide->get_Shapes()->AddAutoShape(ShapeType::Ellipse, 50, 150, 150, 50);

// 保存演示文件
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
將形狀添加到 PowerPoint 幻燈片

將連接的形狀添加到 PowerPoint 幻燈片

連接器可用於連接形狀。為了創建連接器,您可以使用 ISlide->getShapes()->AddConnector() 方法。以下是將連接的形狀添加到 PowerPoint 幻燈片的步驟。

以下是使用 C++ 將連接的形狀添加到 PowerPoint 幻燈片的示例代碼。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\SamplePresentation4.pptx";
const String outputFilePath = u"OutputDirectory\\AddConnectedShapesPresentation.pptx";

// 加載演示文稿文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 獲取第一張幻燈片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 添加第一個形狀
SharedPtr<IAutoShape> ellipse = slide->get_Shapes()->AddAutoShape(ShapeType::Ellipse, 50, 150, 150, 50);

// 添加第二個形狀
SharedPtr<IAutoShape> rectangle = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 100, 300, 100, 100);

// 添加連接器
SharedPtr<IConnector> connector = slide->get_Shapes()->AddConnector(ShapeType::BentConnector2, 0, 0, 10, 10);

// 使用連接器連接形狀
connector->set_StartShapeConnectedTo(ellipse);
connector->set_EndShapeConnectedTo(rectangle);

// 調用 reroute 設置形狀之間的自動最短路徑
connector->Reroute();

// 保存演示文件
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
將連接的形狀添加到 PowerPoint 幻燈片

在 PowerPoint 幻燈片中克隆形狀

您還可以使用 Aspose.Slides for C++ API 克隆現有形狀。要克隆形狀,請使用 API 提供的 ShapeCollection->InsertClone() 方法。以下是將形狀從一張幻燈片克隆到另一張幻燈片的步驟。

以下是使用 C++ 在 PowerPoint 幻燈片中克隆形狀的示例代碼。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\ShapePresentation2.pptx";
const String outputFilePath = u"OutputDirectory\\CloneShapePresentation.pptx";

// 加載演示文稿文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 訪問第一張幻燈片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

// 訪問所選幻燈片的形狀集合
SharedPtr<IShapeCollection> sourceShapes = slide->get_Shapes();

// 從目標幻燈片獲取形狀集合
SharedPtr<ISlide> destSlide = presentation->get_Slides()->idx_get(1);
SharedPtr<IShapeCollection> destShapes = destSlide->get_Shapes();

// 克隆形狀
destShapes->InsertClone(0, sourceShapes->idx_get(1), 50, 150);

// 保存演示文件
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
在 PowerPoint 幻燈片中克隆形狀

使用 C++ 從 PowerPoint 幻燈片中刪除形狀

以下是從 PowerPoint 幻燈片中刪除形狀的步驟。

以下是使用 C++ 從 PowerPoint 幻燈片中刪除形狀的示例代碼。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\ShapePresentation.pptx";
const String outputFilePath = u"OutputDirectory\\RemoveShapePresentation.pptx";

// 加載演示文稿文件
SharedPtr<Presentation> presentation = MakeObject<Presentation>(sourceFilePath);

// 訪問第一張幻燈片
SharedPtr<ISlide> slide = presentation->get_Slides()->idx_get(0);

String alttext = u"User Defined";

int iCount = slide->get_Shapes()->get_Count();

for (int i = 0; i < iCount; i++)
{
	// 訪問形狀
	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);
	}
}

// 保存演示文件
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);

支持的 PowerPoint 形狀

Aspose.Slides for C++ 支持多種形狀供您使用。以下是一些受支持形狀的列表。

您可以通過查看 ShapeType 枚舉值來查看支持的形狀的完整列表。

獲得免費許可證

您可以申請 免費的臨時許可證 來試用沒有評估限制的 API。

結論

在本文中,您了解瞭如何使用 C++ 在 PowerPoint 演示文稿中處理形狀。具體來說,您已經學習瞭如何在 PowerPoint 幻燈片中添加、克隆和刪除形狀。此外,您還了解瞭如何使用連接器連接形狀。除了處理形狀外,Aspose.Slides for C++ 還提供了許多額外的功能來增強您的 PowerPoint 演示文稿。您可以通過訪問 官方文檔 來詳細探索 API。如有任何疑問,請隨時通過我們的免費支持論壇與我們聯繫。

也可以看看