フッターコンテンツにスキップ
製品比較

IronPPTを使用したC# Microsoft Office相互運用アプリケーション代替

PowerPoint プレゼンテーション ファイルで動作する .NET アプリケーションを構築する場合、開発者は通常、従来のMicrosoft Office Interop (PowerPoint またはIronPPTなどの最新の .NET ライブラリのいずれかのアプローチを採用します。

どちらのオプションもPowerPointのスライド操作にはアクセスできますが、使いやすさ、パフォーマンス、スケーラビリティには大きな違いがあります。サーバー上でMicrosoft Officeのセットアップに苦労したり、展開中に不可解なCOMエラーに対処したりした経験があれば、IronPPTのメリットをきっと理解できるでしょう。

このガイドでは、2 つのアプローチを詳細に比較し、実際の使用例を示し、 IronPPT が Interop のすべての機能を、苦労することなく実現する方法を説明します。

Microsoft Office Interop PowerPoint とは何ですか?

! C# Microsoft Office Interop アプリケーション IronPPT を使用した C# の代替手段: 図 1 - Microsoft Office Interop PowerPoint NuGet ページ

Microsoft Office Interop PowerPoint は、Microsoft Office Interop スイート (C# アプリケーションが PowerPoint、Word、Excel などの Office アプリケーションと対話できるようにする COM ベースの API のセット) の一部です。 これは、バックグラウンドで非表示の PowerPoint インスタンスを起動し、コードを介して操作することで機能します。

Interop は機能しますが、重大な制限があります。

Microsoft Interop PowerPoint の主な制限

  • Microsoft Office のインストールが必要:相互運用性を実現するには、ホストマシンに物理的に PowerPoint がインストールされている必要があります。これは、Web アプリ、クラウドベースのシステム、または Docker コンテナにとって大きな障害となります。
  • Windows のみ: Windows でのみ実行されます。 Linux または macOS はサポートされていません。 *サーバー側の互換性が低い*: バックグラウンド サービス、CI/CD パイプライン、または Web サーバーで Interop を実行すると信頼性が低くなり、HRESULT: 0x800706B5 などのエラーが発生することがよくあります。 スレッドセーフではありません: COM オブジェクトは本質的にスレッドセーフではないため、同時実行が難しくなります。 困難な展開: Office のインストールはランタイムに依存するため、自己完結型アプリの配布は困難になります。 より困難なエラー処理**: COM InterOp によってスローされるエラーは、多くの場合曖昧でデバッグが困難です。

Interop がいかに扱いにくくなるかの例を次に示します。

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
var app = new PowerPoint.Application();
// Create a new presentation
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
// Add a slide to the presentation
var slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText);
// Add some text to the slide
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
// Save the presentation to a file
presentation.SaveAs(@"C:\TestInterop.pptx");
// Close the presentation and quit the application
presentation.Close();
app.Quit();
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
var app = new PowerPoint.Application();
// Create a new presentation
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
// Add a slide to the presentation
var slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText);
// Add some text to the slide
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
// Save the presentation to a file
presentation.SaveAs(@"C:\TestInterop.pptx");
// Close the presentation and quit the application
presentation.Close();
app.Quit();
Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
Private app = New PowerPoint.Application()
' Create a new presentation
Private presentation = app.Presentations.Add(MsoTriState.msoTrue)
' Add a slide to the presentation
Private slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText)
' Add some text to the slide
Private slide.Shapes(1).TextFrame.TextRange.Text = "Hello from Interop!"
' Save the presentation to a file
presentation.SaveAs("C:\TestInterop.pptx")
' Close the presentation and quit the application
presentation.Close()
app.Quit()
$vbLabelText   $csharpLabel

一見、これで問題ないように思えます。しかし、運用環境では、PowerPoint がインストールされていることを確認し、Office ライセンスを処理し、リソースを手動で管理し、ヘッドレス環境で問題が発生しないことを祈る必要があります。

IronPPTのご紹介:モダンで強力な代替手段

IronPPTは、Microsoft Officeを必要とせずにPowerPointファイルを作成、読み込み、編集、変換できる強力な.NETライブラリです。目を引くレポートを作成したい場合も、コードを記述してプレゼンテーション作成を自動化したい場合も、Microsoft PowerPointをインストールせずにPowerPointプレゼンテーションを作成できるツールが必要な場合も、IronPPTがお手伝いします。

