.NET のプレゼンテーションで箇条書きと番号付きリストを管理する

概要

Aspose.Slides for .NET を使用すると、PowerPoint および OpenDocument プレゼンテーションで箇条書きリストと番号付きリストを作成および書式設定できます。リスト項目は、段落形式によって箇条書き設定が制御される段落です。

段落レベルのリスト設定にアクセスするには、IParagraph.ParagraphFormat プロパティを使用します。主要なエントリポイントは IParagraphFormat.Bullet で、IBulletFormat オブジェクトを返します。このオブジェクトを使用して、箇条書きのタイプ、シンボル、画像、色、サイズ、番号付スタイル、開始番号を設定できます。

この記事では、以下の方法を示します:

  • カスタムシンボルを使用した箇条書きリストを作成する
  • 画像箇条書きを作成する
  • 段落の深さを設定して多層リストを作成する
  • 番号付きリストを作成する
  • 既存のプレゼンテーションでリストの書式設定を検査および変更する
  • 段落テキストとともに実際のリストマーカーと番号を取得する

箇条書きリストの作成

箇条書きリストを作成するには、IParagraph オブジェクトを ITextFrame に追加し、IBulletFormat.Type を BulletType.Symbol に設定します。その後、IBulletFormat.Char、IBulletFormat.Color、IBulletFormat.Height を設定して箇条書きの外観を制御できます。

以下の C# コードは、スライドで箇条書きリストを作成する方法を示しています:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

static Paragraph CreateParagraph(string text)
{
    var paragraph = new Paragraph();
    paragraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
    paragraph.ParagraphFormat.Bullet.Char = '*';
    paragraph.ParagraphFormat.Indent = 15;
    paragraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
    paragraph.ParagraphFormat.Bullet.Color.Color = Color.IndianRed;
    paragraph.ParagraphFormat.Bullet.Height = 100;
    paragraph.Text = text;
    return paragraph;
}

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 200, 50);

var textFrame = autoShape.TextFrame;
textFrame.Paragraphs.Clear();

var paragraph1 = CreateParagraph("The first paragraph");
textFrame.Paragraphs.Add(paragraph1);

var paragraph2 = CreateParagraph("The second paragraph");
textFrame.Paragraphs.Add(paragraph2);

presentation.Save("symbol_bullets.pptx", SaveFormat.Pptx);

結果:

シンボルの箇条書き

番号付きリストの作成

項目の順序が重要な場合は、番号付きリストを使用します。IBulletFormat.Type を BulletType.Numbered に設定します。また、IBulletFormat.NumberedBulletStyle で番号付形式を選択したり、リストを 1 以外の値から開始する場合は IBulletFormat.NumberedBulletStartWith を設定できます。

以下の C#コードは、スライドで番号付きリストを作成する方法を示しています:

using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 90, 80);

var textFrame = autoShape.TextFrame;
textFrame.Paragraphs.Clear();

var paragraph1 = new Paragraph();
paragraph1.ParagraphFormat.Bullet.Type = BulletType.Numbered;
paragraph1.Text = "Apple";
textFrame.Paragraphs.Add(paragraph1);

var paragraph2 = new Paragraph();
paragraph2.ParagraphFormat.Bullet.Type = BulletType.Numbered;
paragraph2.Text = "Orange";
textFrame.Paragraphs.Add(paragraph2);

var paragraph3 = new Paragraph();
paragraph3.ParagraphFormat.Bullet.Type = BulletType.Numbered;
paragraph3.Text = "Banana";
textFrame.Paragraphs.Add(paragraph3);

presentation.Save("numbered_bullets.pptx", SaveFormat.Pptx);

結果:

番号付き箇条書き

画像箇条書きの作成

Aspose.Slides では、通常の箇条書きシンボルを画像に置き換えることができます。画像箇条書きは、アイコンや小さな透過 PNG ファイルなど、サイズが小さくても可読性を保つシンプルな画像で最適に機能します。

画像箇条書きを作成するには、Presentation.Images に画像を追加し、返された画像オブジェクトを IBulletFormat.Picture に割り当てます。画像を割り当てる前に、IBulletFormat.Type を BulletType.Picture に設定します。

例えば、“image.png” があるとします:

箇条書き用画像

以下の C# コードは、スライドで画像箇条書きを作成する方法を示しています:

using Aspose.Slides;
using Aspose.Slides.Export;

static Paragraph CreateParagraph(string text, IPPImage image)
{
    var paragraph = new Paragraph();
    paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
    paragraph.ParagraphFormat.Bullet.Picture.Image = image;
    paragraph.ParagraphFormat.Indent = 15;
    paragraph.ParagraphFormat.Bullet.Height = 100;
    paragraph.Text = text;
    return paragraph;
}

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 200, 50);

