加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 插入文本框到PPT幻灯片

发布时间:2020-12-16 01:07:16 所属栏目:百科 来源:网络整理
导读:概述 在文本框中我们可以实现的操作有很多,如插入文字、图片、设置字体大

概述

在文本框中我们可以实现的操作有很多,如插入文字、图片、设置字体大小、颜色、文本框背景填充、边框设置等。下面的示例中,将介绍通过C# 在PPT幻灯片中插入幻灯片的方法。

示例中包含了以下要点:

  • 插入文本到文本框
  • 设置边框颜色、粗细
  • 文本框背景色填充
  • 设置文本框旋转
  • 设置文本框阴影效果

?

使用工具:Free Spire.Presentation for .NET 3.3(免费版)

注:安装后,注意在程序中添加引用Spire.Presentaton.dll(dll可在安装路径下的Bin文件夹中获取)

C# 代码(供参考)

步骤 1 :初始化Presentation类,加载测试文档

 Presentation presentation = new Presentation();
 presentation.LoadFromFile("test.pptx");

步骤 2 :获取幻灯片

ISlide slide = presentation.Slides[0];

步骤 3 :添加指定大小的文本框(shape)到幻灯片,并写入文本

IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle,new RectangleF(80,50,1)">420,1)">350));
string textString = 万有引力的发现,是17世纪自然科学最伟大的成果之一。" +
                    它把地面上的物体运动的规律和天体运动的规律统一了起来,对以后物理学和天文学的发展具有深远的影响。它第一次揭示了自然界中一种基本相互作用的规律,在人类认识自然的历史上树立了一座里程碑。牛顿的万有引力概念是所有科学中最实用的概念之一。牛顿认为万有引力是所有物质的基本特征,这成为大部分物理科学的理论基石。";
shape.AppendTextFrame(textString);

步骤 4 :设置文本框边框样式、填充样式、阴影效果、旋转度等

//设置shape线条颜色和宽度
shape.Line.FillType = FillFormatType.Solid;
shape.Line.Width = 2;
shape.Line.SolidFillColor.Color = Color.White;

设置shape填充颜色为渐变色
shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient;
shape.Fill.Gradient.GradientShape = Spire.Presentation.Drawing.GradientShapeType.Linear;
shape.Fill.Gradient.GradientStops.Append(1f,KnownColors.LightGray);
shape.Fill.Gradient.GradientStops.Append(0,KnownColors.LightBlue);            

设置shape阴影
Spire.Presentation.Drawing.OuterShadowEffect shadow =  Spire.Presentation.Drawing.OuterShadowEffect();
shadow.BlurRadius = 20;
shadow.Direction = 30;
shadow.Distance = 8;
shadow.ColorFormat.Color = Color.LightGray;
shape.EffectDag.OuterShadowEffect = shadow;

设置shape向右旋转5度(向左旋转设置数值为负即可)
shape.Rotation = 5;

步骤 5 :保存文档

presentation.SaveToFile(result.pptx",FileFormat.Pptx2010);

完成代码后,调试程序,生成文档。文本框添加效果如下图所示:

全部代码:

using Spire.Presentation;
 Spire.Presentation.Drawing;
 System.Drawing;

namespace InsertTextbox_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            实例化Presentation类对象,加载文档并获取第一个幻灯片
            Presentation presentation =  Presentation();
            presentation.LoadFromFile();
            ISlide slide = presentation.Slides[];

            添加一个文本框(shape)到第一张幻灯片并添加文字。
            IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle,1)">));
            " +
                                ;
            shape.AppendTextFrame(textString);

            设置shape线条颜色和宽度
            shape.Line.FillType = FillFormatType.Solid;
            shape.Line.Width = ;
            shape.Line.SolidFillColor.Color = Color.White;

            设置shape填充颜色为渐变色
            shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient;
            shape.Fill.Gradient.GradientShape = Spire.Presentation.Drawing.GradientShapeType.Linear;
            shape.Fill.Gradient.GradientStops.Append(1f,KnownColors.LightGray);
            shape.Fill.Gradient.GradientStops.Append(设置shape阴影
            Spire.Presentation.Drawing.OuterShadowEffect shadow =  Spire.Presentation.Drawing.OuterShadowEffect();
            shadow.BlurRadius = ;
            shadow.Direction = ;
            shadow.Distance = ;
            shadow.ColorFormat.Color = Color.LightGray;
            shape.EffectDag.OuterShadowEffect = shadow;

            设置shape向右旋转5度(向左旋转设置数值为负即可)
            shape.Rotation = 5;

            保存并打开文档
            presentation.SaveToFile();
        }
    }
}
View Code

?

(本文完)

转载请注明出处!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读