多语言展示
当前在线:523今日阅读:168今日分享:49

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

此条经验将介绍如何通过使用免费版类库Free Spire.Presentation for .NET来插入文本框到PPT幻灯片。
工具/原料
1

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

2

Visual Studio

dll引用

安装该类库后,注意添加引用Spire.Presentation.dll到程序,dll文件可在安装路径下的Bin文件夹中获取。

C#代码示例(供参考)
1

步骤 1 :添加using指令using Spire.Presentation;using Spire.Presentation.Drawing;using System.Drawing;

2

步骤 2 :创建实例,加载测试文档,并获取第一个幻灯片Presentation presentation = new Presentation();presentation.LoadFromFile('test.pptx');ISlide slide = presentation.Slides[0];

3

步骤 3 :添加文本框(shape),并添加文字内容到文本框IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(350, 250, 500, 200));string textString = '宮崎駿は、1941年1月5日、東京都文京区生まれ。' +                    'アニメーター、アニメーター、漫画家、アニメーター、アニメーション脚本家。' +                    ' 東京学習研究所大学政治経済学科卒業。' +                    '1963年には、東営アニメーションカンパニーに入社し、アニメーターとして働いていました。' +                    '1971年に手塚治虫が制作した「バグプロダクションアニメーション部」に参加。';shape.AppendTextFrame(textString);

4

步骤 4 :设置文本框(Shape)线条格式,并填充颜色shape.Line.FillType = FillFormatType.Solid;shape.Line.Width = 1.5;shape.Line.SolidFillColor.Color = Color.LightYellow;shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient;shape.Fill.Gradient.GradientShape = Spire.Presentation.Drawing.GradientShapeType.Linear;shape.Fill.Gradient.GradientStops.Append(1f, KnownColors.SkyBlue);shape.Fill.Gradient.GradientStops.Append(0, KnownColors.LightPink);

5

步骤 5 :设置文本框阴影效果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;

6

步骤 6 :设置shape向左旋转10度, 向右旋转为正shape.Rotation = +10;

7

步骤 7 :保存并打开文档presentation.SaveToFile('result.pptx', FileFormat.Pptx2007);System.Diagnostics.Process.Start('result.pptx');

8

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

附(全部代码)

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(350, 250, 500, 200));            string textString = '宮崎駿は、1941年1月5日、東京都文京区生まれ。' +                                'アニメーター、アニメーター、漫画家、アニメーター、アニメーション脚本家。' +                                ' 東京学習研究所大学政治経済学科卒業。' +                                '1963年には、東営アニメーションカンパニーに入社し、アニメーターとして働いていました。' +                                '1971年に手塚治虫が制作した「バグプロダクションアニメーション部」に参加。';            shape.AppendTextFrame(textString);            //设置shape线条颜色和宽度            shape.Line.FillType = FillFormatType.Solid;            shape.Line.Width = 1.5;            shape.Line.SolidFillColor.Color = Color.LightYellow;            //设置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.SkyBlue);            shape.Fill.Gradient.GradientStops.Append(0, KnownColors.LightPink);            //设置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向左旋转10度, 向右旋转为正            shape.Rotation = +10;            //保存并打开文档            presentation.SaveToFile('result.pptx', FileFormat.Pptx2007);            System.Diagnostics.Process.Start('result.pptx');        }    }}

推荐信息