DEV Community

CodeSharing
CodeSharing

Posted on

Java/Create Numbered and Bulleted Lists in PowerPoint

This article will demonstrate how to add a numbered list, a symbol bulleted list and a custom picture bulleted list using Free Spire.Presentation for Java.

Installation (2 Methods)
● Download the free API and unzip it. Then add the Spire.Presentation.jar file to your project as dependency.

● You can also add the jar dependency to your maven project by adding the following configurations to the pom.xml.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Code Snippet

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class AddBullets {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        Rectangle2D rect = new Rectangle2D.Double(50, 70, 300, 200);
        //Append a shape to the slide and set shape style
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
        shape.getShapeStyle().getLineColor().setColor(Color.white);
        shape.getFill().setFillType(FillFormatType.NONE);

        //Clear the default paragragh
        shape.getTextFrame().getParagraphs().clear();

        String[] str = new String[] {"Item 1", "Item 2", "Item 3" ,"Chapter 1", "Chapter 2", "Chapter 3","List 1", "List 2", "List 3" };

        //Add a numbered bulleted list
        for(int i = 0; i < 3; i ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(str[i]);
            paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.NUMBERED);
            paragraph.setBulletStyle(NumberedBulletStyle.BULLET_ROMAN_LC_PERIOD);
        }

        //Add a symbol bulleted list
        for(int j = 3; j < 6; j ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(str[j]);
            paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.SYMBOL);
        }

        //Add a custom bulleted list with picture
        for(int k = 6; k < str.length; k ++)
        {
            ParagraphEx paragraph = new ParagraphEx();
            shape.getTextFrame().getParagraphs().append(paragraph);
            paragraph.setText(str[k]);
            paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
            paragraph.setBulletType(TextBulletType.PICTURE);
            BufferedImage image = ImageIO.read(new File("timg.png"));
            paragraph.getBulletPicture().setEmbedImage(ppt.getImages().append(image));
        }

        //Save the document
        ppt.saveToFile("AddBullets.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Result
PPTlist

Discussion (0)