var textFrame = autoShape.TextFrame;
textFrame.Paragraphs.Clear();

var imageBytes = File.ReadAllBytes("image.png");
var bulletImage = presentation.Images.AddImage(imageBytes);

var paragraph1 = CreateParagraph("The first paragraph", bulletImage);
textFrame.Paragraphs.Add(paragraph1);

var paragraph2 = CreateParagraph("The second paragraph", bulletImage);
textFrame.Paragraphs.Add(paragraph2);

presentation.Save("picture_bullets.pptx", SaveFormat.Pptx);

結果:

画像箇条書き

多層リストの作成

IParagraphFormat.Depth を使用して、リスト項目を異なるレベルに配置します。レベル 0 が最上位、レベル 1 がその下位にネストされ、以下同様です。

以下の C# コードは、多層箇条書きリストを作成する方法を示しています:

using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 260, 110);

var textFrame = autoShape.TextFrame;
textFrame.Paragraphs.Clear();

var paragraph1 = new Paragraph();
paragraph1.ParagraphFormat.Depth = 0;
paragraph1.Text = "My text - Depth 0";
textFrame.Paragraphs.Add(paragraph1);

var paragraph2 = new Paragraph();
paragraph2.ParagraphFormat.Depth = 1;
paragraph2.Text = "My text - Depth 1";
textFrame.Paragraphs.Add(paragraph2);

var paragraph3 = new Paragraph();
paragraph3.ParagraphFormat.Depth = 2;
paragraph3.Text = "My text - Depth 2";
textFrame.Paragraphs.Add(paragraph3);

var paragraph4 = new Paragraph();
paragraph4.ParagraphFormat.Depth = 3;
paragraph4.Text = "My text - Depth 3";
textFrame.Paragraphs.Add(paragraph4);

presentation.Save("multilevel_bullets.pptx", SaveFormat.Pptx);

結果:

多層リスト

既存リストの変更

既存のプレゼンテーションでリストの書式設定を変更するには、対象の段落にアクセスし、IParagraphFormat.Bullet 設定を更新します。リスト作成時に使用したプロパティは、PPT、PPTX、または ODP ファイルから読み込んだリストを検査または変更する際にも使用できます。

以下の C# コードは、テキストフレーム内の最初の段落を番号付きリストスタイルに変更します:

using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("input.pptx");
var slide = presentation.Slides[0];
var autoShape = (IAutoShape)slide.Shapes[0];

var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
paragraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletRomanUCPeriod;
paragraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 1;
paragraph.ParagraphFormat.MarginLeft = 30;
paragraph.ParagraphFormat.Indent = -20;

presentation.Save("updated_list.pptx", SaveFormat.Pptx);

実際の箇条書き値の取得

プレゼンテーションのコンテンツを抽出する際は、リストマーカーを段落テキストとともに保持します。IParagraphFormat.GetEffective を呼び出し、IParagraphFormatEffectiveData.Bullet にアクセスして実際の箇条書き書式を取得します。ActualBulletValue を読む前に、その Type を確認してください。

番号付きリストの場合、次の 3 つの値を区別します:

  • NumberedBulletStyle は、アラビア数字の後にピリオドが付く形式や小文字のアルファベットの後にピリオドが付く形式など、フォーマットを決定します。
  • NumberedBulletStartWith は、番号付き段落のグループの開始番号を指定します。
  • ActualBulletValue は、特定の段落に対する書式化されたマーカーを返します。例えば、5 から開始するリストの2番目の項目は 6. を返します。

各段落で Depth を保持してネストレベルを維持してください。実際のマーカーは完全な階層をエンコードしません。ネストされた項目は 6.c. のような結合ラベルではなく、c. のみを返すことがあります。

以下の自己完結型サンプルは、リストを作成し、プレゼンテーションに保存し、プレゼンテーションを再読み込みして各段落のテキスト、深さ、箇条書きタイプ、および実際の値を報告します。小さな青い画像マーカーはインライン SVG から作成されます。

