Gestire elenchi puntati e numerati nelle presentazioni su Android
Panoramica
Aspose.Slides for Android via Java ti consente di creare e formattare elenchi puntati e numerati in presentazioni PowerPoint e OpenDocument. Un elemento di elenco è un paragrafo le cui impostazioni di punto elenco sono controllate tramite il suo formato di paragrafo.
Utilizza il metodo IParagraph.getParagraphFormat per accedere alle impostazioni dell’elenco a livello di paragrafo. Il punto di ingresso principale è IParagraphFormat.getBullet, che restituisce un oggetto IBulletFormat. Con questo oggetto, è possibile impostare il tipo di punto elenco, il simbolo, l’immagine, il colore, la dimensione, lo stile di numerazione e il numero di partenza.
Questo articolo mostra come:
- creare un elenco puntato con un simbolo personalizzato
- creare un punto elenco con immagine
- creare un elenco a più livelli impostando la profondità del paragrafo
- creare un elenco numerato
- ispezionare e modificare la formattazione dell’elenco in una presentazione esistente
- leggere i marcatori di elenco reali e i numeri accanto al testo del paragrafo
Crea un elenco puntato
Per creare un elenco puntato, aggiungi paragrafi a un ITextFrame e imposta IBulletFormat.setType su BulletType.Symbol. Puoi poi impostare IBulletFormat.setChar, IBulletFormat.getColor e IBulletFormat.setHeight per controllare l’aspetto del punto elenco.
Il seguente codice Java dimostra come creare un elenco puntato in una diapositiva:
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 200, 50);
ITextFrame textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
int bulletColor = Color.rgb(205, 92, 92);
Paragraph paragraph1 = new Paragraph();
paragraph1.getParagraphFormat().getBullet().setType(BulletType.Symbol);
paragraph1.getParagraphFormat().getBullet().setChar('*');
paragraph1.getParagraphFormat().setIndent(15);
paragraph1.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
paragraph1.getParagraphFormat().getBullet().getColor().setColor(bulletColor);
paragraph1.getParagraphFormat().getBullet().setHeight(100);
paragraph1.setText("The first paragraph");
textFrame.getParagraphs().add(paragraph1);
Paragraph paragraph2 = new Paragraph();
paragraph2.getParagraphFormat().getBullet().setType(BulletType.Symbol);
paragraph2.getParagraphFormat().getBullet().setChar('*');
paragraph2.getParagraphFormat().setIndent(15);
paragraph2.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
paragraph2.getParagraphFormat().getBullet().getColor().setColor(bulletColor);
paragraph2.getParagraphFormat().getBullet().setHeight(100);
paragraph2.setText("The second paragraph");
textFrame.getParagraphs().add(paragraph2);
presentation.save("symbol_bullets.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Il risultato:

Crea un elenco numerato
Utilizza gli elenchi numerati quando l’ordine degli elementi è importante. Imposta IBulletFormat.setType su BulletType.Numbered. È inoltre possibile scegliere un formato di numerazione con IBulletFormat.setNumberedBulletStyle oppure impostare IBulletFormat.setNumberedBulletStartWith quando l’elenco deve iniziare da un valore diverso da 1.
Il seguente codice Java mostra come creare un elenco numerato in una diapositiva:
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 90, 80);
ITextFrame textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
Paragraph paragraph1 = new Paragraph();
paragraph1.getParagraphFormat().getBullet().setType(BulletType.Numbered);
paragraph1.setText("Apple");
textFrame.getParagraphs().add(paragraph1);
Paragraph paragraph2 = new Paragraph();
paragraph2.getParagraphFormat().getBullet().setType(BulletType.Numbered);
paragraph2.setText("Orange");
textFrame.getParagraphs().add(paragraph2);
Paragraph 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();
}
Il risultato:

Crea un punto elenco con immagine
Aspose.Slides ti consente di sostituire un simbolo di punto elenco regolare con un’immagine. I punti elenco con immagine funzionano al meglio con immagini semplici che rimangono leggibili a piccole dimensioni, come icone o piccoli file PNG trasparenti.
Nota
Idealmente, se prevedi di sostituire il simbolo di punto elenco regolare con un’immagine, è consigliabile scegliere una grafica semplice con sfondo trasparente. Tali immagini funzionano bene come simboli di punto elenco personalizzati.
Tieni presente che l’immagine verrà ridotta a dimensioni molto piccole. Per questo motivo, consigliamo vivamente di scegliere un’immagine che rimanga chiara ed efficace visualmente quando utilizzata come punto elenco in un elenco.
Per creare un punto elenco con immagine, aggiungi un’immagine a Presentation.getImages e assegna l’oggetto IPPImage restituito a IBulletFormat.getPicture. Imposta IBulletFormat.setType su BulletType.Picture prima di assegnare l’immagine.
Supponiamo di avere un “image.png”:

Il seguente codice Java mostra come creare punti elenco con immagine in una diapositiva:
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 200, 50);
ITextFrame textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
IPPImage bulletImage = presentation.getImages().addImage(Images.fromFile("image.png"));
Paragraph paragraph1 = new Paragraph();
paragraph1.getParagraphFormat().getBullet().setType(BulletType.Picture);
paragraph1.getParagraphFormat().getBullet().getPicture().setImage(bulletImage);
paragraph1.getParagraphFormat().setIndent(15);
paragraph1.getParagraphFormat().getBullet().setHeight(100);
paragraph1.setText("The first paragraph");
textFrame.getParagraphs().add(paragraph1);
Paragraph paragraph2 = new Paragraph();
paragraph2.getParagraphFormat().getBullet().setType(BulletType.Picture);
paragraph2.getParagraphFormat().getBullet().getPicture().setImage(bulletImage);
paragraph2.getParagraphFormat().setIndent(15);
paragraph2.getParagraphFormat().getBullet().setHeight(100);
paragraph2.setText("The second paragraph");
textFrame.getParagraphs().add(paragraph2);
presentation.save("picture_bullets.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Il risultato:

Crea un elenco a più livelli
Utilizza IParagraphFormat.setDepth per posizionare gli elementi dell’elenco a diversi livelli. Il livello 0 è il livello superiore, il livello 1 è annidato sotto di esso, e così via.
Il seguente codice Java mostra come creare un elenco a più livelli:
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 260, 110);
ITextFrame textFrame = autoShape.getTextFrame();
textFrame.getParagraphs().clear();
Paragraph paragraph1 = new Paragraph();
paragraph1.getParagraphFormat().setDepth((short) 0);
paragraph1.setText("My text - Depth 0");
textFrame.getParagraphs().add(paragraph1);
Paragraph paragraph2 = new Paragraph();
paragraph2.getParagraphFormat().setDepth((short) 1);
paragraph2.setText("My text - Depth 1");
textFrame.getParagraphs().add(paragraph2);
Paragraph paragraph3 = new Paragraph();
paragraph3.getParagraphFormat().setDepth((short) 2);
paragraph3.setText("My text - Depth 2");
textFrame.getParagraphs().add(paragraph3);
Paragraph paragraph4 = new Paragraph();
paragraph4.getParagraphFormat().setDepth((short) 3);
paragraph4.setText("My text - Depth 3");
textFrame.getParagraphs().add(paragraph4);
presentation.save("multilevel_bullets.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Il risultato:

Modifica un elenco esistente
Per modificare la formattazione dell’elenco in una presentazione esistente, accedi al paragrafo target e aggiorna le sue impostazioni IParagraphFormat.getBullet. Gli stessi metodi usati per creare gli elenchi possono essere utilizzati per ispezionare o modificare gli elenchi caricati da un file PPT, PPTX o ODP.
import com.aspose.slides.*;
Presentation presentation = new Presentation("input.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape) slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
paragraph.getParagraphFormat().getBullet().setNumberedBulletStyle(NumberedBulletStyle.BulletRomanUCPeriod);
paragraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 1);
paragraph.getParagraphFormat().setMarginLeft(30);
paragraph.getParagraphFormat().setIndent(-20);
presentation.save("updated_list.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Leggi i valori effettivi dei punti elenco
Durante l’estrazione del contenuto della presentazione, mantieni i marcatori dell’elenco accanto al testo del paragrafo. Chiama IParagraphFormat.getEffective e accedi a IParagraphFormatEffectiveData.getBullet per ottenere il formato effettivo del punto elenco. Verifica il suo getType prima di leggere getActualBulletValue.
Per gli elenchi numerati, distingui questi tre valori:
- getNumberedBulletStyle determina il formato, ad esempio numeri arabi seguiti da un punto o lettere minuscole seguite da un punto.
- getNumberedBulletStartWith specifica il numero di partenza per un gruppo di paragrafi numerati.
- getActualBulletValue restituisce il marcatore formattato per un particolare paragrafo. Ad esempio, il secondo elemento di un elenco che inizia da 5 restituisce
6..
Conserva getDepth per ogni paragrafo per mantenere il suo livello di annidamento. Il marcatore effettivo non codifica l’intera gerarchia: un elemento annidato può restituire c. invece di un’etichetta combinata come 6.c..
Il seguente esempio autonomo crea elenchi, li salva in una presentazione e ricarica la presentazione per segnalare il testo, la profondità, il tipo di punto elenco e il valore effettivo di ogni paragrafo. Il piccolo marcatore immagine blu è creato da SVG in linea.
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(720, 440, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 20, 660, 400);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
// Crea un marcatore immagine senza un file immagine esterno.
SvgImage 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>");
IPPImage bulletImage = presentation.getImages().addImage(svg);
class ListBuilder {
void addParagraph(String text, short depth, byte type, short start, byte style) {
Paragraph paragraph = new Paragraph();
paragraph.setText(text);
IParagraphFormat 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(Color.BLACK);
format.getBullet().setType(type);
format.getBullet().setBulletHardColor(NullableBool.True);
format.getBullet().getColor().setColor(Color.BLACK);
if (type == BulletType.Numbered) {
format.getBullet().setNumberedBulletStyle(style);
format.getBullet().setNumberedBulletStartWith(start);
} else if (type == BulletType.Symbol) {
format.getBullet().setChar('\u2022');
} else if (type == BulletType.Picture) {
format.getBullet().getPicture().setImage(bulletImage);
}
textFrame.getParagraphs().add(paragraph);
}
}
ListBuilder list = new ListBuilder();
list.addParagraph("First item", (short) 0, BulletType.Numbered, (short) 5, NumberedBulletStyle.BulletArabicPeriod);
list.addParagraph("Second item", (short) 0, BulletType.Numbered, (short) 5, NumberedBulletStyle.BulletArabicPeriod);
list.addParagraph("Nested item", (short) 1, BulletType.Numbered, (short) 3, NumberedBulletStyle.BulletAlphaLCPeriod);
list.addParagraph("Next nested item", (short) 1, BulletType.Numbered, (short) 3, NumberedBulletStyle.BulletAlphaLCPeriod);
list.addParagraph("Deep item", (short) 2, BulletType.Numbered, (short) 4, NumberedBulletStyle.BulletRomanLCPeriod);
list.addParagraph("Back to top level", (short) 0, BulletType.Numbered, (short) 5, NumberedBulletStyle.BulletArabicPeriod);
list.addParagraph("Symbol marker", (short) 0, BulletType.Symbol, (short) 1, NumberedBulletStyle.BulletArabicPeriod);
list.addParagraph("No bullet", (short) 0, BulletType.None, (short) 1, NumberedBulletStyle.BulletArabicPeriod);
list.addParagraph("Picture marker", (short) 0, BulletType.Picture, (short) 1, NumberedBulletStyle.BulletArabicPeriod);
presentation.save("actual_bullet_values.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Presentation loadedPresentation = new Presentation("actual_bullet_values.pptx");
try {
IAutoShape loadedShape = (IAutoShape) loadedPresentation.getSlides().get_Item(0).getShapes().get_Item(0);
System.out.println("Text | Depth | Type | Actual value");
for (IParagraph paragraph : loadedShape.getTextFrame().getParagraphs()) {
IBulletFormatEffectiveData bullet = paragraph.getParagraphFormat().getEffective().getBullet();
String marker;
switch (bullet.getType()) {
case BulletType.Numbered:
case BulletType.Symbol:
marker = bullet.getActualBulletValue();
break;
case BulletType.None:
marker = bullet.getActualBulletValue() + " (no bullet)";
break;
case BulletType.Picture:
marker = bullet.getActualBulletValue() + " (image required)";
break;
default:
marker = "unsupported bullet type";
break;
}
String typeName = BulletType.getName(BulletType.class, bullet.getType());
System.out.println(paragraph.getText() + " | " + paragraph.getParagraphFormat().getDepth() + " | " + typeName + " | " + marker);
}
} finally {
loadedPresentation.dispose();
}
Gli elenchi di origine sono mostrati di seguito.

Il rapporto corrisponde ai marcatori visualizzati in 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)
Per BulletType.None and BulletType.Picture, getActualBulletValue restituisce null, non una stringa vuota. Gestisci questi tipi separatamente: il primo non ha marcatore, mentre il secondo richiede l’estrazione dell’immagine del punto elenco separatamente. Un punto elenco con immagine non può essere riprodotto esclusivamente estraendo un marcatore di testo.
FAQ
È possibile esportare elenchi puntati e numerati in PDF o immagini?
Sì. Aspose.Slides conserva la formattazione dell’elenco quando il formato di destinazione supporta la corrispondente disposizione del testo e le funzionalità dei punti elenco.
Posso modificare gli elenchi in presentazioni esistenti?
Sì. Carica la presentazione, accedi al paragrafo target, ispeziona o aggiorna le sue impostazioni IParagraphFormat.getBullet, e salva la presentazione.
Gli elenchi possono contenere testo non latino?
Sì. Il testo degli elementi dell’elenco può contenere caratteri Unicode, quindi è possibile creare elenchi in presentazioni multilingue. Assicurati che i caratteri utilizzati nella presentazione supportino i caratteri di cui hai bisogno.