次のような機能を望む開発者向けに特別に設計されています。

  • シンプルでクリーンな構文
  • 最新の .NET Framework / .NET Core / .NET 6/7+ プラットフォームのサポート
  • 軽量で高速なPowerPoint処理

はい、Office や PowerPoint をインストールする必要はまったくありません。 IronPPT は 100% スタンドアロンです。

インストール

次の行を実行すると、NuGet パッケージ マネージャー コンソールを介して IronPPT を C# プロジェクトに追加できます。

Install-Package IronPPT

本日のデモでは、Visual Studio で新しいプロジェクトを作成します。 ぜひ、一緒にやってみたい方は、自分で作ってみてください! Visual Studio 内で新しいプロジェクトを作成し、コンソール アプリケーションを選択するだけで開始できます。

IronPPTの利点

! C# Microsoft Office 相互運用アプリケーション IronPPT を使用した C# の代替手段: 図 2 - IronPPT

オフィスに依存しない

IronPPT を使用すると、アプリは完全に独立します。 Microsoft Office をインストールしたりライセンスを取得したりすることなく、Azure、AWS Lambda、Docker コンテナー、Linux サーバーなどの任意の環境に展開できます。

プレゼンテーションファイル作成のためのクリーンなAPI

IronPPT を使用すると、わずか数行のコードで新しいプレゼンテーション ファイルを作成し、空のプレゼンテーションに新しいテキストを簡単に追加できます。 IronPPT で新しいファイルを作成すると、最初は 1 つのスライドから始まり、ニーズに合わせて編集できるようになります。 スライドをさらに追加しますか? 簡単な AddSlide メソッドを使用して、数行のコードを書くだけで、プレゼンテーションに必要な数のスライドを追加できます。

using IronPPT;
using IronPPT.Models;

// Create a new empty presentation
var document = new PresentationDocument();

// Add text to the first slide
document.Slides[0].TextBoxes[0].AddText("Hello, World!");
document.Slides[0].TextBoxes[1].AddText("Welcome to IronPPT!");

// Save the presentation to a file
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;

// Create a new empty presentation
var document = new PresentationDocument();

// Add text to the first slide
document.Slides[0].TextBoxes[0].AddText("Hello, World!");
document.Slides[0].TextBoxes[1].AddText("Welcome to IronPPT!");

// Save the presentation to a file
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models

' Create a new empty presentation
Private document = New PresentationDocument()

' Add text to the first slide
document.Slides(0).TextBoxes(0).AddText("Hello, World!")
document.Slides(0).TextBoxes(1).AddText("Welcome to IronPPT!")

' Save the presentation to a file
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

Output

! C# Microsoft Office Interop アプリケーション IronPPT を使用した C# の代替: 図 3 - カスタムテキストを使用した新しいプレゼンテーション

これを、冗長でエラーが発生しやすい Interop アプローチと比較してください。 IronPPT は、クリーンで読みやすく、すぐに実稼働環境で使用できます。

カスタムシェイプと画像を追加する

プレゼンテーションに視覚的に魅力的な要素を作成したいとお考えですか? IronPPT は、スライドへのカスタム図形画像の追加をサポートしており、プレゼンテーションの最終的な外観を完全に制御できます。

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

// Load an existing presentation
var document = new PresentationDocument("presentation.pptx");
Slide slide = new Slide();

// Add a rectangle shape
Shape shape = new Shape
{
    Type = ShapeType.Rectangle,
    FillColor = Color.LightBlue,
    OutlineColor = Color.Black,
    Width = 200,
    Height = 100,
    Position = (200, 50)
};
slide.AddShape(shape);

// Add an Image
Image image = new Image();
image.LoadFromFile("IronPPT.png");
var img = slide.AddImage(image);
img.Position = (100, 200);
img.Width = 400;
img.Height = 200;

// Add the slide to the document and save
document.AddSlide(slide);
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
using IronPPT.Models.Styles;

// Load an existing presentation
var document = new PresentationDocument("presentation.pptx");
Slide slide = new Slide();

// Add a rectangle shape
Shape shape = new Shape
{
    Type = ShapeType.Rectangle,
    FillColor = Color.LightBlue,
    OutlineColor = Color.Black,
    Width = 200,
    Height = 100,
    Position = (200, 50)
};
slide.AddShape(shape);

// Add an Image
Image image = new Image();
image.LoadFromFile("IronPPT.png");
var img = slide.AddImage(image);
img.Position = (100, 200);
img.Width = 400;
img.Height = 200;

