zoukankan      html  css  js  c++  java
  • C# 插入文本框到PPT幻灯片

    概述

    在文本框中我们可以实现的操作有很多,如插入文字、图片、设置字体大小、颜色、文本框背景填充、边框设置等。下面的示例中,将介绍通过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, 420, 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 = new 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;
    using Spire.Presentation.Drawing;
    using System.Drawing;
    
    namespace InsertTextbox_PPT
    {
        class Program
        {
            static void Main(string[] args)
            {
                //实例化Presentation类对象,加载文档并获取第一个幻灯片
                Presentation presentation = new Presentation();
                presentation.LoadFromFile("test.pptx");
                ISlide slide = presentation.Slides[0];
    
                //添加一个文本框(shape)到第一张幻灯片并添加文字。
                IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(80, 50, 420, 350));
                string textString = "万有引力的发现,是17世纪自然科学最伟大的成果之一。" +
                                    "它把地面上的物体运动的规律和天体运动的规律统一了起来,对以后物理学和天文学的发展具有深远的影响。" +
                                    "它第一次揭示了自然界中一种基本相互作用的规律,在人类认识自然的历史上树立了一座里程碑。" +
                                    "牛顿的万有引力概念是所有科学中最实用的概念之一。牛顿认为万有引力是所有物质的基本特征,这成为大部分物理科学的理论基石。";
                shape.AppendTextFrame(textString);
    
                //设置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 = new 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;
    
                //保存并打开文档
                presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
                System.Diagnostics.Process.Start("result.pptx");
            }
        }
    }
    View Code

    (本文完)

    转载请注明出处!

  • 相关阅读:
    【WinAPI】User32.dll注释
    Unity 用ml-agents机器学习造个游戏AI吧(1) (Windows环境配置)
    Unity C#笔记 容器类
    Unity C#笔记 委托/事件/Action/Func/Lambda表达式
    Unity C#笔记 协程
    游戏AI之模糊逻辑
    游戏AI之路径规划
    游戏设计模式——黑板模式
    游戏AI之决策结构—行为树
    游戏AI之感知
  • 原文地址:https://www.cnblogs.com/Yesi/p/9877030.html
Copyright © 2011-2022 走看看