In Apache Нумерованный список POI Я описал, как нумерация basi c работает в Word
.
Значение NumID
, установленное для абзаца, определяет нумерацию, используемую для этот пункт. Таким образом, если требуется новая нумерация, начинающаяся с 1., то к XWPFNumbering
необходимо добавить новую XWPFAbstractNum
и создать новую NumID
.
Сделать это проще всего можно с помощью метода BigInteger getNewDecimalNumberingId(XWPFDocument document, BigInteger abstractNumID)
следующим образом:
BigInteger getNewDecimalNumberingId(XWPFDocument document, BigInteger abstractNumID) {
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
cTAbstractNum.setAbstractNumId(abstractNumID);
CTLvl cTLvl = cTAbstractNum.addNewLvl();
cTLvl.addNewNumFmt().setVal(STNumberFormat.DECIMAL);
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;
}
Получается новый NumID
путем добавления нового XWPFAbstractNum
к XWPFNumbering
.
Полный пример использования:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLvl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STNumberFormat;
import java.math.BigInteger;
public class CreateWordNumberings {
static BigInteger getNewDecimalNumberingId(XWPFDocument document, BigInteger abstractNumID) {
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
cTAbstractNum.setAbstractNumId(abstractNumID);
CTLvl cTLvl = cTAbstractNum.addNewLvl();
cTLvl.addNewNumFmt().setVal(STNumberFormat.DECIMAL);
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;
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The paragraph before first numbering:");
//get first NumID for first numbering
BigInteger numID = getNewDecimalNumberingId(document, BigInteger.valueOf(0));
paragraph = document.createParagraph();
paragraph.setNumID(numID);
run = paragraph.createRun();
run.setText("This is first section number one");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Lorem ipsum semit dolor ...");
paragraph = document.createParagraph();
paragraph.setNumID(numID);
run = paragraph.createRun();
run.setText("This is first section number two");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Lorem ipsum semit dolor ...");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("The paragraph before second numbering:");
//get second NumID for second numbering
numID = getNewDecimalNumberingId(document, BigInteger.valueOf(1));
paragraph = document.createParagraph();
paragraph.setNumID(numID);
run = paragraph.createRun();
run.setText("This is second section number one");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Lorem ipsum semit dolor ...");
paragraph = document.createParagraph();
paragraph.setNumID(numID);
run = paragraph.createRun();
run.setText("This is second section number two");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Lorem ipsum semit dolor ...");
paragraph = document.createParagraph();
paragraph.setNumID(numID);
run = paragraph.createRun();
run.setText("This is second section number three");
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("Lorem ipsum semit dolor ...");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordNumberings.docx");
document.write(out);
out.close();
document.close();
}
}