我們在建立Powerpoint文檔時,系統預設的幻燈片是空白背景的,很多時候我們需要自定義幻燈片背景,以達到美觀的文檔效果。在下面的示例中将介紹給PowerPoint幻燈片設定背景的方法,主要包含以下三個部分:
- 添加純色背景
- 添加漸變色背景
- 添加圖檔作為背景
所需工具
示例代碼(供參考)
步驟 1 :添加如下using指令
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
步驟 2 :建立文檔
Presentation ppt = new Presentation();
ppt.LoadFromFile("test.pptx");
步驟 3 :添加純色背景
//設定文檔的背景填充模式為純色填充
ppt.Slides[0].SlideBackground.Type = BackgroundType.Custom;
ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Solid;
ppt.Slides[0].SlideBackground.Fill.SolidColor.Color = Color.Pink;
步驟 4 :添加漸變背景色
//設定文檔的背景填充模式為漸變色填充
ppt.Slides[1].SlideBackground.Type = BackgroundType.Custom;
ppt.Slides[1].SlideBackground.Fill.FillType = FillFormatType.Gradient;
ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(0f, KnownColors.Yellow);
ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(1f, KnownColors.Orange);
步驟 5 :添加圖檔作為背景
//設定幻燈片背景色為圖檔背景
ppt.Slides[2].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[2].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[2].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
//加載圖檔作為幻燈片背景
Image img = Image.FromFile("green.png");
IImageData image = ppt.Images.Append(img);
ppt.Slides[2].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
步驟6 :儲存檔案
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");
完成代碼後,調試運作程式,生成檔案,如下:
全部代碼:
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace AddBackground_PPT
{
class Program
{
static void Main(string[] args)
{
//執行個體化Presentation類,加載PowerPoint文檔
Presentation ppt = new Presentation();
ppt.LoadFromFile("test.pptx");
//設定文檔的背景填充模式為純色填充
ppt.Slides[0].SlideBackground.Type = BackgroundType.Custom;
ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Solid;
ppt.Slides[0].SlideBackground.Fill.SolidColor.Color = Color.Pink;
//設定文檔的背景填充模式為漸變色填充
ppt.Slides[1].SlideBackground.Type = BackgroundType.Custom;
ppt.Slides[1].SlideBackground.Fill.FillType = FillFormatType.Gradient;
ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(0f, KnownColors.Yellow);
ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(1f, KnownColors.Orange);
//設定幻燈片背景色為圖檔背景
ppt.Slides[2].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[2].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[2].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
//加載圖檔作為幻燈片背景
Image img = Image.FromFile("green.png");
IImageData image = ppt.Images.Append(img);
ppt.Slides[2].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
//儲存并打開文檔
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");
}
}
}
View Code
本文完。
如需轉載,請注明出處!!