文檔金喜正規買球>>Spire.Doc系列教程>>Spire.Doc系列教程(9):創建 PowerPoint 文檔
Spire.Doc系列教程(9):創建 PowerPoint 文檔
PowerPoint文檔(幻燈片)是一種常見的演示文檔,在演講,教學,產品演示等方面得到廣泛的應用。本文將介紹如何通過編程的方式創建PowerPoint文檔。
創建簡單的PowerPoint文檔
//新建一個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(50, 70, 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("Spire.Presentation for .NET")); textboxShape.TextFrame.Paragraphs[0].SpaceAfter = 50f; //添加第二段及內容 textboxShape.TextFrame.Paragraphs.Append(new TextParagraph()); string text = "一款專業的 PowerPoint 組件,使用該組件,開發者可以在 .NET 平臺上對 PowerPoint 文檔進行生成、讀取、寫入、修改、" + "轉換和打印等操作。 作為一個獨立的控件,Spire.Presentation for .NET 的運行無需要安裝 Microsoft PowerPoint。"; 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 = 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("創建PowerPoint.pptx", FileFormat.Pptx2013)

設置文檔屬性
//新建一個文檔 Presentation ppt= new Presentation(); //通過DocumentProperty設置文檔屬性 ppt.DocumentProperty.Application = "Spire.Presentation"; ppt.DocumentProperty.Author = "//www.e-iceblue.com/"; ppt.DocumentProperty.Company = "冰藍科技有限公司"; ppt.DocumentProperty.Keywords = "C#,PowerPoint文檔屬性"; ppt.DocumentProperty.Comments = "無"; ppt.DocumentProperty.Category = "PowerPoint教程"; ppt.DocumentProperty.Title = "如何為PowerPoint文檔添加屬性"; ppt.DocumentProperty.Subject = "Add document properties in C#"; ppt.DocumentProperty.Manager = "Gary"; //保存文檔 ppt.SaveToFile("設置文檔屬性.pptx", FileFormat.Pptx2013);
