SmartArt 用於增強 PowerPoint 演示文稿的體驗並直觀地呈現數據。它可用於使文本更加突出和吸引人,也可用於顯示流程圖、過程、關係圖等。在本文中,您將學習如何使用 C++ 在 PowerPoint 演示文稿中創建 SmartArt。

用於在 PowerPoint 演示文稿中創建 SmartArt 的 C++ API

Aspose.Slides for C++ 是一個用於處理 PowerPoint 文件的 C++ API。它使您能夠創建、讀取和修改 PPT 和 PPTX 文件,而無需額外的軟件。此外,API 支持在 PowerPoint 演示文稿中創建 SmartArt。您可以通過 NuGet 安裝 API 或直接從 下載 部分下載。

PM> Install-Package Aspose.Slides.Cpp

使用 C++ 在 PowerPoint 中創建 SmartArt 形狀

以下是在 PowerPoint 演示文稿中創建 SmartArt 形狀的步驟。

以下示例代碼演示如何使用 C++ 在 PowerPoint 中創建 SmartArt 形狀。

// 文件路徑
const String outputFilePath = u"OutputDirectory\\CreateSmartArt_out.pptx";

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

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

// 添加 SmartArt
auto smartArt = slide->get_Shapes()->AddSmartArt(0, 0, 400, 400, Aspose::Slides::SmartArt::SmartArtLayoutType::BasicBlockList);

smartArt->get_AllNodes()->idx_get(0)->get_TextFrame()->set_Text(u"First Block");
smartArt->get_AllNodes()->idx_get(1)->get_TextFrame()->set_Text(u"Second Block");

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
使用 C++ 在 PowerPoint 演示文稿中創建 SmartArt

示例代碼生成的輸出圖像

在 PowerPoint 演示文稿中訪問 SmartArt 形狀

您可以按照以下步驟訪問 PowerPoint 文件中的 SmartArt 形狀。

以下示例代碼演示如何使用 C++ 從 PowerPoint 演示文稿訪問 SmartArt 形狀。

// 文件路徑
const String sourceFilePath = u"OutputDirectory\\CreateSmartArt_out.pptx";

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

// 遍歷形狀
for (auto shape : presentation->get_Slides()->idx_get(0)->get_Shapes())
{
	// 檢查形狀是否為 SmartArt 類型
	if (System::ObjectExt::Is<Aspose::Slides::SmartArt::SmartArt>(shape))
	{
		// 將形狀類型轉換為 SmartArt
		auto smartArt = System::DynamicCast_noexcept<ISmartArt>(shape);

		Console::WriteLine(String::Format(u"Shape Name: {0}", smartArt->get_Name()));

		// 檢查 SmartArt 佈局
		/*if (smartArt->get_Layout() == SmartArtLayoutType::BasicBlockList)
		{
			Console::WriteLine(u"Do some thing here....");
		}*/
	}
}

使用 C++ 更改 SmartArt 形狀的樣式

您可以在訪問後輕鬆更改 SmartArt 形狀的樣式。以下是使用 C++ 更改 SmartArt 形狀樣式的步驟。

下面的示例代碼演示如何使用 C++ 更改 SmartArt 形狀的樣式。

// 文件路徑
const String sourceFilePath = u"OutputDirectory\\CreateSmartArt_out.pptx";
const String outputFilePath = u"OutputDirectory\\ChangeSmartArt_out.pptx";

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

// 遍歷形狀
for (auto shape : presentation->get_Slides()->idx_get(0)->get_Shapes())
{
	// 檢查形狀是否為 SmartArt 類型
	if (System::ObjectExt::Is<Aspose::Slides::SmartArt::SmartArt>(shape))
	{
		// 將形狀類型轉換為 SmartArt
		auto smartArt = System::DynamicCast_noexcept<ISmartArt>(shape);

		// 檢查 SmartArt 樣式
		if (smartArt->get_QuickStyle() == SmartArtQuickStyleType::SimpleFill) {
			// 更改 SmartArt 樣式
			smartArt->set_QuickStyle(SmartArtQuickStyleType::Cartoon);
		}

		// 檢查 SmartArt 顏色類型
		if (smartArt->get_ColorStyle() == SmartArtColorType::ColoredFillAccent1) {
			// 更改 SmartArt 顏色類型
			smartArt->set_ColorStyle(SmartArtColorType::ColorfulAccentColors);
		}
	}
}

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

獲得免費許可證

為了在沒有評估限制的情況下試用 API,您可以申請免費的臨時許可證

結論

在本文中,您了解瞭如何使用 C++ 在 PowerPoint 演示文稿中創建 SmartArt 形狀。此外,您還了解瞭如何使用 Aspose.Slides for C++ API 訪問和更改 SmartArt 形狀的樣式。它是一個強大且功能豐富的 API,提供了許多用於處理 PowerPoint 文件的附加功能。您可以通過訪問 官方文檔 來詳細探索 API。如有任何疑問,請隨時通過我們的免費支持論壇與我們聯繫。

也可以看看