Reputation: 65
I check apache poi documentation but didn't find anything related to number list which can help to add number list to pptx, only thing i found was bullet points.
XSLFTextParagraph paragraph = textShape.addNewTextParagraph();
paragraph.setBullet(true);
is there any method which we can use as a replacement of setBullet(true) method for number list? Any help would be appreciated.
Upvotes: 2
Views: 395
Reputation: 61852
Numbered lists are not yet implemented in XSLF
. But the underlying org.openxmlformats.schemas.drawingml.x2006.main.*
classes are there. So one could use this classes.
But what and how?
All Office Open XML files are ZIP archives. So one can simply unzip a *.pptx
file and have a look into the XML of /ppt/slides/slide1.xml
. There one will find something like:
<a:p>
<a:pPr marL="406400" indent="-406400">
<a:buAutoNum type="arabicPeriod"/>
</a:pPr>
<a:r>
<a:rPr sz="2200"/>
<a:t>First list item</a:t>
</a:r>
</a:p>
for a paragraph in a numbered list.
So the paragraph p
contains a paragraph properties pPr
element, having margin left set and a negative indent for the first text line (hanging indent). In pPr
is a bullet auto number (sic) buAutoNum
element having the type Arabic numbers followed by period arabicPeriod
.
Unfortunately there is no API documentation for the org.openxmlformats.schemas.drawingml.x2006.main.*
classes public available. So one would need getting the sources of ooxml-schemas
or poi-ooxml-full
to create the API documentation using javadoc
.
Complete example:
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;
public class CreatePPTXNunberings {
static void createBulletpointList(XSLFTextShape textShape, String[] items) {
XSLFTextParagraph paragraph = null;
XSLFTextRun run = null;
double fontSize = 22d;
double indent = 22d;
for (String item : items) {
paragraph = textShape.addNewTextParagraph();
paragraph.setBullet(true); //XSLFTextParagraph.setBullet sets pPr too
paragraph.getXmlObject().getPPr().setMarL(org.apache.poi.util.Units.toEMU(indent));
paragraph.setIndent(-indent);
run = paragraph.addNewTextRun();
run.setFontSize(fontSize);
run.setText(item);
}
}
static void createNumberedList(XSLFTextShape textShape, String[] items) {
XSLFTextParagraph paragraph = null;
XSLFTextRun run = null;
double fontSize = 22d;
double indent = 32d;
for (String item : items) {
paragraph = textShape.addNewTextParagraph();
if (paragraph.getXmlObject().getPPr() == null) paragraph.getXmlObject().addNewPPr();
if (paragraph.getXmlObject().getPPr().getBuAutoNum() == null) paragraph.getXmlObject().getPPr().addNewBuAutoNum();
paragraph.getXmlObject().getPPr().getBuAutoNum().setType(org.openxmlformats.schemas.drawingml.x2006.main.STTextAutonumberScheme.ARABIC_PERIOD);
paragraph.getXmlObject().getPPr().setMarL(org.apache.poi.util.Units.toEMU(indent));
paragraph.setIndent(-indent);
run = paragraph.addNewTextRun();
run.setFontSize(fontSize);
run.setText(item);
}
}
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow();
XSLFSlide slide = slideShow.createSlide();
XSLFTextShape textShape = slide.createAutoShape();
java.awt.Rectangle rect = new java.awt.Rectangle(100, 100, 500, 300);
textShape.setAnchor(rect.getBounds2D());
textShape.setShapeType(ShapeType.RECT);
XSLFTextParagraph paragraph = null;
XSLFTextRun run = null;
double fontSize = 22d;
paragraph = textShape.addNewTextParagraph();
run = paragraph.addNewTextRun();
run.setFontSize(fontSize);
run.setText("Following is a bullet point list:");
createBulletpointList(textShape,
new String[]{"First list item", "Second list item, a little bit longer to show automatic line breaks", "Third list item"}
);
paragraph = textShape.addNewTextParagraph();
paragraph = textShape.addNewTextParagraph();
run = paragraph.addNewTextRun();
run.setFontSize(fontSize);
run.setText("Following is a numbered list:");
createNumberedList(textShape,
new String[]{"First list item", "Second list item, a little bit longer to show automatic line breaks", "Third list item"}
);
FileOutputStream out = new FileOutputStream("./CreatePPTXNunberings.pptx");
slideShow.write(out);
out.close();
slideShow.close();
}
}
This code needs the full jar of all of the schemas, which is poi-ooxml-full-5.2.2.jar
for apache poi 5.2.2
, as mentioned in FAQ. Note, since apache poi 5.*
the formerly used ooxml-schemas-*.jar
cannot be used anymore. There must not be any ooxml-schemas-*.jar
in class path when using apache poi 5.*
.
Upvotes: 5