文檔首頁(yè)>>Spire.Doc系列教程>>Spire.Doc系列教程(9):創(chuàng)建 PowerPoint 文檔
Spire.Doc系列教程(9):創(chuàng)建 PowerPoint 文檔
PowerPoint文檔(幻燈片)是一種常見(jiàn)的演示文檔,在演講,教學(xué),產(chǎn)品演示等方面得到廣泛的應(yīng)用。本文將介紹如何通過(guò)編程的方式創(chuàng)建PowerPoint文檔。
創(chuàng)建簡(jiǎn)單的PowerPoint文檔
//新建一個(gè)PowerPoint文檔(默認(rèn)包含一頁(yè)空白幻燈片) Presentation ppt = new Presentation(); //設(shè)置幻燈片大小和方向 ppt.SlideSize.Type = SlideSizeType.Screen16x9; ppt.SlideSize.Orientation = SlideOrienation.Landscape; //為幻燈片設(shè)置背景圖片 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; //添加一個(gè)圖形(shape)到第一張幻燈片 IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 600, 100)); textboxShape.ShapeStyle.LineColor.Color = Color.Transparent; textboxShape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None; //清除圖形上的段落(默認(rèn)有一個(gè)空白段落) textboxShape.TextFrame.Paragraphs.Clear(); //在圖形上添加段落及內(nèi)容 textboxShape.TextFrame.Paragraphs.Append(new TextParagraph()); textboxShape.TextFrame.Paragraphs[0].TextRanges.Append(new TextRange("Spire.Presentation for .NET")); textboxShape.TextFrame.Paragraphs[0].SpaceAfter = 50f; //添加第二段及內(nèi)容 textboxShape.TextFrame.Paragraphs.Append(new TextParagraph()); string text = "一款專業(yè)的 PowerPoint 組件,使用該組件,開(kāi)發(fā)者可以在 .NET 平臺(tái)上對(duì) PowerPoint 文檔進(jìn)行生成、讀取、寫(xiě)入、修改、" + "轉(zhuǎn)換和打印等操作。 作為一個(gè)獨(dú)立的控件,Spire.Presentation for .NET 的運(yùn)行無(wú)需要安裝 Microsoft PowerPoint。"; textboxShape.TextFrame.Paragraphs[1].TextRanges.Append(new TextRange(text)); //設(shè)置段落中文字的字體、大小、顏色及段落的對(duì)齊方式、段首縮進(jìn)距離 foreach (TextParagraph para in textboxShape.TextFrame.Paragraphs) { para.TextRanges[0].LatinFont = new TextFont("Arial Rounded MT Bold"); para.TextRanges[0].FontHeight = 13f; para.TextRanges[0].Fill.FillType = FillFormatType.Solid; para.TextRanges[0].Fill.SolidColor.Color = Color.Black; para.Alignment = TextAlignmentType.Left; para.Indent = 35; } //保存文檔 ppt.SaveToFile("創(chuàng)建PowerPoint.pptx", FileFormat.Pptx2013)
設(shè)置文檔屬性
//新建一個(gè)文檔 Presentation ppt= new Presentation(); //通過(guò)DocumentProperty設(shè)置文檔屬性 ppt.DocumentProperty.Application = "Spire.Presentation"; ppt.DocumentProperty.Author = "http://www.e-iceblue.com/"; ppt.DocumentProperty.Company = "冰藍(lán)科技有限公司"; ppt.DocumentProperty.Keywords = "C#,PowerPoint文檔屬性"; ppt.DocumentProperty.Comments = "無(wú)"; ppt.DocumentProperty.Category = "PowerPoint教程"; ppt.DocumentProperty.Title = "如何為PowerPoint文檔添加屬性"; ppt.DocumentProperty.Subject = "Add document properties in C#"; ppt.DocumentProperty.Manager = "Gary"; //保存文檔 ppt.SaveToFile("設(shè)置文檔屬性.pptx", FileFormat.Pptx2013);