Zarządzanie listami wypunktowanymi i numerowanymi w prezentacjach przy użyciu JavaScript
Przegląd
Aspose.Slides for Node.js via Java umożliwia tworzenie i formatowanie list wypunktowanych i numerowanych w prezentacjach PowerPoint oraz OpenDocument. Element listy to akapit, którego ustawienia wypunktowania są kontrolowane przez format akapitu.
Użyj klasy Paragraph aby uzyskać dostęp do ustawień list na poziomie akapitu. Głównym punktem wejścia jest Paragraph.getParagraphFormat().getBullet(), który zwraca obiekt BulletFormat. Dzięki temu obiektowi możesz ustawić typ wypunktowania, symbol, obraz, kolor, rozmiar, styl numeracji oraz liczbę początkową.
Ten artykuł pokazuje, jak:
- utworzyć listę wypunktowaną z niestandardowym symbolem
- utworzyć wypunktowanie obrazkowe
- utworzyć listę wielopoziomową, ustawiając głębokość akapitu
- utworzyć listę numerowaną
- sprawdzić i zmienić formatowanie listy w istniejącej prezentacji
- odczytać rzeczywiste znaczniki list i numery obok tekstu akapitu
Utworzenie listy wypunktowanej
Aby utworzyć listę wypunktowaną, dodaj obiekty Paragraph do TextFrame i ustaw BulletFormat.setType na BulletType.Symbol. Następnie możesz ustawić BulletFormat.setChar, BulletFormat.getColor i BulletFormat.setHeight, aby kontrolować wygląd wypunktowania.
Poniższy kod JavaScript demonstruje, jak utworzyć listę wypunktowaną na slajdzie:
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();
}
Wynik:

Utworzenie listy numerowanej
Używaj list numerowanych, gdy kolejność elementów ma znaczenie. Ustaw BulletFormat.setType na BulletType.Numbered. Możesz także wybrać format numeracji za pomocą BulletFormat.setNumberedBulletStyle lub ustawić BulletFormat.setNumberedBulletStartWith, gdy lista ma zaczynać się od wartości innej niż 1.
Poniższy kod JavaScript pokazuje, jak utworzyć listę numerowaną na slajdzie:
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();
}
Wynik:

Utworzenie wypunktowania obrazkowego
Aspose.Slides pozwala zastąpić zwykły symbol wypunktowania obrazem. Wypunktowania obrazkowe działają najlepiej z prostymi obrazami, które pozostają czytelne w małym rozmiarze, takimi jak ikony lub małe pliki PNG z przezroczystością.
Note
Idealnie, jeśli planujesz zastąpić zwykły symbol wypunktowania obrazem, najlepiej wybrać prostą grafikę z przezroczystym tłem. Takie obrazy dobrze sprawdzają się jako niestandardowe symbole wypunktowania.
Pamiętaj, że obraz zostanie przeskalowany do bardzo małego rozmiaru. Z tego powodu zdecydowanie zalecamy wybranie obrazu, który pozostaje wyraźny i wizualnie skuteczny jako wypunktowanie w liście.
Aby utworzyć wypunktowanie obrazkowe, dodaj obraz do Presentation przy użyciu Presentation.getImages().addImage i przypisz zwrócony obiekt PPImage do BulletFormat.getPicture().setImage. Przed przypisaniem obrazu ustaw BulletFormat.setType na BulletType.Picture.
Załóżmy, że mamy plik “image.png”:

Poniższy kod JavaScript pokazuje, jak utworzyć wypunktowania obrazkowe na slajdzie:
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();
}
Wynik:

Utworzenie listy wielopoziomowej
Użyj ParagraphFormat.setDepth, aby umieścić elementy listy na różnych poziomach. Poziom 0 to najwyższy poziom, poziom 1 jest zagnieżdżony poniżej niego i tak dalej.
Poniższy kod JavaScript pokazuje, jak utworzyć wielopoziomową listę wypunktowaną:
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();
}
Wynik:

Modyfikacja istniejącej listy
Aby zmienić formatowanie listy w istniejącej prezentacji, uzyskaj dostęp do docelowego akapitu i zaktualizuj jego ustawienia ParagraphFormat.getBullet. Te same właściwości użyte do tworzenia list mogą być użyte do przeglądania lub modyfikowania list wczytanych z pliku PPT, PPTX lub ODP.
Poniższy kod JavaScript zmienia pierwszy akapit w ramce tekstowej, aby używał stylu listy numerowanej:
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();
}
Odczyt rzeczywistych wartości wypunktowania
Podczas wyodrębniania treści prezentacji, zachowaj znaczniki list obok tekstu akapitu. Wywołaj ParagraphFormat.getEffective i uzyskaj dostęp do IParagraphFormatEffectiveData.getBullet aby otrzymać efektywny format wypunktowania. Sprawdź jego getType przed odczytaniem getActualBulletValue.
Dla list numerowanych rozróżnij trzy następujące wartości:
- getNumberedBulletStyle określa format, np. liczby arabskie z kropką lub małe litery z kropką.
- getNumberedBulletStartWith określa liczbę początkową dla grupy numerowanych akapitów.
- getActualBulletValue zwraca sformatowany znacznik dla konkretnego akapitu. Na przykład drugi element listy zaczynającej się od 5 zwróci
6..
Zachowaj getDepth przy każdym akapicie, aby utrzymać poziom zagnieżdżenia. Rzeczywisty znacznik nie koduje pełnej hierarchii: zagnieżdżony element może zwrócić c. zamiast połączonej etykiety takiej jak 6.c..
Poniższy przykład samodzielny tworzy listy, zapisuje je do prezentacji i ponownie wczytuje prezentację, aby zgłosić tekst każdego akapitu, głębokość, typ wypunktowania i rzeczywistą wartość. Mały niebieski znacznik obrazkowy jest tworzony z osadzonego 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();
// Utwórz znacznik obrazkowy bez zewnętrznego pliku obrazu.
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();
}
Źródłowe listy przedstawiono poniżej.

Raport odpowiada znacznikom wyświetlanym w 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)
Dla BulletType.None i BulletType.Picture, getActualBulletValue zwraca null, a nie pusty ciąg. Obsługuj te typy osobno: pierwszy nie ma znacznika, drugi wymaga osobnego wyodrębnienia obrazu wypunktowania. Wypunktowanie obrazkowe nie może być odtworzone wyłącznie przez wyodrębnienie znacznika tekstowego.
FAQ
Czy listy wypunktowane i numerowane mogą być eksportowane do PDF lub obrazów?
Tak. Aspose.Slides zachowuje formatowanie list, gdy docelowy format obsługuje odpowiednie rozmieszczenie tekstu i funkcje wypunktowania.
Czy mogę edytować listy w istniejących prezentacjach?
Tak. Wczytaj prezentację, uzyskaj dostęp do docelowego akapitu, sprawdź lub zaktualizuj jego ustawienia ParagraphFormat.getBullet i zapisz prezentację.
Czy listy mogą zawierać tekst niełaciński?
Tak. Tekst elementów listy może zawierać znaki Unicode, dzięki czemu możesz tworzyć listy w wielojęzycznych prezentacjach. Upewnij się, że czcionki użyte w prezentacji obsługują potrzebne znaki.