多语言展示
当前在线:1863今日阅读:84今日分享:32

C# 创建PPT文档

PPT文档又称演示文稿,用户可以在投影仪或者计算机上进行演示,也可以打印出来,制作成胶片,以便应用到更广泛的领域中。本文将介绍如何用C#编程语言来创建PPT文档。
工具/原料
1

Spire.Presentation for .NET

2

Visual Studio

方法/步骤
1

下载并安装Spire.Presentation,并将Spire.Presentation.dll文件引用到项目中。

2

将代码放入Visual Studio中:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Spire.Presentation;using System.Drawing;using Spire.Presentation.Drawing;namespace Create_PPT{        class Program            {                    static void Main(string[] args)                        {                             //新建一个PowerPoint文档(默认包含一页空白幻灯片)                             Presentation ppt = new Presentation();                            //设置幻灯片大小和方向                              ppt.SlideSize.Type = SlideSizeType.Screen16x9;                                          ppt.SlideSize.Orientation = SlideOrienation.Landscape;                              //为幻灯片设置背景图片                             string ImageFile = 'background.jpg';                              RectangleF rect = new RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height);                               ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Picture;                               IEmbedImage image = ppt.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);                                ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image as IImageData;                               //添加一个图形(shape)到第一张幻灯片                                IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(150, 170, 600, 100));                                textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;                                textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;                                 //清除图形上的段落(默认有一个空白段落)                                 textboxShape.TextFrame.Paragraphs.Clear();                                 //在图形上添加段落及内容                                 textboxShape.TextFrame.Paragraphs.Append(new TextParagraph());                                 textboxShape.TextFrame.Paragraphs[0].TextRanges.Append(new TextRange('教师节快乐'));                                  textboxShape.TextFrame.Paragraphs[0].SpaceAfter = 50f;                          //添加第二段及内容                                  textboxShape.TextFrame.Paragraphs.Append(new TextParagraph());                                  string text = '一张讲桌是你的依托,三尺讲台是你的岗位,五尺黑板是你传播知识的器具。'                                                       + '一年四季风雨无阻,答疑解惑传授知识,将幸福的阳光铺洒大地。'                                                       + '教师节到了,祝老师节日快乐!身体健康!';                                   textboxShape.TextFrame.Paragraphs[1].TextRanges.Append(new TextRange(text));                                 //设置段落中文字的字体、大小、颜色及段落的对齐方式、段首缩进距离                           foreach (TextParagraph para in textboxShape.TextFrame.Paragraphs)                             {                                   para.TextRanges[0].LatinFont = new TextFont('Arial Rounded MT Bold');                                     para.TextRanges[0].FontHeight = 30f;                                     para.TextRanges[0].Fill.FillType = FillFormatType.Solid;                               para.TextRanges[0].Fill.SolidColor.Color = Color.Black;                               para.Alignment = TextAlignmentType.Left;                                     para.Indent = 35;                             }                             //保存文档                              ppt.SaveToFile('创建PowerPoint.pptx', FileFormat.Pptx2013);                     }              }}

3

调试并运行代码后,生成的文档如下图所示:

推荐信息