网站建设资讯

NEWS

网站建设资讯

C#如何添加PPT背景(纯色背景、渐变色背景、图片背景)-创新互联

概述

我们在创建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");

完成代码后,调试运行程序,生成文件,如下:
C# 如何添加PPT背景(纯色背景、渐变色背景、图片背景)
全部代码:

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

本文完。
如需转载,请注明出处!!

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章名称:C#如何添加PPT背景(纯色背景、渐变色背景、图片背景)-创新互联
本文URL:http://cdweb.net/article/djcodg.html