// Add the slide to the document and save
document.AddSlide(slide);
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
Imports IronPPT.Models.Styles

' Load an existing presentation
Private document = New PresentationDocument("presentation.pptx")
Private slide As New Slide()

' Add a rectangle shape
Private shape As New Shape With {
	.Type = ShapeType.Rectangle,
	.FillColor = Color.LightBlue,
	.OutlineColor = Color.Black,
	.Width = 200,
	.Height = 100,
	.Position = (200, 50)
}
slide.AddShape(shape)

' Add an Image
Dim image As New Image()
image.LoadFromFile("IronPPT.png")
Dim img = slide.AddImage(image)
img.Position = (100, 200)
img.Width = 400
img.Height = 200

' Add the slide to the document and save
document.AddSlide(slide)
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

Output

! C# Microsoft Office Interop アプリケーション C# の代替案 IronPPT の使用: 図 4 - プレゼンテーションにカスタム図形と画像を追加しました

スタイル付き段落を追加する

プレゼンテーションのトピックに関する情報や詳細をテキストで提供し、同時に視覚的に魅力的な要素としても機能させたいと思いませんか? IronPPT を使用して、スタイル設定された段落を作成し、プレゼンテーション ファイルに追加して、スライド ショーで視聴者の関心を引き付けます。

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

// Create a new presentation
var document = new PresentationDocument();
Slide slide = new Slide();

// Define the paragraph style
var style = new ParagraphStyle()
{
    NoBullet = true,
    RightToLeft = true,
    Indent = 10.00,
    Alignment = TextAlignmentTypeValues.Center,
};

// Create a paragraph with the style
var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("This is a sample paragraph with custom styles applied.");

// Add the paragraph to the slide
document.AddSlide(slide);
slide.AddParagraph(paragraph);

// Save the presentation to a file
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;
using IronPPT.Enums;
using IronPPT.Models.Styles;

// Create a new presentation
var document = new PresentationDocument();
Slide slide = new Slide();

// Define the paragraph style
var style = new ParagraphStyle()
{
    NoBullet = true,
    RightToLeft = true,
    Indent = 10.00,
    Alignment = TextAlignmentTypeValues.Center,
};

// Create a paragraph with the style
var paragraph = new Paragraph();
paragraph.Style = style;
paragraph.AddText("This is a sample paragraph with custom styles applied.");

// Add the paragraph to the slide
document.AddSlide(slide);
slide.AddParagraph(paragraph);

// Save the presentation to a file
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models
Imports IronPPT.Enums
Imports IronPPT.Models.Styles

' Create a new presentation
Private document = New PresentationDocument()
Private slide As New Slide()

' Define the paragraph style
Private style = New ParagraphStyle() With {
	.NoBullet = True,
	.RightToLeft = True,
	.Indent = 10.00,
	.Alignment = TextAlignmentTypeValues.Center
}

' Create a paragraph with the style
Private paragraph = New Paragraph()
paragraph.Style = style
paragraph.AddText("This is a sample paragraph with custom styles applied.")

' Add the paragraph to the slide
document.AddSlide(slide)
slide.AddParagraph(paragraph)

' Save the presentation to a file
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

Output

! C# Microsoft Office Interop アプリケーション IronPPT を使用した C# の代替: 図 5 - スタイル付き段落出力

Microsoft PowerPoint Interop の欠点

1. PowerPointのインストールが必要

Microsoft PowerPoint がインストールされていない場合、アプリはクラッシュします。

using Microsoft.Office.Interop.PowerPoint;

// Attempt to open an existing PowerPoint file
var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
using Microsoft.Office.Interop.PowerPoint;

// Attempt to open an existing PowerPoint file
var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
Imports Microsoft.Office.Interop.PowerPoint

' Attempt to open an existing PowerPoint file
Private app = New Application()
Private presentation = app.Presentations.Open("C:\Slides\Deck.pptx")
$vbLabelText   $csharpLabel

Problem:

PowerPoint がマシン (クラウド サーバーや Docker コンテナーなど) にインストールされていない場合、通常は COMException がスローされます。

次のエラーのため、CLSID を持つコンポーネントの COM クラス ファクトリの取得に失敗しました: 80040154 クラスが登録されていません。

2. STAスレッド要件

Interop は、シングル スレッド アパートメント (STA) スレッドで実行する必要があります。そうしないとクラッシュします。

// This will crash if called from a background thread in a web app or service
var app = new Application();
// This will crash if called from a background thread in a web app or service
var app = new Application();
' This will crash if called from a background thread in a web app or service
Dim app = New Application()
$vbLabelText   $csharpLabel

