Apache poi Java. I am trying to add upper_letter numbering, however the result is that all sections get numbering A

joe :

the code below allows me to add upper letter numbering, it adds letter A. to the first section but for the remaining sections it keeps the same letter A. It does not create the continuous list numbering. What I get is the following:

A. This is section number one

A. This is section number two

What I need to get is:

A. This is section number one

B. This is section number two

any ideas on how to achieve this?

List list = document.getParagraphs();

CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
CTLvl cTLvl = cTAbstractNum.addNewLvl();
cTLvl.addNewNumFmt().setVal(STNumberFormat.UPPER_LETTER);
cTLvl.addNewLvlText().setVal("A.");
cTLvl.addNewStart().setVal(BigInteger.valueOf(1));

XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
XWPFNumbering numbering = document.createNumbering();
BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
BigInteger numID = numbering.addNum(abstractNumID);


for (XWPFParagraph paragraph : list) {

    if (paragraph.getText().trim().contains("This is section number one")|| 
            paragraph.getText().trim().contains("This is section number two") ){

        System.out.println(paragraph.getText() + " " + list.indexOf(paragraph));
        paragraph.setNumID(numID);


    }

}

}

Axel Richter :

In Hi, how can I restart numbering in java word apache poi? I haave provided a method BigInteger getNewDecimalNumberingId(XWPFDocument document, BigInteger abstractNumID). In this method

...
  CTLvl cTLvl = cTAbstractNum.addNewLvl();
  cTLvl.addNewNumFmt().setVal(STNumberFormat.DECIMAL);
  cTLvl.addNewLvlText().setVal("%1.");
  cTLvl.addNewStart().setVal(BigInteger.valueOf(1));
...

means the folowing: Add a first level to the abstract numbering having number format decimal and a level text of pattern %1. starting with first available number.

There the %1. is a pattern . It does not mean 1. but %1 = next available number for level 1 followed by dot .. If there were more levels, then in level 2 the lvlText could have val="%1.%2.". That would mean %1 = current number for level 1, followed by dot ., followed by %2 = next available number for level 2, followed by dot ..

The corresponding method for upper letter numbering would be:

 BigInteger getNewUpperLetterNumberingId(XWPFDocument document, BigInteger abstractNumID) {
  CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
  cTAbstractNum.setAbstractNumId(abstractNumID);

  CTLvl cTLvl = cTAbstractNum.addNewLvl();
  cTLvl.addNewNumFmt().setVal(STNumberFormat.UPPER_LETTER);
  cTLvl.addNewLvlText().setVal("%1:");
  cTLvl.addNewStart().setVal(BigInteger.valueOf(1));

  XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);

  XWPFNumbering numbering = document.createNumbering();

  abstractNumID = numbering.addAbstractNum(abstractNum);

  BigInteger numID = numbering.addNum(abstractNumID);

  return numID;
 }

You see only cTLvl.addNewNumFmt().setVal(STNumberFormat.UPPER_LETTER); has changed. The %1: is a pattern again. It now means %1 = next available letter for level 1 followed by colon :.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=341934&siteId=1