当前位置: 移动技术网 > IT编程>开发语言>c# > C#如何添加PPT背景

C#如何添加PPT背景

2019年07月18日  | 移动技术网IT编程  | 我要评论

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

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

所需工具

free spire.presentation for .net 版本3.3 (社区版)

示例代码(供参考)

步骤 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");
    }
  }
}

本文完。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网