回避策: STA スレッドで呼び出しを手動でラップする必要があります。

Thread thread = new Thread(() =>
{
    // Create a new PowerPoint application
    var app = new Application();

    // Add a presentation and slide
    var pres = app.Presentations.Add();
    pres.Slides.Add(1, PpSlideLayout.ppLayoutText);

    // Save and close the presentation
    pres.SaveAs(@"C:\output.pptx");
    app.Quit();
});

// Set thread apartment state and start
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
Thread thread = new Thread(() =>
{
    // Create a new PowerPoint application
    var app = new Application();

    // Add a presentation and slide
    var pres = app.Presentations.Add();
    pres.Slides.Add(1, PpSlideLayout.ppLayoutText);

    // Save and close the presentation
    pres.SaveAs(@"C:\output.pptx");
    app.Quit();
});

// Set thread apartment state and start
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
Dim thread As New Thread(Sub()
	' Create a new PowerPoint application
	Dim app = New Application()

	' Add a presentation and slide
	Dim pres = app.Presentations.Add()
	pres.Slides.Add(1, PpSlideLayout.ppLayoutText)

	' Save and close the presentation
	pres.SaveAs("C:\output.pptx")
	app.Quit()
End Sub)

' Set thread apartment state and start
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
$vbLabelText   $csharpLabel

これは、特に ASP.NET またはバックグラウンド サービス内では扱いにくく、脆弱です。

3. アンマネージCOMオブジェクトとメモリリーク

COM オブジェクトの解放に失敗すると、メモリ リークが発生し、アプリがクラッシュします。

var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
// Forgetting to release COM objects correctly can lead to memory leaks
presentation.Close();
app.Quit();
// Forgot to release COM objects!
var app = new Application();
var presentation = app.Presentations.Open(@"C:\Slides\Deck.pptx");
// Forgetting to release COM objects correctly can lead to memory leaks
presentation.Close();
app.Quit();
// Forgot to release COM objects!
Dim app = New Application()
Dim presentation = app.Presentations.Open("C:\Slides\Deck.pptx")
' Forgetting to release COM objects correctly can lead to memory leaks
presentation.Close()
app.Quit()
' Forgot to release COM objects!
$vbLabelText   $csharpLabel

4. 複雑で冗長な構文

シンプルなテキスト スライドを追加するには、大量の定型文が必要です。

var app = new Application();
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
var slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutText);
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
presentation.SaveAs(@"C:\test.pptx");
presentation.Close();
app.Quit();
var app = new Application();
var presentation = app.Presentations.Add(MsoTriState.msoTrue);
var slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutText);
slide.Shapes[1].TextFrame.TextRange.Text = "Hello from Interop!";
presentation.SaveAs(@"C:\test.pptx");
presentation.Close();
app.Quit();
Dim app = New Application()
Dim presentation = app.Presentations.Add(MsoTriState.msoTrue)
Dim slide = presentation.Slides.Add(1, PpSlideLayout.ppLayoutText)
slide.Shapes(1).TextFrame.TextRange.Text = "Hello from Interop!"
presentation.SaveAs("C:\test.pptx")
presentation.Close()
app.Quit()
$vbLabelText   $csharpLabel

これを IronPPT のクリーンで管理された構文と比較してみましょう。

using IronPPT;
using IronPPT.Models;

// Create and save a simple presentation with IronPPT
var document = new PresentationDocument();
document.Save("presentation.pptx");
using IronPPT;
using IronPPT.Models;

// Create and save a simple presentation with IronPPT
var document = new PresentationDocument();
document.Save("presentation.pptx");
Imports IronPPT
Imports IronPPT.Models

' Create and save a simple presentation with IronPPT
Private document = New PresentationDocument()
document.Save("presentation.pptx")
$vbLabelText   $csharpLabel

結論: 現代の.NETプロジェクトにとっての明確な勝者

C# アプリケーションに PowerPoint 自動化を組み込む場合、[Microsoft Office Interop PowerPoint](https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff761925(v=office.12)と[**IronPPT**](https://ironsoftware.com/csharp/ppt/)のどちらを選択するかは明白です

この記事では、2 つのライブラリの基本的な違いについて説明しました。

