管理 C++ 演示文稿中的项目符号和编号列表
概述
Aspose.Slides for C++ 允许您在 PowerPoint 和 OpenDocument 演示文稿中创建和格式化项目符号列表和编号列表。列表项是一个段落,其项目符号设置通过段落格式进行控制。
使用 IParagraph::get_ParagraphFormat 方法访问段落级列表设置。主要入口是 IParagraphFormat::get_Bullet,它返回一个 IBulletFormat 对象。使用该对象,您可以设置项目符号类型、符号、图片、颜色、大小、编号样式以及起始编号。
本文介绍了如何:
- 创建带自定义符号的项目符号列表
- 创建图片项目符号
- 通过设置段落深度创建多级列表
- 创建编号列表
- 检查并更改现有演示文稿中的列表格式
- 读取实际的列表标记和编号以及段落文本
创建项目符号列表
要创建项目符号列表,向 ITextFrame 添加 Paragraph 对象,并将 IBulletFormat::set_Type 设置为 BulletType::Symbol。然后可以设置 IBulletFormat::set_Char、IBulletFormat::get_Color 和 IBulletFormat::set_Height 来控制项目符号的外观。
下面的 C++ 代码演示了如何在幻灯片中创建项目符号列表:
#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IColorFormat.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/NullableBool.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
auto createParagraph = [](System::String text)
{
auto paragraph = System::MakeObject<Paragraph>();
auto paragraphFormat = paragraph->get_ParagraphFormat();
auto bulletFormat = paragraphFormat->get_Bullet();
bulletFormat->set_Type(BulletType::Symbol);
bulletFormat->set_Char(u'*');
paragraphFormat->set_Indent(15);
bulletFormat->set_IsBulletHardColor(NullableBool::True);
bulletFormat->get_Color()->set_Color(System::Drawing::Color::get_IndianRed());
bulletFormat->set_Height(100);
paragraph->set_Text(text);
return paragraph;
};
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 200, 50);
auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto paragraph1 = createParagraph(u"The first paragraph");
textFrame->get_Paragraphs()->Add(paragraph1);
auto paragraph2 = createParagraph(u"The second paragraph");
textFrame->get_Paragraphs()->Add(paragraph2);
presentation->Save(u"symbol_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();
结果:

创建编号列表
当项目顺序重要时使用编号列表。将 IBulletFormat::set_Type 设置为 BulletType::Numbered。您还可以使用 IBulletFormat::set_NumberedBulletStyle 选择编号格式,或在列表应从除 1 之外的值开始时设置 IBulletFormat::set_NumberedBulletStartWith。
下面的 C++ 代码展示了如何在幻灯片中创建编号列表:
#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 90, 80);
auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto paragraph1 = System::MakeObject<Paragraph>();
paragraph1->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
paragraph1->set_Text(u"Apple");
textFrame->get_Paragraphs()->Add(paragraph1);
auto paragraph2 = System::MakeObject<Paragraph>();
paragraph2->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
paragraph2->set_Text(u"Orange");
textFrame->get_Paragraphs()->Add(paragraph2);
auto paragraph3 = System::MakeObject<Paragraph>();
paragraph3->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
paragraph3->set_Text(u"Banana");
textFrame->get_Paragraphs()->Add(paragraph3);
presentation->Save(u"numbered_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();
结果:

创建图片项目符号
Aspose.Slides 允许您将常规项目符号替换为图像。图片项目符号最适合使用在小尺寸下仍保持可读性的简洁图像,例如图标或小型透明 PNG 文件。
Note
理想情况下,如果您计划用图像替换常规项目符号,最好选择具有透明背景的简洁图形。这类图像非常适合作为自定义项目符号。
请记住,图像会被缩放到非常小的尺寸。因此,我们强烈建议选择在列表中用作项目符号时仍保持清晰且视觉有效的图像。
要创建图片项目符号,向 IPresentation::get_Images 添加图像,并将返回的 IPPImage 对象分配给 IBulletFormat::get_Picture。在分配图像之前,将 IBulletFormat::set_Type 设置为 BulletType::Picture。
假设我们有一个 “image.png”:

下面的 C++ 代码展示了如何在幻灯片中创建图片项目符号:
#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/ITextFrame.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <IImage.h>
#include <Util/Images.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto createParagraph = [](System::String text, System::SharedPtr<IPPImage> image)
{
auto paragraph = System::MakeObject<Paragraph>();
auto paragraphFormat = paragraph->get_ParagraphFormat();
auto bulletFormat = paragraphFormat->get_Bullet();
bulletFormat->set_Type(BulletType::Picture);
bulletFormat->get_Picture()->set_Image(image);
paragraphFormat->set_Indent(15);
bulletFormat->set_Height(100);
paragraph->set_Text(text);
return paragraph;
};
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 200, 50);
auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto sourceImage = Images::FromFile(u"image.png");
auto bulletImage = presentation->get_Images()->AddImage(sourceImage);
sourceImage->Dispose();
auto paragraph1 = createParagraph(u"The first paragraph", bulletImage);
textFrame->get_Paragraphs()->Add(paragraph1);
auto paragraph2 = createParagraph(u"The second paragraph", bulletImage);
textFrame->get_Paragraphs()->Add(paragraph2);
presentation->Save(u"picture_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();
结果:

创建多级列表
使用 IParagraphFormat::set_Depth 将列表项放置在不同层级。层级 0 为顶层,层级 1 为其下的嵌套层,以此类推。
下面的 C++ 代码展示了如何创建多级项目符号列表:
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 260, 110);
auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto paragraph1 = System::MakeObject<Paragraph>();
paragraph1->get_ParagraphFormat()->set_Depth(0);
paragraph1->set_Text(u"My text - Depth 0");
textFrame->get_Paragraphs()->Add(paragraph1);
auto paragraph2 = System::MakeObject<Paragraph>();
paragraph2->get_ParagraphFormat()->set_Depth(1);
paragraph2->set_Text(u"My text - Depth 1");
textFrame->get_Paragraphs()->Add(paragraph2);
auto paragraph3 = System::MakeObject<Paragraph>();
paragraph3->get_ParagraphFormat()->set_Depth(2);
paragraph3->set_Text(u"My text - Depth 2");
textFrame->get_Paragraphs()->Add(paragraph3);
auto paragraph4 = System::MakeObject<Paragraph>();
paragraph4->get_ParagraphFormat()->set_Depth(3);
paragraph4->set_Text(u"My text - Depth 3");
textFrame->get_Paragraphs()->Add(paragraph4);
presentation->Save(u"multilevel_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();
结果:

更改现有列表
要更改现有演示文稿中的列表格式,访问目标段落并更新其 IParagraphFormat::get_Bullet 设置。创建列表时使用的相同属性也可用于检查或修改从 PPT、PPTX 或 ODP 文件加载的列表。
下面的 C++ 代码将文本框中的第一段落更改为使用编号列表样式:
#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IParagraph.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/NumberedBulletStyle.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
auto presentation = System::MakeObject<Presentation>(u"input.pptx");
auto slide = presentation->get_Slide(0);
auto autoShape = System::ExplicitCast<IAutoShape>(slide->get_Shape(0));
auto paragraph = autoShape->get_TextFrame()->get_Paragraph(0);
auto paragraphFormat = paragraph->get_ParagraphFormat();
auto bulletFormat = paragraphFormat->get_Bullet();
bulletFormat->set_Type(BulletType::Numbered);
bulletFormat->set_NumberedBulletStyle(NumberedBulletStyle::BulletRomanUCPeriod);
bulletFormat->set_NumberedBulletStartWith(1);
paragraphFormat->set_MarginLeft(30);
paragraphFormat->set_Indent(-20);
presentation->Save(u"updated_list.pptx", SaveFormat::Pptx);
presentation->Dispose();
读取实际的项目符号值
在提取演示文稿内容时,保持列表标记与段落文本一起。调用 IParagraphFormat::GetEffective 并访问 IParagraphFormatEffectiveData::get_Bullet 以获取有效的项目符号格式。在读取 get_ActualBulletValue 之前,先检查其 get_Type。
对于编号列表,需要区分以下三个值:
- get_NumberedBulletStyle 确定格式,例如带句点的阿拉伯数字或带句点的小写字母。
- get_NumberedBulletStartWith 指定一组编号段落的起始数字。
- get_ActualBulletValue 返回特定段落的格式化标记。例如,列表从 5 开始的第二项返回
6.。
使用 IParagraphFormat::get_Depth 保留每个段落的嵌套层级。实际标记并未编码完整的层级结构:嵌套项可能返回 c. 而不是类似 6.c. 的组合标签。
下面的独立示例创建列表,将其保存到演示文稿,然后重新加载演示文稿以报告每个段落的文本、深度、项目符号类型和实际值。小的蓝色图片标记是由内联 SVG 创建的。
#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IBulletFormatEffectiveData.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/ILineFormat.h>
#include <DOM/IParagraph.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IParagraphFormatEffectiveData.h>
#include <DOM/IPortionFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/ITextFrame.h>
#include <DOM/FillType.h>
#include <DOM/NullableBool.h>
#include <DOM/NumberedBulletStyle.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <DOM/SvgImage.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/console.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;
{
auto presentation = System::MakeObject<Presentation>();
presentation->get_SlideSize()->SetSize(720, 440, SlideSizeScaleType::DoNotScale);
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 30, 20, 660, 400);
shape->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
auto textFrame = shape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
// 创建一个没有外部图像文件的图片标记。
auto svg = System::MakeObject<SvgImage>(u"<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16'><rect width='16' height='16' rx='4' fill='#0078D4'/></svg>");
auto bulletImage = presentation->get_Images()->AddImage(svg);
auto addParagraph = [&](System::String text, int16_t depth, BulletType type, int16_t start = 1, NumberedBulletStyle style = NumberedBulletStyle::BulletArabicPeriod)
{
auto paragraph = System::MakeObject<Paragraph>();
paragraph->set_Text(text);
auto format = paragraph->get_ParagraphFormat();
format->set_Depth(depth);
format->set_MarginLeft(30 + depth * 40);
format->set_Indent(-28);
format->set_SpaceAfter(8);
format->get_DefaultPortionFormat()->set_FontHeight(22);
format->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
format->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
auto bullet = format->get_Bullet();
bullet->set_Type(type);
bullet->set_IsBulletHardColor(NullableBool::True);
bullet->get_Color()->set_Color(Color::get_Black());
if (type == BulletType::Numbered)
{
bullet->set_NumberedBulletStyle(style);
bullet->set_NumberedBulletStartWith(start);
}
else if (type == BulletType::Symbol)
{
bullet->set_Char(u'\u2022');
}
else if (type == BulletType::Picture)
{
bullet->get_Picture()->set_Image(bulletImage);
}
textFrame->get_Paragraphs()->Add(paragraph);
};
addParagraph(u"First item", 0, BulletType::Numbered, 5);
addParagraph(u"Second item", 0, BulletType::Numbered, 5);
addParagraph(u"Nested item", 1, BulletType::Numbered, 3, NumberedBulletStyle::BulletAlphaLCPeriod);
addParagraph(u"Next nested item", 1, BulletType::Numbered, 3, NumberedBulletStyle::BulletAlphaLCPeriod);
addParagraph(u"Deep item", 2, BulletType::Numbered, 4, NumberedBulletStyle::BulletRomanLCPeriod);
addParagraph(u"Back to top level", 0, BulletType::Numbered, 5);
addParagraph(u"Symbol marker", 0, BulletType::Symbol);
addParagraph(u"No bullet", 0, BulletType::None);
addParagraph(u"Picture marker", 0, BulletType::Picture);
presentation->Save(u"actual_bullet_values.pptx", SaveFormat::Pptx);
presentation->Dispose();
}
auto loadedPresentation = System::MakeObject<Presentation>(u"actual_bullet_values.pptx");
auto loadedShape = System::ExplicitCast<IAutoShape>(loadedPresentation->get_Slide(0)->get_Shape(0));
System::Console::WriteLine(u"Text | Depth | Type | Actual value");
for (auto paragraph : loadedShape->get_TextFrame()->get_Paragraphs())
{
auto bullet = paragraph->get_ParagraphFormat()->GetEffective()->get_Bullet();
auto actualValue = bullet->get_ActualBulletValue();
System::String marker;
switch (bullet->get_Type())
{
case BulletType::Numbered:
case BulletType::Symbol:
marker = actualValue;
break;
case BulletType::None:
marker = (actualValue.IsNull() ? System::String(u"null") : actualValue) + u" (no bullet)";
break;
case BulletType::Picture:
marker = (actualValue.IsNull() ? System::String(u"null") : actualValue) + u" (image required)";
break;
default:
marker = u"unsupported bullet type";
break;
}
System::Console::WriteLine(u"{0} | {1} | {2} | {3}", paragraph->get_Text(), paragraph->get_ParagraphFormat()->get_Depth(), bullet->get_Type(), marker);
}
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,get_ActualBulletValue 返回一个空的 System::String,而不是空字符串。使用 IsNull 检查该值。需要分别处理这些类型:前者没有标记,后者需要单独提取项目符号图像。仅通过提取文本标记无法再现图片项目符号。
常见问题
项目符号和编号列表可以导出为 PDF 或图像吗?
可以。Aspose.Slides 在目标格式支持相应的文字布局和项目符号特性的情况下会保留列表格式。
我可以编辑现有演示文稿中的列表吗?
可以。加载演示文稿,访问目标段落,检查或更新其 IParagraphFormat::get_Bullet 设置,然后保存演示文稿。
列表可以包含非拉丁文字吗?
可以。列表项文本可以包含 Unicode 字符,因此您可以在多语言演示文稿中创建列表。确保演示文稿中使用的字体支持所需的字符。