using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation())
{
    presentation.SlideSize.SetSize(720, 440, SlideSizeScaleType.DoNotScale);
    var slide = presentation.Slides[0];

    var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 30, 20, 660, 400);
    shape.FillFormat.FillType = FillType.NoFill;
    shape.LineFormat.FillFormat.FillType = FillType.NoFill;

    var textFrame = shape.TextFrame;
    textFrame.Paragraphs.Clear();

    // 外部画像ファイルを使用せずに画像マーカーを作成します。
    var svg = new SvgImage("<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16'><rect width='16' height='16' rx='4' fill='#0078D4'/></svg>");
    var bulletImage = presentation.Images.AddImage(svg);

    void AddParagraph(string text, short depth, BulletType type, short start = 1, NumberedBulletStyle style = NumberedBulletStyle.BulletArabicPeriod)
    {
        var paragraph = new Paragraph { Text = text };

        var format = paragraph.ParagraphFormat;
        format.Depth = depth;
        format.MarginLeft = 30 + depth * 40;
        format.Indent = -28;
        format.SpaceAfter = 8;
        format.DefaultPortionFormat.FontHeight = 22;
        format.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
        format.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
        format.Bullet.Type = type;
        format.Bullet.IsBulletHardColor = NullableBool.True;
        format.Bullet.Color.Color = Color.Black;
        if (type == BulletType.Numbered)
        {
            format.Bullet.NumberedBulletStyle = style;
            format.Bullet.NumberedBulletStartWith = start;
        }
        else if (type == BulletType.Symbol)
        {
            format.Bullet.Char = '\u2022';
        }
        else if (type == BulletType.Picture)
        {
            format.Bullet.Picture.Image = bulletImage;
        }

        textFrame.Paragraphs.Add(paragraph);
    }

    AddParagraph("First item", 0, BulletType.Numbered, 5);
    AddParagraph("Second item", 0, BulletType.Numbered, 5);
    AddParagraph("Nested item", 1, BulletType.Numbered, 3, NumberedBulletStyle.BulletAlphaLCPeriod);
    AddParagraph("Next nested item", 1, BulletType.Numbered, 3, NumberedBulletStyle.BulletAlphaLCPeriod);
    AddParagraph("Deep item", 2, BulletType.Numbered, 4, NumberedBulletStyle.BulletRomanLCPeriod);
    AddParagraph("Back to top level", 0, BulletType.Numbered, 5);
    AddParagraph("Symbol marker", 0, BulletType.Symbol);
    AddParagraph("No bullet", 0, BulletType.None);
    AddParagraph("Picture marker", 0, BulletType.Picture);
    presentation.Save("actual_bullet_values.pptx", SaveFormat.Pptx);
}

using var loadedPresentation = new Presentation("actual_bullet_values.pptx");
var loadedShape = (IAutoShape)loadedPresentation.Slides[0].Shapes[0];
Console.WriteLine("Text | Depth | Type | Actual value");
foreach (var paragraph in loadedShape.TextFrame.Paragraphs)
{
    var bullet = paragraph.ParagraphFormat.GetEffective().Bullet;
    string marker;
    switch (bullet.Type)
    {
        case BulletType.Numbered:
        case BulletType.Symbol:
            marker = bullet.ActualBulletValue;
            break;
        case BulletType.None:
            marker = $"{bullet.ActualBulletValue ?? "null"} (no bullet)";
            break;
        case BulletType.Picture:
            marker = $"{bullet.ActualBulletValue ?? "null"} (image required)";
            break;
        default:
            marker = "unsupported bullet type";
            break;
    }
    Console.WriteLine($"{paragraph.Text} | {paragraph.ParagraphFormat.Depth} | {bullet.Type} | {marker}");
}

ソースリストは以下に示されています。

5 から開始するリスト、入れ子になった文字とローマ数字の項目、シンボルマーカー、箇条書きなしの段落、青い画像マーカー

レポートは PowerPoint に表示されるマーカーと一致します:

Text | Depth | Type | Actual value
First item | 0 | Numbered | 5.
Second item | 0 | Numbered | 6.
Nested item | 1 | Numbered | c.
Next nested item | 1 | Numbered | d.
Deep item | 2 | Numbered | iv.
Back to top level | 0 | Numbered | 7.
Symbol marker | 0 | Symbol | •
No bullet | 0 | None | null (no bullet)
Picture marker | 0 | Picture | null (image required)

BulletType.None および BulletType.Picture の場合、ActualBulletValue は空文字列ではなく null を返します。これらのタイプは個別に処理してください。前者はマーカーがなく、後者は箇条書き画像を別途抽出する必要があります。画像箇条書きは、テキストマーカーだけを抽出して再現することはできません。

よくある質問

箇条書きおよび番号付きリストを PDF や画像にエクスポートできますか?

はい。対象フォーマットが対応するテキストレイアウトおよび箇条書き機能をサポートしている場合、Aspose.Slides はリスト書式を保持します。

既存のプレゼンテーションでリストを編集できますか?

はい。プレゼンテーションを読み込み、対象の段落にアクセスし、その IParagraphFormat.Bullet 設定を検査または更新してから、プレゼンテーションを保存します。

リストに非ラテン文字を含めることはできますか?

はい。リスト項目のテキストは Unicode 文字を含めることができるため、多言語プレゼンテーションでリストを作成できます。プレゼンテーションで使用するフォントが必要な文字をサポートしていることを確認してください。