チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル».NET»Spire.Presentation for .NET»文書操作»C#/VB.NET:PowerPoint でスライドを追加または削除する方法
2023-08-24

C#/VB.NET:PowerPoint でスライドを追加または削除する方法

スライドは PowerPoint 文書の基本的な構成要素です。各 PowerPoint プレゼンテーションは、テキスト、図形、表、画像などの異なる要素を含むスライドのシリーズで構成されることがあります。PowerPoint 文書で作業している場合、スライドの追加や削除などの操作を行うことができます。この記事では、Spire.Presentation for .NET を使用して、PowerPoint でスライドを追加または削除する方法を示します。

Spire.Presentation for.NET をインストールします

まず、Spire.Presentation for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。

PM> Install-Package Spire.Presentation

PowerPoint の最後に新しいスライドを追加する

Spire.Presentation for .NET によって提供される Presentation.Slides.Append() メソッドを使用すると、PowerPoint ドキュメントの最後に新しいスライドを追加できます。詳細な手順は次のとおりです。

  • Presentation クラスのインスタンスを初期化します。
  • Presentation.LoadFromFile() メソッドを使用して PowerPoint ドキュメントを読み込みます。
  • Presentation.Slides.Append() メソッドを使用して、ドキュメントの最後に新しいスライドを追加します。
  • Presentation.SaveToFile() メソッドを使用して結果文書を保存します。
  • C#
  • VB.NET
using Spire.Presentation;

namespace AddNewSlideinPowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //Presentation クラスのインスタンスを初期化する
            Presentation presentation = new Presentation();

            //PowerPointドキュメントを読み込む
            presentation.LoadFromFile("Sample.pptx");

            //PowerPointの最後に新しいスライドを追加する
            presentation.Slides.Append();

            //結果文書を保存する
            presentation.SaveToFile("AddSlide.pptx", FileFormat.Pptx2013);
        }
    }
}
Imports Spire.Presentation

Namespace AddNewSlideinPowerPoint
    Class Program
        Private Shared Sub Main(ByVal args() As String)

            'Presentation クラスのインスタンスを初期化する
            Dim presentation As Presentation = New Presentation

            'PowerPointドキュメントを読み込む
            presentation.LoadFromFile("Sample.pptx")

            'PowerPointの最後に新しいスライドを追加する
            presentation.Slides.Append()

            '結果文書を保存する
            presentation.SaveToFile("AddSlide.pptx", FileFormat.Pptx2013)
        End Sub
    End Class
End Namespace

C#/VB.NET:PowerPoint でスライドを追加または削除する方法

PowerPoint で特定のスライドの前に新しいスライドを挿入する

特定のスライドの前にスライドを挿入して、補足情報を追加する必要がある場合もあります。詳細な手順は次のとおりです。

  • Presentation クラスのインスタンスを初期化します。
  • Presentation.LoadFromFile() メソッドを使用して PowerPoint ドキュメントを読み込みます。
  • Presentation.Slides.Insert() メソッドを使用して、指定したスライドの前に新しいスライドを挿入します。
  • Presentation.SaveToFile() メソッドを使用して結果文書を保存します。
  • C#
  • VB.NET
using Spire.Presentation;

namespace InsertSlideinPowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //Presentation クラスのインスタンスを初期化する
            Presentation presentation = new Presentation();

            //PowerPointドキュメントを読み込む
            presentation.LoadFromFile("Sample.pptx");

            //2 番目のスライドの前に新しいスライドを挿入する
            presentation.Slides.Insert(1);

            //結果文書を保存する
            presentation.SaveToFile("InsertSlide.pptx", FileFormat.Pptx2013);
        }
    }
}
Imports Spire.Presentation

Namespace InsertSlideinPowerPoint
    Class Program
        Private Shared Sub Main(ByVal args() As String)

            'Presentation クラスのインスタンスを初期化する
            Dim presentation As Presentation = New Presentation

            'PowerPointドキュメントを読み込む
            presentation.LoadFromFile("Sample.pptx")

            '2 番目のスライドの前に新しいスライドを挿入する
            presentation.Slides.Insert(1)

            '結果文書を保存する
            presentation.SaveToFile("InsertSlide.pptx", FileFormat.Pptx2013)
        End Sub
    End Class
End Namespace

C#/VB.NET:PowerPoint でスライドを追加または削除する方法

PowerPoint から特定のスライドを削除する

不要なスライドを文書から削除したい場合は、Presentation.Slides.RemoveAt(int index) メソッドを使用できます。詳細な手順は次のとおりです。

  • Presentation クラスのインスタンスを初期化します。
  • Presentation.LoadFromFile() メソッドを使用して PowerPoint ドキュメントを読み込みます。
  • Presentation.Slides.RemoveAt() メソッドを使用して、指定したスライドをドキュメントから削除します。
  • Presentation.SaveToFile() メソッドを使用して結果文書を保存します。
  • C#
  • VB.NET
using Spire.Presentation;

namespace DeletePowerPointSlide
{
    class Program
    {
        static void Main(string[] args)
        {
            //Presentation クラスのインスタンスを初期化する
            Presentation presentation = new Presentation();

            //PowerPointドキュメントを読み込む
            presentation.LoadFromFile("Sample.pptx");

            //4番目のスライドを削除する
            presentation.Slides.RemoveAt(3);

            //結果文書を保存する
            presentation.SaveToFile("RemoveSlide.pptx", FileFormat.Pptx2013);
        }
    }
}
Imports Spire.Presentation

Namespace DeletePowerPointSlide
    Class Program
        Private Shared Sub Main(ByVal args() As String)

            'Presentation クラスのインスタンスを初期化する
            Dim presentation As Presentation = New Presentation

            'PowerPointドキュメントを読み込む
            presentation.LoadFromFile("Sample.pptx")

            '4番目のスライドを削除する	
            presentation.Slides.RemoveAt(3)

            '結果文書を保存する
            presentation.SaveToFile("RemoveSlide.pptx", FileFormat.Pptx2013)
        End Sub
    End Class
End Namespace

C#/VB.NET:PowerPoint でスライドを追加または削除する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 363 times