0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#コードを使用して、PowerPointのセクション内でスライドを挿入、取得、並べ替え、および削除します

0
Posted at

PowerPoint のセクション機能を使用すると、関連するスライドをグループ化でき、プレゼンテーションをトピック、章、またはその他の論理的な構造ごとに分割しやすくなります。大規模で複数のセクションを含むプレゼンテーションを扱う場合、スライドの挿入、取得、並べ替え、削除などの操作を自動化することで、生産性を大幅に向上させることができます。この記事では、Spire.Presentation for .NET を使用して、C#でPPTセクション内のスライドを挿入、取得、並べ替え、および削除する方法を説明します。

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

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

PM> Install-Package Spire.Presentation

C#でPowerPointセクションにスライドを挿入する

セクションに新しいコンテンツを追加する必要がある場合、スライドの挿入はよく行われます。Spire.Presentation for .NET を使用すると、Section.Insert() メソッドを使ってセクションにスライドを挿入できます。詳細な手順は次のとおりです。

  1. Presentation クラスのインスタンスを作成します
  2. Presentation.LoadFromFile() メソッドを使用してPowerPointプレゼンテーションを読み込みます
  3. Presentation.SectionList(index) プロパティを使用して、インデックス(0から始まる)で特定のセクションを取得します
  4. プレゼンテーションに新しいスライドを追加し、Section.Insert() メソッドを使用してそのスライドをセクションに挿入します
  5. 追加したスライドをプレゼンテーションから削除します
  6. Presentation.SaveToFile() メソッドを使用して結果のプレゼンテーションを保存します

サンプルコードは以下のとおりです:

using Spire.Presentation;
using System.Collections.Generic;

namespace InsertSlidesInSection
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Presentation クラスのインスタンスを作成
            using (Presentation presentation = new Presentation())
            {
                // PowerPoint プレゼンテーションを読み込む
                presentation.LoadFromFile("Example.pptx");

                // 最初のセクションにアクセス
                Section firstSection = presentation.SectionList[0];

                // プレゼンテーションに新しいスライドを追加し、セクションの先頭に挿入する
                ISlide slide = presentation.Slides.Append();
                firstSection.Insert(0, slide);

                // 追加したスライドをプレゼンテーションから削除する
                presentation.Slides.Remove(slide);

                // 変更したプレゼンテーションを保存する
                presentation.SaveToFile("InsertSlidesInSection.pptx", FileFormat.Pptx2016);
            }
        }
    }
}

C#でPowerPointセクションからスライドを取得する

特定のセクションからスライドを抽出することで、スライドの並べ替えや特定の書式設定の適用など、対象を絞った操作を行うことができます。Spire.Presentation for .NET の Section.GetSlides() メソッドを使用すると、指定したセクション内のすべてのスライドを簡単に取得できます。詳細な手順は次のとおりです。

  1. Presentation クラスのインスタンスを作成します
  2. Presentation.LoadFromFile() メソッドを使用してPowerPointプレゼンテーションを読み込みます
  3. Presentation.SectionList(index) プロパティを使用して、インデックス(0から始まる)で特定のセクションを取得します
  4. Section.GetSlides() メソッドを使用して、そのセクション内のスライドを取得します
  5. 取得したスライドを反復処理し、各スライドのスライド番号(1から始まる)を取得します

サンプルコードは以下のとおりです:

using Spire.Presentation;
using System;

namespace RetrieveSlidesInSection
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Presentation クラスのインスタンスを作成
            using (Presentation presentation = new Presentation())
            {
                // PowerPoint プレゼンテーションを読み込む
                presentation.LoadFromFile("Example.pptx");

                // 3番目のセクションのスライドを取得
                Section section = presentation.SectionList[2];
                ISlide[] slides = section.GetSlides();

                // セクション内の各スライドのスライド番号を出力する
                foreach (ISlide slide in slides)
                {
                    Console.Write(slide.SlideNumber + " ");
                }

                Console.ReadKey();
            }
        }
    }
}

C#でPowerPointセクション内のスライドを並べ替える

関連するコンテンツを論理的な順序で並べるためには、スライドの並べ替えが重要です。Spire.Presentation for .NET では、Section.Move() メソッドを使用して、セクション内のスライドを別の位置へ移動できます。詳細な手順は次のとおりです。

  1. Presentation クラスのインスタンスを作成します
  2. Presentation.LoadFromFile() メソッドを使用してPowerPointプレゼンテーションを読み込みます
  3. Presentation.SectionList(index) プロパティを使用して、インデックス(0から始まる)で特定のセクションを取得します
  4. Section.Move() メソッドを使用して、セクション内の特定のスライドを別の位置へ移動します
  5. Presentation.SaveToFile() メソッドを使用して結果のプレゼンテーションを保存します

サンプルコードは以下のとおりです:

using Spire.Presentation;

namespace ReorderSlidesInSection
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Presentation クラスのインスタンスを作成
            using (Presentation presentation = new Presentation())
            {
                // PowerPoint プレゼンテーションを読み込む
                presentation.LoadFromFile("Example.pptx");

                // 3番目のセクションにアクセス
                Section section = presentation.SectionList[2];

                // セクション内のスライドを取得
                ISlide[] slides = section.GetSlides();

                // セクション内の1番目のスライドを指定位置へ移動
                section.Move(2, slides[0]);

                // 変更したプレゼンテーションを保存する
                presentation.SaveToFile("ReorderSlidesInSection.pptx", FileFormat.Pptx2016);
            }
        }
    }
}

C#でPowerPointセクションからスライドを削除する

セクションからスライドを削除することで、プレゼンテーションを整理しやすくなり、特定のスライドが古くなったり不要になった場合に特に有効です。Spire.Presentation for .NET の Section.RemoveAt() または Section.RemoveRange() メソッドを使用すると、セクション内の個別スライドやスライド範囲を簡単に削除できます。詳細な手順は次のとおりです。

  1. Presentation クラスのインスタンスを作成します
  2. Presentation.LoadFromFile() メソッドを使用してPowerPointプレゼンテーションを読み込みます
  3. Presentation.SectionList(index) プロパティを使用して、インデックス(0から始まる)で特定のセクションを取得します
  4. Section.RemoveAt() または Section.RemoveRange() メソッドを使用して、セクションから特定のスライドまたはスライド範囲を削除します
  5. Presentation.SaveToFile() メソッドを使用して結果のプレゼンテーションを保存します

サンプルコードは以下のとおりです:

using Spire.Presentation;

namespace RemoveSlidesInSection
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Presentation クラスのインスタンスを作成
            using (Presentation presentation = new Presentation())
            {
                // PowerPoint プレゼンテーションを読み込む
                presentation.LoadFromFile("Course.pptx");

                // 3番目のセクションにアクセス
                Section section = presentation.SectionList[2];

                // セクションの最初のスライドを削除する
                section.RemoveAt(0);

                // またはセクション内のスライド範囲を削除する
                // section.RemoveRange(0, 2);

                // 変更したプレゼンテーションを保存する
                presentation.SaveToFile("RemoveSlidesInSection.pptx", FileFormat.Pptx2016);
            }
        }
    }
}

一時ライセンスの申請

生成されたドキュメントから評価メッセージを削除したり、機能制限を解除したりしたい場合は、30日間の試用ライセンスを申請してください。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?