PHP を使用したプレゼンテーションでの箇条書きリストと番号付きリストの管理
概要
Aspose.Slides for PHP via Java を使用すると、PowerPoint および OpenDocument プレゼンテーションで箇条書きリストや番号付きリストを作成および書式設定できます。リスト項目は段落であり、箇条書きの設定は段落の書式を通じて制御されます。
Paragraph::getParagraphFormat メソッドを使用して段落レベルのリスト設定にアクセスします。主要なエントリーポイントは ParagraphFormat::getBullet で、BulletFormat オブジェクトを返します。このオブジェクトを使用して、箇条書きの種類、記号、画像、色、サイズ、番号付スタイル、開始番号を設定できます。
この記事では以下をご紹介します。
- カスタム記号を使用した箇条書きリストを作成する
- 画像箇条書きを作成する
- 段落の深さを設定して階層リストを作成する
- 番号付きリストを作成する
- 既存のプレゼンテーションでリストの書式を検査および変更する
- 段落テキストと共に実際のリストマーカーと番号を読み取る
箇条書きリストを作成する
箇条書きリストを作成するには、Paragraph オブジェクトを TextFrame に追加し、BulletFormat::setType を BulletType::Symbol に設定します。その後、BulletFormat::setChar、BulletFormat::getColor、BulletFormat::setHeight を設定して箇条書きの外観を制御できます。
以下の PHP コードはスライドで箇条書きリストを作成する方法を示しています。
use aspose\slides\BulletType;
use aspose\slides\NullableBool;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
function createParagraph($paragraphText)
{
$paragraph = new Paragraph();
$paragraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$paragraph->getParagraphFormat()->getBullet()->setChar("*");
$paragraph->getParagraphFormat()->setIndent(15);
$paragraph->getParagraphFormat()->getBullet()->setBulletHardColor(NullableBool::True);
$bulletColor = new Java("java.awt.Color", 205, 92, 92);
$paragraph->getParagraphFormat()->getBullet()->getColor()->setColor($bulletColor);
$paragraph->getParagraphFormat()->getBullet()->setHeight(100);
$paragraph->setText($paragraphText);
return $paragraph;
}
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 200, 50);
$textFrame = $autoShape->getTextFrame();
$textFrame->getParagraphs()->clear();
$paragraph1 = createParagraph("The first paragraph");
$textFrame->getParagraphs()->add($paragraph1);
$paragraph2 = createParagraph("The second paragraph");
$textFrame->getParagraphs()->add($paragraph2);
$presentation->save("symbol_bullets.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
結果:

番号付きリストを作成する
項目の順序が重要な場合は番号付きリストを使用します。BulletFormat::setType を BulletType::Numbered に設定します。また、BulletFormat::setNumberedBulletStyle で番号付スタイルを選択したり、BulletFormat::setNumberedBulletStartWith で 1 以外の開始番号を指定したりできます。
以下の PHP コードはスライドで番号付きリストを作成する方法を示しています。
use aspose\slides\BulletType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 90, 80);
$textFrame = $autoShape->getTextFrame();
$textFrame->getParagraphs()->clear();
$paragraph1 = new Paragraph();
$paragraph1->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$paragraph1->setText("Apple");
$textFrame->getParagraphs()->add($paragraph1);
$paragraph2 = new Paragraph();
$paragraph2->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$paragraph2->setText("Orange");
$textFrame->getParagraphs()->add($paragraph2);
$paragraph3 = new Paragraph();
$paragraph3->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$paragraph3->setText("Banana");
$textFrame->getParagraphs()->add($paragraph3);
$presentation->save("numbered_bullets.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
結果:

画像箇条書きを作成する
Aspose.Slides を使用すると、通常の箇条書き記号を画像に置き換えることができます。画像箇条書きは、アイコンや小さな透過 PNG ファイルなど、サイズが小さくても読みやすいシンプルな画像で最適に機能します。
Note
通常の箇条書き記号を画像に置き換える場合は、透過背景を持つシンプルなグラフィックを選択するのが最適です。そのような画像はカスタム箇条書き記号としてうまく機能します。画像は非常に小さなサイズに縮小されるため、リストの箇条書きとして使用したときに鮮明で視覚的に効果的な画像を選択することを強く推奨します。
画像箇条書きを作成するには、Presentation::getImages に画像を追加し、返された PPImage オブジェクトを BulletFormat::getPicture に割り当てます。画像を割り当てる前に、BulletFormat::setType を BulletType::Picture に設定してください。
例として「image.png」を使用するとします:

以下の PHP コードはスライドで画像箇条書きを作成する方法を示しています。
use aspose\slides\BulletType;
use aspose\slides\Images;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
function createParagraph($paragraphText, $bulletImage)
{
$paragraph = new Paragraph();
$paragraph->getParagraphFormat()->getBullet()->setType(BulletType::Picture);
$paragraph->getParagraphFormat()->getBullet()->getPicture()->setImage($bulletImage);
$paragraph->getParagraphFormat()->setIndent(15);
$paragraph->getParagraphFormat()->getBullet()->setHeight(100);
$paragraph->setText($paragraphText);
return $paragraph;
}
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 200, 50);
$textFrame = $autoShape->getTextFrame();
$textFrame->getParagraphs()->clear();
$image = Images::fromFile("image.png");
$bulletImage = $presentation->getImages()->addImage($image);
$paragraph1 = createParagraph("The first paragraph", $bulletImage);
$textFrame->getParagraphs()->add($paragraph1);
$paragraph2 = createParagraph("The second paragraph", $bulletImage);
$textFrame->getParagraphs()->add($paragraph2);
$presentation->save("picture_bullets.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
結果:

階層リストを作成する
ParagraphFormat::setDepth を使用して、リスト項目を異なるレベルに配置します。レベル 0 が最上位レベル、レベル 1 がその下位にネストされる、といった具合です。
以下の PHP コードは階層箇条書きリストを作成する方法を示しています。
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 260, 110);
$textFrame = $autoShape->getTextFrame();
$textFrame->getParagraphs()->clear();
$paragraph1 = new Paragraph();
$paragraph1->getParagraphFormat()->setDepth(0);
$paragraph1->setText("My text - Depth 0");
$textFrame->getParagraphs()->add($paragraph1);
$paragraph2 = new Paragraph();
$paragraph2->getParagraphFormat()->setDepth(1);
$paragraph2->setText("My text - Depth 1");
$textFrame->getParagraphs()->add($paragraph2);
$paragraph3 = new Paragraph();
$paragraph3->getParagraphFormat()->setDepth(2);
$paragraph3->setText("My text - Depth 2");
$textFrame->getParagraphs()->add($paragraph3);
$paragraph4 = new Paragraph();
$paragraph4->getParagraphFormat()->setDepth(3);
$paragraph4->setText("My text - Depth 3");
$textFrame->getParagraphs()->add($paragraph4);
$presentation->save("multilevel_bullets.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
結果:

既存のリストを変更する
既存のプレゼンテーションでリストの書式を変更するには、対象の段落にアクセスし、ParagraphFormat::getBullet 設定を更新します。リスト作成時と同じプロパティを使用して、PPT、PPTX、または ODP ファイルから読み込んだリストを検査または修正できます。
以下の PHP コードはテキストフレーム内の最初の段落を番号付きリストスタイルに変更します。
use aspose\slides\BulletType;
use aspose\slides\NumberedBulletStyle;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("input.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$paragraph->getParagraphFormat()->getBullet()->setNumberedBulletStyle(NumberedBulletStyle::BulletRomanUCPeriod);
$paragraph->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(1);
$paragraph->getParagraphFormat()->setMarginLeft(30);
$paragraph->getParagraphFormat()->setIndent(-20);
$presentation->save("updated_list.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
実際の箇条書き値を読み取る
プレゼンテーションコンテンツを抽出する際は、段落テキストとともにリストマーカーを保持します。ParagraphFormat::getEffective を呼び出し、ParagraphFormatEffectiveData::getBullet にアクセスして有効な箇条書き書式を取得します。取得したオブジェクトの getType を確認してから、getActualBulletValue を呼び出します。
番号付きリストの場合、以下の 3 つの値を区別します。
- getNumberedBulletStyle は、例えばピリオド付きアラビア数字やピリオド付き小文字アルファベットなど、書式を決定します。
- getNumberedBulletStartWith は、番号付き段落のグループの開始番号を指定します。
- getActualBulletValue は特定の段落に対するフォーマット済みマーカーを返します。たとえば、5 から開始するリストの 2 番目の項目は
6.を返します。
各段落に対して getDepth を保持すると、ネストレベルが維持されます。実際のマーカーは完全な階層をエンコードしません。ネストされた項目は c. のように返すことがあり、6.c. のように結合されたラベルは返しません。
以下の自己完結型サンプルはリストを作成し、プレゼンテーションに保存した後、プレゼンテーションを再読込して各段落のテキスト、深さ、箇条書きタイプ、実際の値を報告します。小さな青い画像マーカーはインライン SVG から作成されます。
use aspose\slides\BulletType;
use aspose\slides\FillType;
use aspose\slides\NullableBool;
use aspose\slides\NumberedBulletStyle;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\SlideSizeScaleType;
use aspose\slides\SvgImage;
$presentation = new Presentation();
try {
$presentation->getSlideSize()->setSize(720, 440, SlideSizeScaleType::DoNotScale);
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 30, 20, 660, 400);
$shape->getFillFormat()->setFillType(FillType::NoFill);
$shape->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
// 外部画像ファイルなしで画像マーカーを作成します。
$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>");
$bulletImage = $presentation->getImages()->addImage($svg);
$black = new Java("java.awt.Color", 0, 0, 0);
$addParagraph = function ($text, $depth, $type, $start, $style) use ($textFrame, $bulletImage, $black) {
$paragraph = new Paragraph();
$paragraph->setText($text);
$format = $paragraph->getParagraphFormat();
$format->setDepth($depth);
$format->setMarginLeft(30 + $depth * 40);
$format->setIndent(-28);
$format->setSpaceAfter(8);
$format->getDefaultPortionFormat()->setFontHeight(22);
$format->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$format->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor($black);
$format->getBullet()->setType($type);
$format->getBullet()->setBulletHardColor(NullableBool::True);
$format->getBullet()->getColor()->setColor($black);
if ($type == BulletType::Numbered) {
$format->getBullet()->setNumberedBulletStyle($style);
$format->getBullet()->setNumberedBulletStartWith($start);
} else if ($type == BulletType::Symbol) {
$format->getBullet()->setChar("\u{2022}");
} else if ($type == BulletType::Picture) {
$format->getBullet()->getPicture()->setImage($bulletImage);
}
$textFrame->getParagraphs()->add($paragraph);
};
$addParagraph("First item", 0, BulletType::Numbered, 5, NumberedBulletStyle::BulletArabicPeriod);
$addParagraph("Second item", 0, BulletType::Numbered, 5, NumberedBulletStyle::BulletArabicPeriod);
$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, NumberedBulletStyle::BulletArabicPeriod);
$addParagraph("Symbol marker", 0, BulletType::Symbol, 1, NumberedBulletStyle::BulletArabicPeriod);
$addParagraph("No bullet", 0, BulletType::None, 1, NumberedBulletStyle::BulletArabicPeriod);
$addParagraph("Picture marker", 0, BulletType::Picture, 1, NumberedBulletStyle::BulletArabicPeriod);
$presentation->save("actual_bullet_values.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
$loadedPresentation = new Presentation("actual_bullet_values.pptx");
try {
$loadedShape = $loadedPresentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
echo "Text | Depth | Type | Actual value\n";
$paragraphs = $loadedShape->getTextFrame()->getParagraphs();
for ($i = 0; $i < java_values($paragraphs->getCount()); $i++) {
$paragraph = $paragraphs->get_Item($i);
$bullet = $paragraph->getParagraphFormat()->getEffective()->getBullet();
switch (java_values($bullet->getType())) {
case BulletType::Numbered:
case BulletType::Symbol:
$marker = $bullet->getActualBulletValue();
break;
case BulletType::None:
$marker = "null (no bullet)";
break;
case BulletType::Picture:
$marker = "null (image required)";
break;
default:
$marker = "unsupported bullet type";
break;
}
$typeNames = [BulletType::Numbered => "Numbered", BulletType::Symbol => "Symbol", BulletType::None => "None", BulletType::Picture => "Picture"];
$typeName = $typeNames[java_values($bullet->getType())] ?? "Unknown";
echo $paragraph->getText() . " | " . $paragraph->getParagraphFormat()->getDepth() . " | " . $typeName . " | " . $marker . "\n";
}
} finally {
$loadedPresentation->dispose();
}
ソースリストは以下に示されています。

レポートは 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 and BulletType::Picture に対して、getActualBulletValue は null を返し、空文字列は返しません。これらのタイプは別々に処理してください。前者はマーカーがなく、後者は箇条書き画像を別途抽出する必要があります。画像箇条書きはテキストマーカーだけを抽出して再現することはできません。
FAQ
箇条書きや番号付きリストは PDF や画像にエクスポートできますか?
はい。Aspose.Slides は、対象フォーマットが対応するテキストレイアウトと箇条書き機能をサポートしている場合、リストの書式を保持したままエクスポートします。
既存のプレゼンテーションでリストを編集できますか?
はい。プレゼンテーションを読み込み、対象の段落にアクセスし、ParagraphFormat::getBullet 設定を検査または更新してからプレゼンテーションを保存します。
リストに非ラテン文字を含めることはできますか?
はい。リスト項目のテキストは Unicode 文字を含めることができるため、複数言語のプレゼンテーションでリストを作成できます。使用するフォントが必要な文字をサポートしていることを確認してください。