使用 JavaScript 管理演示文稿中的项目符号和编号列表
概述
Aspose.Slides for Node.js via Java 可让您在 PowerPoint 和 OpenDocument 演示文稿中创建和格式化项目符号列表和编号列表。列表项是段落,其项目符号设置通过段落格式进行控制。
使用 Paragraph 类访问段落级别的列表设置。主要入口是 Paragraph.getParagraphFormat().getBullet(),它返回一个 BulletFormat 对象。通过该对象,您可以设置项目符号类型、符号、图片、颜色、大小、编号样式和起始编号。
本文展示了如何:
- 创建带自定义符号的项目符号列表
- 创建图片项目符号
- 通过设置段落深度创建多级列表
- 创建编号列表
- 检查并更改现有演示文稿中的列表格式
- 在段落文本旁读取实际的列表标记和编号
创建项目符号列表
要创建项目符号列表,向 TextFrame 添加 Paragraph 对象,并将 BulletFormat.setType 设置为 BulletType.Symbol。随后可以设置 BulletFormat.setChar、BulletFormat.getColor 和 BulletFormat.setHeight 来控制项目符号的外观。
下面的 JavaScript 代码演示了如何在幻灯片中创建项目符号列表:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
function createParagraph(text, bulletColor) {
const paragraph = new aspose.slides.Paragraph();
const paragraphFormat = paragraph.getParagraphFormat();
const bulletFormat = paragraphFormat.getBullet();
bulletFormat.setType(java.newByte(aspose.slides.BulletType.Symbol));
bulletFormat.setChar(java.newChar("*"));
paragraphFormat.setIndent(15);
bulletFormat.setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
bulletFormat.getColor().setColor(bulletColor);
bulletFormat.setHeight(100);
paragraph.setText(text);
return paragraph;
}
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 200, 50);
const textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
const bulletColor = java.newInstanceSync("java.awt.Color", 205, 92, 92);
const paragraph1 = createParagraph("The first paragraph", bulletColor);
textFrame.getParagraphs().add(paragraph1);
const paragraph2 = createParagraph("The second paragraph", bulletColor);
textFrame.getParagraphs().add(paragraph2);
presentation.save("symbol_bullets.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

创建编号列表
当项目顺序重要时使用编号列表。将 BulletFormat.setType 设置为 BulletType.Numbered。您还可以使用 BulletFormat.setNumberedBulletStyle 选择编号格式,或在列表应从除 1 之外的值开始时使用 BulletFormat.setNumberedBulletStartWith。
下面的 JavaScript 代码展示了如何在幻灯片中创建编号列表:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 90, 80);
const textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
const paragraph1 = new aspose.slides.Paragraph();
paragraph1.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
paragraph1.setText("Apple");
textFrame.getParagraphs().add(paragraph1);
const paragraph2 = new aspose.slides.Paragraph();
paragraph2.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
paragraph2.setText("Orange");
textFrame.getParagraphs().add(paragraph2);
const paragraph3 = new aspose.slides.Paragraph();
paragraph3.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
paragraph3.setText("Banana");
textFrame.getParagraphs().add(paragraph3);
presentation.save("numbered_bullets.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

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

下面的 JavaScript 代码展示了如何在幻灯片中创建图片项目符号:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
function createParagraph(text, image) {
const paragraph = new aspose.slides.Paragraph();
const paragraphFormat = paragraph.getParagraphFormat();
const bulletFormat = paragraphFormat.getBullet();
bulletFormat.setType(java.newByte(aspose.slides.BulletType.Picture));
bulletFormat.getPicture().setImage(image);
paragraphFormat.setIndent(15);
bulletFormat.setHeight(100);
paragraph.setText(text);
return paragraph;
}
const presentation = new aspose.slides.Presentation();
let image = null;
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 200, 50);
const textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
image = aspose.slides.Images.fromFile("image.png");
const bulletImage = presentation.getImages().addImage(image);
const paragraph1 = createParagraph("The first paragraph", bulletImage);
textFrame.getParagraphs().add(paragraph1);
const paragraph2 = createParagraph("The second paragraph", bulletImage);
textFrame.getParagraphs().add(paragraph2);
presentation.save("picture_bullets.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (image !== null) {
image.dispose();
}
presentation.dispose();
}
结果:

创建多级列表
使用 ParagraphFormat.setDepth 可将列表项放在不同层级。层级 0 为顶层,层级 1 为其下的嵌套,以此类推。
下面的 JavaScript 代码展示了如何创建多级项目符号列表:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 260, 110);
const textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
const paragraph1 = new aspose.slides.Paragraph();
paragraph1.getParagraphFormat().setDepth(java.newShort(0));
paragraph1.setText("My text - Depth 0");
textFrame.getParagraphs().add(paragraph1);
const paragraph2 = new aspose.slides.Paragraph();
paragraph2.getParagraphFormat().setDepth(java.newShort(1));
paragraph2.setText("My text - Depth 1");
textFrame.getParagraphs().add(paragraph2);
const paragraph3 = new aspose.slides.Paragraph();
paragraph3.getParagraphFormat().setDepth(java.newShort(2));
paragraph3.setText("My text - Depth 2");
textFrame.getParagraphs().add(paragraph3);
const paragraph4 = new aspose.slides.Paragraph();
paragraph4.getParagraphFormat().setDepth(java.newShort(3));
paragraph4.setText("My text - Depth 3");
textFrame.getParagraphs().add(paragraph4);
presentation.save("multilevel_bullets.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

更改现有列表
要更改现有演示文稿中的列表格式,访问目标段落并更新其 ParagraphFormat.getBullet 设置。创建列表时使用的相同属性也可用于检查或修改从 PPT、PPTX 或 ODP 文件加载的列表。
下面的 JavaScript 代码将文本框中的第一段落更改为使用编号列表样式:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation("input.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const paragraphFormat = paragraph.getParagraphFormat();
const bulletFormat = paragraphFormat.getBullet();
bulletFormat.setType(java.newByte(aspose.slides.BulletType.Numbered));
bulletFormat.setNumberedBulletStyle(java.newByte(aspose.slides.NumberedBulletStyle.BulletRomanUCPeriod));
bulletFormat.setNumberedBulletStartWith(java.newShort(1));
paragraphFormat.setMarginLeft(30);
paragraphFormat.setIndent(-20);
presentation.save("updated_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
读取实际的项目符号值
在提取演示文稿内容时,将列表标记与段落文本一起保留。调用 ParagraphFormat.getEffective 并访问 IParagraphFormatEffectiveData.getBullet 以获取有效的项目符号格式。在读取 getActualBulletValue 之前,先检查其 getType。
对于编号列表,下面这三个值需要区分:
- getNumberedBulletStyle 确定格式,例如带句点的阿拉伯数字或带句点的小写字母。
- getNumberedBulletStartWith 指定一组编号段落的起始数字。
- getActualBulletValue 返回特定段落的格式化标记。例如,从 5 开始的列表中第二项返回
6.。
使用每个段落的 getDepth 可保留其嵌套层级。实际标记不编码完整层次结构:嵌套项可能返回 c. 而不是组合标签如 6.c.。
下面的独立示例创建列表、将其保存到演示文稿、重新加载演示文稿并报告每个段落的文本、深度、项目符号类型和实际值。小蓝色图片标记由内联 SVG 创建。
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const black = java.newInstanceSync("java.awt.Color", 0, 0, 0);
const presentation = new aspose.slides.Presentation();
try {
presentation.getSlideSize().setSize(720, 440, aspose.slides.SlideSizeScaleType.DoNotScale);
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 30, 20, 660, 400);
shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
// 创建一个无需外部图像文件的图片标记。
const svg = new aspose.slides.SvgImage("<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16'><rect width='16' height='16' rx='4' fill='#0078D4'/></svg>");
const bulletImage = presentation.getImages().addImage(svg);
function addParagraph(text, depth, type, start, style) {
const paragraph = new aspose.slides.Paragraph();
paragraph.setText(text);
const format = paragraph.getParagraphFormat();
format.setDepth(java.newShort(depth));
format.setMarginLeft(30 + depth * 40);
format.setIndent(-28);
format.setSpaceAfter(8);
format.getDefaultPortionFormat().setFontHeight(22);
format.getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(black);
format.getBullet().setType(java.newByte(type));
format.getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
format.getBullet().getColor().setColor(black);
if (type === aspose.slides.BulletType.Numbered) {
format.getBullet().setNumberedBulletStyle(java.newByte(style));
format.getBullet().setNumberedBulletStartWith(java.newShort(start));
} else if (type === aspose.slides.BulletType.Symbol) {
format.getBullet().setChar(java.newChar(0x2022));
} else if (type === aspose.slides.BulletType.Picture) {
format.getBullet().getPicture().setImage(bulletImage);
}
textFrame.getParagraphs().add(paragraph);
}
addParagraph("First item", 0, aspose.slides.BulletType.Numbered, 5, aspose.slides.NumberedBulletStyle.BulletArabicPeriod);
addParagraph("Second item", 0, aspose.slides.BulletType.Numbered, 5, aspose.slides.NumberedBulletStyle.BulletArabicPeriod);
addParagraph("Nested item", 1, aspose.slides.BulletType.Numbered, 3, aspose.slides.NumberedBulletStyle.BulletAlphaLCPeriod);
addParagraph("Next nested item", 1, aspose.slides.BulletType.Numbered, 3, aspose.slides.NumberedBulletStyle.BulletAlphaLCPeriod);
addParagraph("Deep item", 2, aspose.slides.BulletType.Numbered, 4, aspose.slides.NumberedBulletStyle.BulletRomanLCPeriod);
addParagraph("Back to top level", 0, aspose.slides.BulletType.Numbered, 5, aspose.slides.NumberedBulletStyle.BulletArabicPeriod);
addParagraph("Symbol marker", 0, aspose.slides.BulletType.Symbol, 1, aspose.slides.NumberedBulletStyle.BulletArabicPeriod);
addParagraph("No bullet", 0, aspose.slides.BulletType.None, 1, aspose.slides.NumberedBulletStyle.BulletArabicPeriod);
addParagraph("Picture marker", 0, aspose.slides.BulletType.Picture, 1, aspose.slides.NumberedBulletStyle.BulletArabicPeriod);
presentation.save("actual_bullet_values.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
const loadedPresentation = new aspose.slides.Presentation("actual_bullet_values.pptx");
try {
const loadedShape = loadedPresentation.getSlides().get_Item(0).getShapes().get_Item(0);
console.log("Text | Depth | Type | Actual value");
const paragraphs = loadedShape.getTextFrame().getParagraphs();
for (let i = 0; i < paragraphs.getCount(); i++) {
const paragraph = paragraphs.get_Item(i);
const bullet = paragraph.getParagraphFormat().getEffective().getBullet();
let marker;
switch (bullet.getType()) {
case aspose.slides.BulletType.Numbered:
case aspose.slides.BulletType.Symbol:
marker = bullet.getActualBulletValue();
break;
case aspose.slides.BulletType.None:
marker = bullet.getActualBulletValue() + " (no bullet)";
break;
case aspose.slides.BulletType.Picture:
marker = bullet.getActualBulletValue() + " (image required)";
break;
default:
marker = "unsupported bullet type";
break;
}
const typeName = Object.keys(aspose.slides.BulletType).find(name => aspose.slides.BulletType[name] === bullet.getType());
console.log(paragraph.getText() + " | " + paragraph.getParagraphFormat().getDepth() + " | " + typeName + " | " + marker);
}
} 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 字符,您可以在多语言演示文稿中创建列表。请确保演示文稿使用的字体支持所需字符。