PPTツール

C# を使用して PowerPoint プレゼンテーションを作成する方法

リーガン・パン
リーガン・パン
2024年3月6日
更新済み 2025年4月3日
共有:

紹介

PowerPoint プレゼンテーションは、ビジネスレポート、営業プレゼンテーション、学術講義、ソフトウェア生成のダッシュボードに不可欠です。 しかし、手動でPowerPointスライドを作成することは退屈で時間がかかることがあります。

インターロップライブラリをダウンロードする必要なく、C#でPowerPoint作成を自動化できたら素晴らしいと思いませんか? そこで、Iron Software の強力な PowerPoint ライブラリ、IronPPTが役立ちます。 IronPPTは、開発者がプログラムにより簡単にPowerPointプレゼンテーションを作成、修正、エクスポートできる強力な.NETライブラリで、Microsoft PowerPointインターロップライブラリを必要としません。 スライドを動的に生成したり、チャートを挿入したり、プレゼンテーションをPDFに変換したりする必要がある場合、IronPPTはシンプルで効果的なソリューションを提供します。

今日は、IronPPTを使用してC#でPowerPointプレゼンテーションを作成する方法を探ります。 私たちは以下をカバーします:

  • IronPPT のインストール
  • PowerPointファイルの作成と保存
  • スライド、テキスト、画像、図形を追加する
  • PowerPoint自動化のベストプラクティス

    さあ、始めましょう! 🚀

IronPPTの使用を開始する

Csharp Create Powerpoint Tutorial 1 related to IronPPTの使用を開始する

プログラムによってプレゼンテーションを作成する前に、まず開発環境を設定しましょう。

IronPPTのインストール

IronPPTを使用するには、C#プロジェクトにNuGetパッケージをインストールします。

  1. Visual Studioを開きます。

  2. パッケージ マネージャー コンソール を開きます (ツール > NuGet パッケージ マネージャー > パッケージ マネージャー コンソール)。

  3. 次のコマンドを実行します:

Install-Package IronPPT


4. または、「ツール -> NuGet パッケージ マネージャー -> ソリューションの NuGet パッケージの管理」に移動し、IronPPT を検索して「インストール」をクリックすることでインストールできます。

 ![NuGetパッケージマネージャー画面を通じてIronPPTをインストール](/static-assets/pdf/blog/csharp-create-powerpoint-tutorial/csharp-create-powerpoint-tutorial-2.webp)

5. 新しいコンソールアプリケーションプロジェクトを作成します。

 インストールが完了すると、.NET Framework 内で PowerPoint プレゼンテーションの作成を開始できます。

## **C#でのPowerPointプレゼンテーションの作成**

### **1. PowerPointファイルの初期化**

PowerPointの自動化の最初のステップは、新しいPowerPointファイルを作成することです。IronPPTを使用すると、プログラムでPowerPointプレゼンテーションを生成し、.pptxファイルとして保存することができます。 デフォルトでは、生成されたPowerPointファイルはMicrosoft PowerPoint、Googleスライド、および**.pptx形式**をサポートする他のソフトウェアと互換性があります。 本日の例では、新しいVisual Studioプロジェクトを作成します。

