在 .NET 中管理演示文稿背景
概述
实色、渐变和图像通常用于幻灯片背景。您可以为 普通幻灯片(单张幻灯片)或 母版幻灯片(一次应用于多张幻灯片)设置背景。

为普通幻灯片设置实色背景
Aspose.Slides 允许您为演示文稿中的特定幻灯片设置实色背景,即使该演示文稿使用了母版幻灯片。此更改仅适用于所选幻灯片。
- 创建 Presentation 类的实例。
- 将幻灯片的 BackgroundType 设置为
OwnBackground。 - 将幻灯片背景的 FillType 设置为
Solid。 - 在 FillFormat 上使用 SolidFillColor 属性来指定实色背景颜色。
- 保存修改后的演示文稿。
以下 C# 示例展示了如何将蓝色实色设置为普通幻灯片的背景:
// 创建 Presentation 类的实例。
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
// 将幻灯片的背景颜色设置为蓝色。
slide.Background.Type = BackgroundType.OwnBackground;
slide.Background.FillFormat.FillType = FillType.Solid;
slide.Background.FillFormat.SolidFillColor.Color = Color.Blue;
// 将演示文稿保存到磁盘。
presentation.Save("SolidColorBackground.pptx", SaveFormat.Pptx);
}
为母版幻灯片设置实色背景
Aspose.Slides 允许您为演示文稿中的母版幻灯片设置实色背景。母版幻灯片充当模板,控制所有幻灯片的格式,因此为母版幻灯片的背景选择实色后,它会应用于每张幻灯片。
- 创建 Presentation 类的实例。
- 将母版幻灯片的 BackgroundType(通过
masters)设置为OwnBackground。 - 将母版幻灯片背景的 FillType 设置为
Solid。 - 使用 SolidFillColor 来指定实色背景颜色。
- 保存修改后的演示文稿。
以下 C# 示例展示了如何将实色(森林绿)设置为母版幻灯片的背景:
// 创建 Presentation 类的实例。
using (Presentation presentation = new Presentation())
{
IMasterSlide masterSlide = presentation.Masters[0];
// 将母版幻灯片的背景颜色设置为森林绿。
masterSlide.Background.Type = BackgroundType.OwnBackground;
masterSlide.Background.FillFormat.FillType = FillType.Solid;
masterSlide.Background.FillFormat.SolidFillColor.Color = Color.ForestGreen;
// 将演示文稿保存到磁盘。
presentation.Save("MasterSlideBackground.pptx", SaveFormat.Pptx);
}
为幻灯片设置渐变背景
渐变是一种通过颜色逐渐变化产生的图形效果。用作幻灯片背景时,渐变可以使演示文稿看起来更具艺术感和专业性。Aspose.Slides 允许您为幻灯片设置渐变颜色背景。
- 创建 Presentation 类的实例。
- 将幻灯片的 BackgroundType 设置为
OwnBackground。 - 将幻灯片背景的 FillType 设置为
Gradient。 - 在 FillFormat 上使用 GradientFormat 属性来配置所需的渐变设置。
- 保存修改后的演示文稿。
以下 C# 示例展示了如何将渐变颜色设置为幻灯片的背景:
// 创建 Presentation 类的实例。
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
// 对背景应用渐变效果。
slide.Background.Type = BackgroundType.OwnBackground;
slide.Background.FillFormat.FillType = FillType.Gradient;
slide.Background.FillFormat.GradientFormat.TileFlip = TileFlip.FlipBoth;
// 将演示文稿保存到磁盘。
presentation.Save("GradientBackground.pptx", SaveFormat.Pptx);
}
将图像设置为幻灯片背景
除了实色和渐变填充,Aspose.Slides 还允许您使用图像作为幻灯片背景。
- 创建 Presentation 类的实例。
- 将幻灯片的 BackgroundType 设置为
OwnBackground。 - 将幻灯片背景的 FillType 设置为
Picture。 - 加载您想用作幻灯片背景的图像。
- 将图像添加到演示文稿的图像集合中。
- 在 FillFormat 上使用 PictureFillFormat 属性将图像分配为背景。
- 保存修改后的演示文稿。
以下 C# 示例展示了如何将图像设置为幻灯片的背景:
// 创建 Presentation 类的实例。
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
// 设置背景图片属性。
slide.Background.Type = BackgroundType.OwnBackground;
slide.Background.FillFormat.FillType = FillType.Picture;
slide.Background.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
// 加载图片。
IImage image = Images.FromFile("Tulips.jpg");
// 将图片添加到演示文稿的图像集合中。
IPPImage ppImage = presentation.Images.AddImage(image);
image.Dispose();
slide.Background.FillFormat.PictureFillFormat.Picture.Image = ppImage;
// 将演示文稿保存到磁盘。
presentation.Save("ImageAsBackground.pptx", SaveFormat.Pptx);
}
以下代码示例展示了如何将背景填充类型设置为平铺图片并修改平铺属性:
using (Presentation presentation = new Presentation())
{
ISlide firstSlide = presentation.Slides[0];
IBackground background = firstSlide.Background;
background.Type = BackgroundType.OwnBackground;
background.FillFormat.FillType = FillType.Picture;
IPPImage ppImage;
using (IImage newImage = Aspose.Slides.Images.FromFile("image.png"))
ppImage = presentation.Images.AddImage(newImage);
// 设置用于背景填充的图像。
IPictureFillFormat backPictureFillFormat = background.FillFormat.PictureFillFormat;
backPictureFillFormat.Picture.Image = ppImage;
// 将图片填充模式设置为平铺并调整平铺属性。
backPictureFillFormat.PictureFillMode = PictureFillMode.Tile;
backPictureFillFormat.TileOffsetX = 15f;
backPictureFillFormat.TileOffsetY = 15f;
backPictureFillFormat.TileScaleX = 46f;
backPictureFillFormat.TileScaleY = 87f;
backPictureFillFormat.TileAlignment = RectangleAlignment.Center;
backPictureFillFormat.TileFlip = TileFlip.FlipY;
presentation.Save("TileBackground.pptx", SaveFormat.Pptx);
}
更改背景图像透明度
您可能需要调整幻灯片背景图像的透明度,以突出幻灯片内容。以下 C# 代码展示了如何更改幻灯片背景图像的透明度:
var transparencyValue = 30; // 例如。
// Get the collection of picture transform operations.
var imageTransform = slide.Background.FillFormat.PictureFillFormat.Picture.ImageTransform;
// Find an existing fixed-percentage transparency effect.
var transparencyOperation = null as IAlphaModulateFixed;
foreach (var operation in imageTransform)
{
if (operation is IAlphaModulateFixed alphaModulateFixed)
{
transparencyOperation = alphaModulateFixed;
break;
}
}
// Set the new transparency value.
if (transparencyOperation == null)
{
imageTransform.AddAlphaModulateFixedEffect(100 - transparencyValue);
}
else
{
transparencyOperation.Amount = (100 - transparencyValue);
}
获取幻灯片背景值
Aspose.Slides 提供了 IBackgroundEffectiveData 接口,用于检索幻灯片的有效背景值。该接口公开了有效的 FillFormat 和 EffectFormat。
通过使用 BaseSlide 类的 background 属性,您可以获取幻灯片的有效背景。
以下 C# 示例展示了如何获取幻灯片的有效背景值:
// 创建 Presentation 类的实例。
using (Presentation presentation = new Presentation("Sample.pptx"))
{
ISlide slide = presentation.Slides[0];
// 检索有效背景,考虑母版、布局和主题。
IBackgroundEffectiveData effBackground = slide.Background.GetEffective();
if (effBackground.FillFormat.FillType == FillType.Solid)
Console.WriteLine("Fill color: " + effBackground.FillFormat.SolidFillColor);
else
Console.WriteLine("Fill type: " + effBackground.FillFormat.FillType);
}
常见问题
我可以重置自定义背景并恢复主题/布局背景吗?
可以。移除幻灯片的自定义填充后,背景将再次从相应的 layout/master 幻灯片继承(即 theme background)。
如果我稍后更改演示文稿的主题,背景会怎样?
如果幻灯片拥有自己的填充,则保持不变。如果背景是从 layout/master 继承的,它将更新以匹配 new theme。