在本文中,您将学习如何使用 C# 以编程方式设置 PowerPoint 演示文稿中的幻灯片背景。特别是,本文将介绍如何设置普通幻灯片和母版幻灯片的背景。

C# API 在 PowerPoint 中设置幻灯片背景

为了在 PowerPoint 演示文稿中设置或更改幻灯片的背景,我们将使用 Aspose.Slides for .NET。该 API 旨在创建、操作和转换 PowerPoint 和 OpenOffice 演示文稿。您可以 下载 API 或使用 NuGet 安装它。

PM> Install-Package Aspose.Slides.NET

在 C# 中设置普通幻灯片的背景颜色

以下是使用 C# 在 PowerPoint 演示文稿中设置普通幻灯片背景颜色的步骤。

下面的代码示例演示如何在 PowerPoint 演示文稿中设置幻灯片的背景。

// 实例化表示演示文件的 Presentation 类
using (Presentation pres = new Presentation("presentation.pptx"))
{
    // 将第一个 ISlide 的背景颜色设置为蓝色
    pres.Slides[0].Background.Type = BackgroundType.OwnBackground;
    pres.Slides[0].Background.FillFormat.FillType = FillType.Solid;
    pres.Slides[0].Background.FillFormat.SolidFillColor.Color = Color.Blue;
    
    // 保存演示文稿
    pres.Save("ContentBG_out.pptx", SaveFormat.Pptx);
}

下面是设置背景之前的幻灯片截图。

PowerPoint演示文稿

下面是设置背景后的 PowerPoint 幻灯片。

在 C# 中设置幻灯片的背景

在 C# 中设置母版幻灯片的背景颜色

您还可以设置将影响演示文稿中所有幻灯片的母版幻灯片的背景。以下是更改母版幻灯片背景颜色的步骤。

下面的代码示例演示如何在 PowerPoint 中更改母版幻灯片的背景。

// 实例化表示演示文件的 Presentation 类
using (Presentation pres = new Presentation("presentation.pptx"))
{
    // 将 Master ISlide 的背景颜色设置为 Forest Green
    pres.Masters[0].Background.Type = BackgroundType.OwnBackground;
    pres.Masters[0].Background.FillFormat.FillType = FillType.Solid;
    pres.Masters[0].Background.FillFormat.SolidFillColor.Color = Color.ForestGreen;

    // 保存演示文稿
    pres.Save("SetSlideBackgroundMaster_out.pptx", SaveFormat.Pptx);
}

设置幻灯片的渐变背景颜色

您还可以使用 Aspose.Slides for .NET 设置幻灯片的渐变背景颜色,如下面的步骤所示。

下面的代码示例演示如何在 PowerPoint 中设置幻灯片的渐变背景颜色。

// 实例化表示演示文件的 Presentation 类
using (Presentation pres = new Presentation("presentation.pptx"))
{
    // 将渐变效果应用于背景
    pres.Slides[0].Background.Type = BackgroundType.OwnBackground;
    pres.Slides[0].Background.FillFormat.FillType = FillType.Gradient;
    pres.Slides[0].Background.FillFormat.GradientFormat.TileFlip = TileFlip.FlipBoth;

    // 保存演示文稿
    pres.Save("ContentBG_Grad_out.pptx", SaveFormat.Pptx);
}

以下屏幕截图显示了幻灯片的渐变背景。

在 C# 中设置幻灯片的渐变背景

使用 C# 将图像设置为幻灯片背景

以下是使用 C# 将图像设置为幻灯片背景的步骤。

以下代码示例演示如何将图像设置为 PowerPoint 演示文稿中幻灯片的背景。

// 实例化表示演示文件的 Presentation 类
using (Presentation pres = new Presentation("SetImageAsBackground.pptx"))
{

    // 用图像设置背景
    pres.Slides[0].Background.Type = BackgroundType.OwnBackground;
    pres.Slides[0].Background.FillFormat.FillType = FillType.Picture;
    pres.Slides[0].Background.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;

    // 设置图片
    System.Drawing.Image img = (System.Drawing.Image)new Bitmap(dataDir + "Tulips.jpg");

    // 将图像添加到演示文稿的图像集合
    IPPImage imgx = pres.Images.AddImage(img);

    pres.Slides[0].Background.FillFormat.PictureFillFormat.Picture.Image = imgx;

    // 保存演示文稿
    pres.Save("ContentBG_Img_out.pptx", SaveFormat.Pptx);
}

获取免费 API 许可证

您可以通过请求 临时许可证 来使用 Aspose.Slides for .NET,而不受评估限制。

结论

在本文中,您学习了如何使用 C# 在 PowerPoint PPTX 或 PPT 中设置幻灯片的背景。此外,您还了解了如何设置 PowerPoint 演示文稿的渐变或图像背景。您可以访问 文档 来探索 Aspose.Slides for .NET 的其他功能。此外,您可以随时通过我们的 论坛 让我们知道您的查询。

也可以看看