パワーポイント(PowerPoint)とはマイクロソフトのプレゼンテーションソフトで日常的の仕事ではよく使われているものです。例えば、講義や講演などの分野で幅広く応用することが多いと思います。
では、今回はC#でSpire.Presentation というライブラリを通じてPowerPointを作成する方法を紹介します。
下準備
1.E-iceblueの公式サイトからFree Spire.Presentation無料版をダウンロードしてください。
2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. Presentation.dllを参照に追加してください。
(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→Presentation.dll”というようです。)
サンプルコード
usingSpire.Presentation;usingSpire.Presentation.Drawing;usingSystem.Drawing;namespaceConsoleApplication25{classProgram{staticvoidMain(string[]args){//PowerPointを作成します。Presentationppt=newPresentation();//スライドのサイズと方向を配置します。ppt.SlideSize.Type=SlideSizeType.Screen16x9;ppt.SlideSize.Orientation=SlideOrienation.Landscape;//スライドの背景画像を挿入します。stringImageFile="picture.jpg";RectangleFrect=newRectangleF(0,0,ppt.SlideSize.Size.Width,ppt.SlideSize.Size.Height);ppt.Slides[0].SlideBackground.Fill.FillType=FillFormatType.Picture;IEmbedImageimage=ppt.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle,ImageFile,rect);ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage=imageasIImageData;//図形を初めのスライドに追加します。IAutoShapetextboxShape=ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle,newRectangleF(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(newTextParagraph());textboxShape.TextFrame.Paragraphs[0].TextRanges.Append(newTextRange("初めまして!"));textboxShape.TextFrame.Paragraphs[0].SpaceAfter=50f;//二つめの段落とそのコンテンツを追加します。textboxShape.TextFrame.Paragraphs.Append(newTextParagraph());stringtext="私はパンダと申します。これからよろしくお願いします!";textboxShape.TextFrame.Paragraphs[1].TextRanges.Append(newTextRange(text));//段落の文字のフォント・サイズなどを配置します。foreach(TextParagraphparaintextboxShape.TextFrame.Paragraphs){para.TextRanges[0].LatinFont=newTextFont("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);}}}完成例
以上です。
ここまで読んでくれてありがとうございます!