*相互運用性は強力ですが、柔軟性に欠けます。プレゼンテーションの作成やプレゼンテーションの他の形式への変換などのタスクを処理できますが、PowerPoint がインストールされている必要があり、STA スレッド制限が適用され、注意しないとメモリ リークが発生するなど、最新のクラウド ネイティブ .NET ワークフローには適合しません。

  • 一方、 IronPPT は、今日の開発環境向けに設計されています。 軽量で、Office がインストールされている必要がなく、Web サーバーや CI/CD パイプラインでシームレスに実行され、使いやすく保守しやすいクリーンで最新の API を提供します。

また、スレッド例外や COM エラーから展開の難しさまで、相互運用性の一般的な落とし穴を強調した実際のコード例を確認し、それらを IronPPT のスムーズで直感的な構文と比較しました。

Interop のレガシーな負担なしに、アプリケーション内での PowerPoint スライドの作成、編集、エクスポートを簡素化することに真剣に取り組んでいる場合、 IronPPT が明らかに勝者です

違いを自分で確認してみませんか? 無料の IronPPT 試用版をダウンロードして、わずか数行の C# コードでプロ品質の PowerPoint ファイルを作成し始めましょう。Office のインストールは必要ありません。

COM オブジェクトとの格闘はやめましょう。 IronPPT を使用して、最新の高速で信頼性の高い .NET ソリューションの配布を開始します。

よくある質問

.NET における PowerPoint 用 Microsoft Office Interop の一般的な欠点は何ですか?

Microsoft Office Interop は Microsoft Office のインストールが必要で、Windows のみをサポートし、サーバーサイドの互換性が低く、スレッドセーフ性に欠け、エラーハンドリングが複雑です。IronPPT は、スタンドアロンでクロスプラットフォームのソリューションを提供し、簡素化された API を備えてこれらの問題に対応します。

IronPPT は .NET アプリケーションでの PowerPoint のオートメーションをどのように強化しますか?

IronPPT は、開発者が Microsoft Office を必要とせずに PowerPoint ファイルを作成、読み取り、編集、変換できるようにするモダンな .NET ライブラリを提供することで、オートメーションを強化します。さまざまなプラットフォームをサポートし、クリーンな構文を提供するため、クラウドベースのシステムに最適です。

.NET PowerPoint ライブラリを使用するためのインストール要件は何ですか?

IronPPTは、C#プロジェクト内でInstall-Package IronPPTコマンドを用いてNuGetパッケージマネージャーコンソールからインストール可能で、Microsoft Officeのインストールは不要です。

IronPPT はクラウド環境に展開できますか?

はい、IronPPT は、AWS Lambda、Azure、Docker コンテナ、Linux サーバーなどのクラウド環境にスムーズに展開でき、Office のインストールは不要です。

なぜ IronPPT は PowerPoint オートメーションのための Interop のより良い代替と考えられるのですか?

IronPPT は、その軽量設計、Office インストールからの独立、多様なプラットフォームのサポート、使いやすいモダンな API により、.NET プロジェクトでの PowerPoint オートメーションを簡素化するため、好ましいとされています。

IronPPT は、PowerPoint プレゼンテーションの作成を C# でどのように簡単にしますか?

IronPPT は、開発者が Interop の複雑さを避けながら、シンプルな API を使用してプレゼンテーションにテキスト、カスタムシェイプ、画像、スタイル付き段落を簡単に追加できるようにすることで、プロセスを簡素化します。

IronPPT は、システムに Microsoft Office または PowerPoint がインストールされている必要がありますか?

いいえ、IronPPT はスタンドアロンライブラリで、Microsoft Office や PowerPoint をインストールする必要がなく、サーバーサイドやクラウド アプリケーションに非常に柔軟に対応します。

何が IronPPT をモダンな .NET ワークフローに適しているのですか?

IronPPT は、その軽量でスタンドアロンな性質、クロスプラットフォームのサポート、および Interop の依存性や冗長性を排除し、サーバーおよびクラウド環境で効率的に動作する能力により、モダンな .NET ワークフローに適しています。

Jordi Bardia
ソフトウェアエンジニア
Jordiは、最も得意な言語がPython、C#、C++であり、Iron Softwareでそのスキルを発揮していない時は、ゲームプログラミングをしています。製品テスト、製品開発、研究の責任を分担し、Jordiは継続的な製品改善において多大な価値を追加しています。この多様な経験は彼を挑戦させ続け、興味を持たせており、Iron Softwareで働くことの好きな側面の一つだと言います。Jordiはフロリダ州マイアミで育ち、フロリダ大学でコンピュータサイエンスと統計学を学びました。