Microsoft PowerPoint 提供了根據您的需要設置 PowerPoint 幻燈片背景的選項。在某些情況下,您可能需要以編程方式設置 PowerPoint 幻燈片的背景。為此,本文將教您如何使用 C++ 設置 PowerPoint 幻燈片的背景。具體來說,您將學習如何設置純色、漸變和圖像背景。

用於在 PowerPoint 演示文稿中設置幻燈片背景的 C++ API

我們將使用 Aspose.Slides for C++ API 在 Powerpoint 演示文稿中設置幻燈片背景。它是一個強大且功能豐富的 API,支持創建、讀取和修改 PowerPoint 文件,而無需額外的軟件。您可以通過 NuGet 安裝 API 或直接從 下載 部分下載。

PM> Install-Package Aspose.Slides.Cpp

使用 C++ 設置普通幻燈片的背景顏色

以下是使用 C++ 設置普通幻燈片背景顏色的步驟。

以下示例代碼顯示瞭如何使用 C++ 設置普通幻燈片的背景顏色。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\Slides\\Presentation2.pptx";
const String outputFilePath = u"OutputDirectory\\SetSlideBackground_out.pptx";

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

// 設置第一個ISlide的背景色為藍色
presentation->get_Slides()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
presentation->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Solid);
presentation->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());

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

下面給出的是設置背景顏色之前的幻燈片圖像。

設置背景前的幻燈片圖像

下面是設置背景色後的幻燈片圖片。

示例代碼生成的輸出圖像

使用 C++ 設置母版幻燈片的背景顏色

要設置母版幻燈片的背景顏色,請按照以下步驟操作。

以下示例代碼顯示瞭如何使用 C++ 設置母版幻燈片的背景顏色。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\Slides\\Presentation2.pptx";
const String outputFilePath = u"OutputDirectory\\SetMasterSlideBackground_out.pptx";

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

// 將 Master Islide 的背景顏色設置為森林綠
presentation->get_Masters()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
presentation->get_Masters()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Solid);
presentation->get_Masters()->idx_get(0)->get_Background()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_ForestGreen());

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
示例代碼生成的輸出圖像

示例代碼生成的輸出圖像

使用 C++ 設置幻燈片的漸變背景顏色

除了使用純色,您還可以將漸變背景色應用於 PowerPoint 幻燈片。為了實現這一點,請按照以下步驟操作。

以下示例代碼展示瞭如何使用 C++ 設置幻燈片的漸變背景顏色。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\Slides\\Presentation2.pptx";
const String outputFilePath = u"OutputDirectory\\SetSlideGradientBackground_out.pptx";

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

// 對背景應用漸變效果
presentation->get_Slides()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
presentation->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Gradient);
presentation->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_GradientFormat()->set_TileFlip(TileFlip::FlipBoth);

// 保存演示文稿
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
示例代碼生成的輸出圖像

示例代碼生成的輸出圖像

使用 C++ 將圖像設置為幻燈片背景

要使用圖像作為幻燈片背景,請按照以下步驟操作。

以下示例代碼顯示瞭如何使用 C++ 將圖像設置為幻燈片背景。

// 文件路徑
const String sourceFilePath = u"SourceDirectory\\Slides\\Presentation2.pptx";
const String imageFilePath = u"SourceDirectory\\Images\\Tulips.jpg";
const String outputFilePath = u"OutputDirectory\\SetSlideImageBackground_out.pptx";

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

// 設置背景屬性
presentation->get_Slides()->idx_get(0)->get_Background()->set_Type(BackgroundType::OwnBackground);
presentation->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->set_FillType(FillType::Picture);
presentation->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_PictureFillFormat()->set_PictureFillMode(PictureFillMode::Stretch);

// 獲取圖片
auto bitmap = MakeObject<System::Drawing::Bitmap>(imageFilePath);

// 將圖像添加到演示文稿的圖像集合
SharedPtr<IPPImage> imgx = presentation->get_Images()->AddImage(bitmap);

// 將圖像設置為背景
presentation->get_Slides()->idx_get(0)->get_Background()->get_FillFormat()->get_PictureFillFormat()->get_Picture()->set_Image(imgx);

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

獲得免費許可證

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

結論

在本文中,您學習瞭如何使用 C++ 設置 PowerPoint 幻燈片的背景。具體來說,您已經學習瞭如何使用 Aspose.Slides for C++ API 設置純色、漸變和圖像背景。它是一個強大的 API,提供了一系列用於處理 PowerPoint 文件的附加功能。您可以通過訪問 官方文檔 來詳細探索 API。如有任何疑問,請隨時通過我們的免費支持論壇與我們聯繫。

也可以看看