PDFツール

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

更新済み 3月 6, 2024
共有:

オフィスから教室まで、Microsoft PowerPointは欠かせません。 何十年にもわたる改良により、業界標準のプレゼンテーションツールとしての地位を確立しています。 ただし、PowerPointプレゼンテーションを最大限に活用するのは複雑で時間がかかることがあります。

このガイドでは、C# の柔軟性を活かして PowerPoint プレゼンテーションの作成を自動化し、プロセスを最適化し、プログラムを最大限に活用する方法を説明します。 (C#) およびMicrosoft PowerPoint interopライブラリ。

C# パワーポイントプレゼンテーションを作成する - 入門

  • 新しいC#プロジェクトを作成
  • PowerPointプログラムの新しいインスタンスを起動する
  • スライド、テキスト、書式設定、および他の要素を含むPowerPointプレゼンテーションを作成します。
  • プロジェクトを新しいファイルとしてエクスポート

    まず最初に、C#でPowerPointドキュメントを作成する微妙な点に入る前に、PowerPointインターロップの基本を学びましょう。 開発者は、PowerPoint相互運用ライブラリを使用して、プレゼンテーションの作成、スライドの追加、コンテンツの追加、フォーマットの適用などを行うことができ、新しいプレゼンテーションの作成時により柔軟性を持たせることができます。 この方法では、開発者は独自の要件を満たすためにプレゼンテーションファイルをカスタマイズし、多くの機能にアクセスすることができます。

    C#でPowerPointプレゼンテーションを作成することで、即座にレポートを作成し、プレゼンテーションを他の形式に変換することができます。 相互運用ライブラリを無料でダウンロードできますが、プログラムでPowerPointプレゼンテーションを作成するにはMicrosoft Officeがインストールされている必要があります。

新しいVisual Studioプロジェクトを作成する

私たちはまず、Visual Studio で新しいコンソールアプリケーション プロジェクトを作成します。 「ファイル」に移動して「新しいプロジェクト」を選択します。 C#言語を選択し、コンソールアプリケーションを選択します。 プロジェクト名を入力し、保存場所を選択して、「次へ」ボタンを押してください。 最新の.NETフレームワークを選択して、作成します。 プロジェクトが稼働したら、私たちのライブラリを追加する時です。

Microsoft PowerPointインターロップライブラリを追加

Microsoft.Office.interop.PowerPoint.Applicationクラスを使用して、C#プロジェクト内でPowerPointドキュメントを作成、開く、および変更することができます。 コードは次のようになります:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;

class Program
{
   static void Main(string[] args)
   {
           // Create an instance of PowerPoint application
           PowerPoint.Application powerpointApp = new PowerPoint.Application();
           // Create powerpoint presentation
           PowerPoint.Presentation presentation = powerpointApp.Presentations.Add();
           // Customize the presentation
           // Add slides, insert content, apply formatting, etc.
           // Add a new slide
           PowerPoint.Slide slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
           // Insert text into the presentation slide
           slide.Shapes[1].TextFrame.TextRange.Text = "Demo";
           slide.Shapes[2].TextFrame.TextRange.Text = "PowerPoint";
           // Add an image to the slide
           slide.Shapes.AddPicture(@"sample.png",
                                   Microsoft.Office.Core.MsoTriState.msoFalse,
                                   Microsoft.Office.Core.MsoTriState.msoCTrue,
                                   100, 100, 300, 200);
           // Save and close the presentation file
           presentation.SaveAs("Presentation.pptx");
           presentation.Close();
           // Quit PowerPoint application
           powerpointApp.Quit();
           Console.ReadKey();
   }
}
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

class Program
{
   static void Main(string[] args)
   {
           // Create an instance of PowerPoint application
           PowerPoint.Application powerpointApp = new PowerPoint.Application();
           // Create powerpoint presentation
           PowerPoint.Presentation presentation = powerpointApp.Presentations.Add();
           // Customize the presentation
           // Add slides, insert content, apply formatting, etc.
           // Add a new slide
           PowerPoint.Slide slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitle);
           // Insert text into the presentation slide
           slide.Shapes[1].TextFrame.TextRange.Text = "Demo";
           slide.Shapes[2].TextFrame.TextRange.Text = "PowerPoint";
           // Add an image to the slide
           slide.Shapes.AddPicture(@"sample.png",
                                   Microsoft.Office.Core.MsoTriState.msoFalse,
                                   Microsoft.Office.Core.MsoTriState.msoCTrue,
                                   100, 100, 300, 200);
           // Save and close the presentation file
           presentation.SaveAs("Presentation.pptx");
           presentation.Close();
           // Quit PowerPoint application
           powerpointApp.Quit();
           Console.ReadKey();
   }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

新しく作成されたPowerPointプレゼンテーションを、あなたの要件に合わせて変更できます。 スライド、テキスト、写真、チャート、チャートのタイトル、その他のマルチメディア項目を挿入することができ、書式設定を適用し、トランジションやアニメーションを設定し、その他のタスクを実行することができます。 PowerPointのインターオプロライブラリが提供する機能を利用することで、プレゼンテーションのさまざまな部分をプログラムで調整することができます。

出力ファイルを保存

PowerPointドキュメントを特定のファイルロケーションに保存するには、SaveAsを使用します。() メソッド。 終了するには、dismissメソッドを使用します。

結論

C#を使用してPowerPointプレゼンテーションを作成することは、生産性を向上させ、プロセスを合理化し、時間を節約する効果的な方法です。Microsoft PowerPoint interopライブラリの機能を利用することで、開発者はプレゼンテーションの作成やカスタマイズを自動化でき、PowerPointファイルを扱う際に時間と労力を節約し、プロフェッショナルな成果を得ることができます。

この方法を使用することで、プレゼンテーションスキルを向上させたり、プレゼンター、教育者、またはビジネスプロフェッショナルとして視覚的に印象的なスライドで聴衆を引き付ける手助けができます。

< 以前
C# を使用して PowerPoint を画像に変換する方法
次へ >
オープンソースPDFエディター(無料および有料ツールの比較)

準備はできましたか? バージョン: 2024.9 新発売

無料のNuGetダウンロード 総ダウンロード数: 10,659,073 View Licenses >