では、最初のコード例として、新しい空のパワーポイントプレゼンテーションファイルを[作成](https://ironsoftware.com/csharp/ppt/examples/create-empty-presentation/)します。これは出発点となるものであり、後でテキスト、スタイリングなどを追加できるシンプルなパワーポイントプレゼンテーションファイルです。

```cs
using IronPPT;

class Program
{
    static void main()
    {
        // Creating a new PresentationDocument object
        var ppt = new PresentationDocument();

    // Save the blank file presentation
        ppt.Save("example.pptx");
    }
}

出力ファイル

IronPPTを使用して作成された空のプレゼンテーション

新しいプレゼンテーションをPowerPointプログラムで開くと、現時点で含まれているのは1枚の空白のスライドだけであることがわかります。

2. PowerPointにスライドを追加する

PowerPointプレゼンテーションは複数のスライドで構成されており、それぞれが異なるレイアウト、テキスト、画像、デザイン要素を持つことができます。 IronPPTを使用すると、プレゼンテーションにスライドを動的に追加し、そのレイアウトを変更し、プログラムでコンテンツを挿入できます。 これは、レポート自動化システム、ダッシュボードのエクスポート、およびプレゼンテーションの生成に役立ちます。

この時点で、私たちのPowerPointドキュメントは内容や追加のスライドがない単一の空白スライドだけです。 では、PowerPoint プレゼンテーションにスライドを追加し始める方法を見てみましょう。

using IronPPT;
using IronPPT.Models;

// Loading in our presentation file
var ppt = new PresentationDocument("output.pptx");

ppt.AddSlide();

ppt.Save("output.pptx");
using IronPPT;
using IronPPT.Models;

// Loading in our presentation file
var ppt = new PresentationDocument("output.pptx");

ppt.AddSlide();

ppt.Save("output.pptx");
Imports IronPPT
Imports IronPPT.Models

' Loading in our presentation file
Private ppt = New PresentationDocument("output.pptx")

ppt.AddSlide()

ppt.Save("output.pptx")
$vbLabelText   $csharpLabel

出力

Csharp Create Powerpoint Tutorial 4 related to 2. PowerPointにスライドを追加する

🔹 このような単純なスライドを追加するだけでなく、IronPPT を使用して複数のスライドを追加し、それらを動的に変更することができます。 例では、プレゼンテーションに2枚のスライドのみを追加しましたが、必要なだけ追加することができます。

3. テキスト、画像、および書式設定の追加

スライドを追加したら、次のステップはコンテンツを挿入することです。 IronPPTは次のことを追加できます:

  • テキスト: タイトル、段落、およびフォーマットされたコンテンツ。
  • 画像: ロゴ、チャート、スクリーンショット。
  • カスタムフォーマット: フォント、色、配置。

    たとえば、スライドにタイトルを追加したり、画像を挿入したり、レイアウトのニーズに基づいて動的に位置を設定したりできます。 IronPPTのParagraphStyleおよびTextStyleクラスを使用して、スライドに追加されるテキストに書式設定やカスタムスタイルを適用できます。

PowerPointプレゼンテーションにテキストを追加する

using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("test.pptx");

// Adding simple text to the title slide
ppt.Slides[0].TextBoxes[0].AddText("Welcome to IronPPT");

TextBox textBox = new TextBox();
textBox.Type = ShapeType.Rectangle;
textBox.Width = 500;
textBox.Height = 200;
textBox.Position = (100, 100);
ppt.Slides[1].AddChild(textBox);

// Adding a styled paragraph to the second slide
var style = new ParagraphStyle()
{
    Alignment = TextAlignmentTypeValues.Center,
    NoBullet = true,
    LineSpacing = 30,
    ContextualSpacing = true,
};
TextStyle textStyle = new TextStyle()
{
    IsBold = true,
    Color = Color.Blue,
};

var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("Let's create some awesome presentations with IronPPT!");
paragraph.TextStyle = textStyle;
ppt.Slides[1].TextBoxes[0].AddParagraph(paragraph);

ppt.Save("example.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("test.pptx");

// Adding simple text to the title slide
ppt.Slides[0].TextBoxes[0].AddText("Welcome to IronPPT");

TextBox textBox = new TextBox();
textBox.Type = ShapeType.Rectangle;
textBox.Width = 500;
textBox.Height = 200;
textBox.Position = (100, 100);
ppt.Slides[1].AddChild(textBox);

// Adding a styled paragraph to the second slide
var style = new ParagraphStyle()
{
    Alignment = TextAlignmentTypeValues.Center,
    NoBullet = true,
    LineSpacing = 30,
    ContextualSpacing = true,
};
TextStyle textStyle = new TextStyle()
{
    IsBold = true,
    Color = Color.Blue,
};

var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("Let's create some awesome presentations with IronPPT!");
paragraph.TextStyle = textStyle;
ppt.Slides[1].TextBoxes[0].AddParagraph(paragraph);

ppt.Save("example.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums

' Creating a new PresentationDocument object
Private ppt = New PresentationDocument("test.pptx")

' Adding simple text to the title slide
ppt.Slides(0).TextBoxes(0).AddText("Welcome to IronPPT")

Dim textBox As New TextBox()
textBox.Type = ShapeType.Rectangle
textBox.Width = 500
textBox.Height = 200
textBox.Position = (100, 100)
ppt.Slides(1).AddChild(textBox)

' Adding a styled paragraph to the second slide
Dim style = New ParagraphStyle() With {
	.Alignment = TextAlignmentTypeValues.Center,
	.NoBullet = True,
	.LineSpacing = 30,
	.ContextualSpacing = True
}
Dim textStyle As New TextStyle() With {
	.IsBold = True,
	.Color = Color.Blue
}

Dim paragraph As New Paragraph()
paragraph.Style = style
paragraph.AddText("Let's create some awesome presentations with IronPPT!")
paragraph.TextStyle = textStyle
ppt.Slides(1).TextBoxes(0).AddParagraph(paragraph)

ppt.Save("example.pptx")
$vbLabelText   $csharpLabel

出力

テキストとテキストボックスの追加

上記のコードでは、タイトル画面の既存のテキストボックスに新しいテキストを追加しました。 また、スタイル付き段落を追加する前に、2枚目のスライド用の新しいテキストボックスを作成しました。 これは、IronPPTを使用してPowerPointプレゼンテーションに完全にカスタマイズされたテキストを追加する方法のシンプルな例です。

画像の追加

次に、画像を追加して、タイトルスライドに配置し、スライド上に望むように配置するために、そのサイズと位置をカスタマイズしましょう。

using IronPPT;
using IronPPT.Models;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument();

Image image = new Image();
image.LoadFromFile("example.png");
var newImage = ppt.Slides[0].AddImage(image);
newImage.Position = (150, 300);
newImage.Name = "IronPPT";
newImage.Width = 450;
newImage.Height = 200;

ppt.Save("output.pptx");
using IronPPT;
using IronPPT.Models;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument();

Image image = new Image();
image.LoadFromFile("example.png");
var newImage = ppt.Slides[0].AddImage(image);
newImage.Position = (150, 300);
newImage.Name = "IronPPT";
newImage.Width = 450;
newImage.Height = 200;

ppt.Save("output.pptx");
Imports IronPPT
Imports IronPPT.Models

' Creating a new PresentationDocument object
Private ppt = New PresentationDocument()

Private image As New Image()
image.LoadFromFile("example.png")
Dim newImage = ppt.Slides(0).AddImage(image)
newImage.Position = (150, 300)
newImage.Name = "IronPPT"
newImage.Width = 450
newImage.Height = 200

ppt.Save("output.pptx")
$vbLabelText   $csharpLabel

出力

最初のスライドに画像を追加

4. 図形とチャートを使用したスライドのカスタマイズ

シンプルなテキストや画像を超えて、PowerPointプレゼンテーションにはしばしば図形などの視覚的要素が含まれています。IronPPTを使用すると、図形を追加することもこれまでのテキストや画像の追加と同様に簡単に行えます。

例として、円形を作成し、指定されたスライドの特定の位置にそれを描画します。

using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("example.pptx");

Shape shape = new Shape();
shape.Name = "Circle";
shape.Type = ShapeType.Ellipse;
shape.Width = 300;
shape.Height = 300;
shape.FillColor = Color.PowderBlue;
shape.OutlineColor = Color.Black;
shape.Position = (180, 200);

ppt.Slides[1].AddShape(shape);

ppt.Save("example.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;

// Creating a new PresentationDocument object
var ppt = new PresentationDocument("example.pptx");

Shape shape = new Shape();
shape.Name = "Circle";
shape.Type = ShapeType.Ellipse;
shape.Width = 300;
shape.Height = 300;
shape.FillColor = Color.PowderBlue;
shape.OutlineColor = Color.Black;
shape.Position = (180, 200);

ppt.Slides[1].AddShape(shape);

ppt.Save("example.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums

' Creating a new PresentationDocument object
Private ppt = New PresentationDocument("example.pptx")

Private shape As New Shape()
shape.Name = "Circle"
shape.Type = ShapeType.Ellipse
shape.Width = 300
shape.Height = 300
shape.FillColor = Color.PowderBlue
shape.OutlineColor = Color.Black
shape.Position = (180, 200)

ppt.Slides(1).AddShape(shape)

ppt.Save("example.pptx")
$vbLabelText   $csharpLabel

出力

Csharp Create Powerpoint Tutorial 7 related to 4. 図形とチャートを使用したスライドのカスタマイズ

C#におけるPowerPoint自動化のベストプラクティス

PowerPoint生成を自動化する際は、次のベストプラクティスを考慮してください:

1. 大規模なプレゼンテーションのパフォーマンスを最適化

  • スライドの数を制限してメモリの過負荷を防ぎます。
  • 画像のファイルサイズを減少させるために圧縮を使用します。
  • プレゼンテーションを効率的に保つために、冗長なオブジェクトを避けてください。

2. 一貫したデザインを保つ

  • 一貫したフォントと色を使用してブランドを維持します。
  • スライドテンプレートを適用して、プロフェッショナルな外観を確保します。
  • 読みやすさを向上させるために、コンテンツを論理的に構成する。

3. エラーと例外を優雅に処理する

  • 無効な入力を管理するためにエラーハンドリングを実装します。
  • 画像を挿入する前に画像パスを検証します。
  • エクスポートされたファイルが正しい形式と権限を持っていることを確認してください。

    これらのベストプラクティスに従うことで、信頼性と効率性を確保しながら、高品質でプロフェッショナルなPowerPointプレゼンテーションを作成することができます。

結論

C#でIronPPTを使用してPowerPoint作成を自動化することは、動的なプレゼンテーションを迅速かつ効率的に生成する必要がある開発者にとって革命的な手段です。 エンタープライズレポートシステム、教育ツール、またはビジネスダッシュボードを構築している場合でも、IronPPTを使用すると、.NETアプリケーション内でプログラムによってPowerPointファイルを作成および操作することが容易になります。

スライドのカスタマイズ、画像の挿入、PDFエクスポートなどの機能を備えたIronPPTは、スムーズなPowerPoint自動化に必要なすべてを提供します。

試してみますか? 無料トライアルをダウンロードし、今日からC#で自動化されたPowerPointプレゼンテーションの構築を始めましょう!

リーガン・パン
ソフトウェアエンジニア
レーガンはリーディング大学で電子工学の学士号を取得しました。Iron Softwareに入社する前の仕事では、一つのタスクに集中して取り組んでいました。Iron Softwareでは、営業、技術サポート、製品開発、マーケティングのいずれにおいても広範な業務に携わることが最も楽しいと感じています。彼は、Iron Softwareライブラリを開発者がどのように使用しているかを理解し、その知識を使ってドキュメントを継続的に改善し、製品を開発することを楽しんでいます。
次へ >
C# を使用して PowerPoint を画像に変換する方法