使用 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 選擇編號格式,或在清單需要從非 1 的值開始時,使用 BulletFormat::setNumberedBulletStartWith。
以下 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 以取得有效的項目符號格式。於讀取 getActualBulletValue 前,先檢查其 getType。
對於編號清單,請區分以下三個值:
- getNumberedBulletStyle 決定格式,例如以句點結尾的阿拉伯數字或以句點結尾的小寫字母。
- getNumberedBulletStartWith 指定一組編號段落的起始編號。
- getActualBulletValue 會回傳特定段落的格式化標記。例如,從 5 開始的清單中第二項會回傳
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,而非空字串。請分別處理這些類型:前者沒有標記,後者則需另外擷取項目符號圖片。僅透過擷取文字標記無法重現圖片項目符號。
常見問題
項目符號與編號清單可以匯出為 PDF 或影像嗎?
是。當目標格式支援相應的文字佈局和項目符號功能時,Aspose.Slides 會保留清單格式。
我可以編輯既有簡報中的清單嗎?
是。載入簡報,存取目標段落,檢查或更新其 ParagraphFormat::getBullet 設定,然後儲存簡報。
清單可以包含非拉丁文字嗎?
是。清單項目文字可以包含 Unicode 字元,您可在多語言簡報中建立清單。請確保簡報使用的字型支援所需的字元。