demir5334
demir5334

Reputation: 225

How to add custom bullet to word document with apache poi

I want to add custom bullet to my word document using apache poi.

I could add bullet with this code part

    CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
    cTAbstractNum.setAbstractNumId(BigInteger.valueOf(0));
    CTLvl cTLvl = cTAbstractNum.addNewLvl();
    cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET);
    cTLvl.addNewLvlText().setVal("•");
    XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
    XWPFNumbering numbering = document.createNumbering();
    BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
    BigInteger numID = numbering.addNum(abstractNumID);

But i want to change the bullet format to in like this photo

bullet

Upvotes: 0

Views: 673

Answers (1)

Axel Richter
Axel Richter

Reputation: 61870

The

cTLvl.addNewLvlText().setVal("•");

sets the bullet symbol. In this case the Unicode bullet u\2022. So if another symbol is needed, then set it there.

Word itself uses the special font Wingdings for special symbols here. There ANSI code is mapped to special glyph. For example ANSI code 216 (D8) is mapped to the glyph showing that arrow symbol. But that would need font settings for the CTLvl.

The simplest solution is using equivalent Unicode characters. Wingdings 216 is equivalent to Unicode \u2B9A. So

cTLvl.addNewLvlText().setVal("\u2B9A");

should work.

If you really want using Wingdings as Word does, then following is needed:

cTLvl.addNewLvlText().setVal("\uF0D8");
cTLvl.addNewRPr().addNewRFonts().setHAnsi("Wingdings");
cTLvl.getRPr().getRFontsArray(0).setAscii("Wingdings");
cTLvl.getRPr().getRFontsArray(0).setHint(STHint.DEFAULT);

This sets font Wingdings for the cTLvl.

Note the value is set \uF0D8 and not \u00D8. This is used because \u00D8 would show Ø if Wingdings cannot be used. This could lead to irritations.

Upvotes: 1

Related Questions