我是靠谱客的博主 等待鸡翅,这篇文章主要介绍C#如何添加PPT背景,现在分享给大家,希望可以做个参考。

我们在创建Powerpoint文档时,系统默认的幻灯片是空白背景的,很多时候我们需要自定义幻灯片背景,以达到美观的文档效果。在下面的示例中将介绍给PowerPoint幻灯片设置背景的方法,主要包含以下三个部分:

  • 添加纯色背景
  • 添加渐变色背景
  • 添加图片作为背景

所需工具

Free Spire.Presentation for .NET 版本3.3 (社区版)

示例代码(供参考)

步骤 1 :添加如下using指令

复制代码
1
2
3
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing;

步骤 2 :创建文档

复制代码
1
2
Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx");

步骤 3 :添加纯色背景

复制代码
1
2
3
4
//设置文档的背景填充模式为纯色填充 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 :添加渐变背景色

复制代码
1
2
3
4
5
//设置文档的背景填充模式为渐变色填充 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 :添加图片作为背景

复制代码
1
2
3
4
5
6
7
8
//设置幻灯片背景色为图片背景 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 :保存文件

复制代码
1
2
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx");

完成代码后,调试运行程序,生成文件,如下:

全部代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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"); } } }

本文完。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是等待鸡翅最近收集整理的关于C#如何添加PPT背景的全部内容,更多相关C#如何添加PPT背景内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(179)

评论列表共有 0 条评论

立即
投稿
返回
顶部