LoginSignup
1
1

More than 3 years have passed since last update.

C#でPowerPointを作成する方法

Posted at

 

パワーポイント(PowerPoint)とはマイクロソフトのプレゼンテーションソフトで日常的の仕事ではよく使われているものです。例えば、講義や講演などの分野で幅広く応用することが多いと思います。

では、今回はC#でSpire.Presentation というライブラリを通じてPowerPointを作成する方法を紹介します。

下準備

1.E-iceblueの公式サイトからFree Spire.Presentation無料版をダウンロードしてください。

f:id:lendoris:20201118114812p:plain

 

 

2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. Presentation.dllを参照に追加してください。

(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→Presentation.dll”というようです。)

 

f:id:lendoris:20201118114749p:plain

 

サンプルコード

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace ConsoleApplication25
{
    class Program
    {
        static void Main(string[] args)
        {
            //PowerPointを作成します。
            Presentation ppt = new Presentation();

            //スライドのサイズと方向を配置します。
            ppt.SlideSize.Type = SlideSizeType.Screen16x9;
            ppt.SlideSize.Orientation = SlideOrienation.Landscape;

            //スライドの背景画像を挿入します。
            string ImageFile ="picture.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;

            //図形を初めのスライドに追加します。
            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("初めまして!"));
            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 = 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);
        }
    }
}

完成例

 

f:id:lendoris:20201118115201p:plain

 

 

以上です。

 

ここまで読んでくれてありがとうございます!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1