本文介绍了如何使用 Apache POI 在 Word 中重新启动编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
下面的方法添加编号成功.但是,当我尝试将编号添加到应该从零开始的其他部分时,第一个列表中添加的编号会在我的第二个列表中继续.如何重新启动它?
The method below adds numbering successfully. However, when I try to add numbering to other sections which are supposed to start from zero, the numbering added in the first list continues on my second list. How do I restart it?
public void numbering(XWPFDocument document) {
List<XWPFParagraph> list = document.getParagraphs();
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
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();
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);
}
}
}
在 Apache POI编号列表 我已经描述了 Word 中基本编号的工作原理.
In Apache POI numbered list I have described how basic numbering works in Word.
为段落设置的 NumID
确定用于该段落的编号.因此,如果需要以 1. 开头的新编号,则需要将新的 XWPFAbstractNum
添加到 XWPFNumbering
并且需要添加新的 NumID
被创建.
The NumID
set to the paragraph determines the numbering used for that paragraph. So if a new numbering starting with 1. is needed, then a new XWPFAbstractNum
needs to be added to the XWPFNumbering
and a new NumID
needs to be created.
最简单的方法是使用方法BigInteger getNewDecimalNumberingId(XWPFDocument document, BigInteger abstractNumID)
,如下所示:
Doing this at simplest could be achieved using a method BigInteger getNewDecimalNumberingId(XWPFDocument document, BigInteger abstractNumID)
as follows:
BigInteger getNewDecimalNumberingId(XWPFDocument document, BigInteger abstractNumID) {
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
cTAbstractNum.setAbstractNumId(abstractNumID);
CTLvl cTLvl = cTAbstractNum.addNewLvl();
cTLvl.setIlvl(BigInteger.valueOf(0)); // set indent level 0
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;
}
这通过向 XWPFNumbering
添加新的 XWPFAbstractNum
来获得新的 NumID
.
This gets a new NumID
by adding a new XWPFAbstractNum
to the 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.setIlvl(BigInteger.valueOf(0)); // set indent level 0
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();
}
}
这篇关于如何使用 Apache POI 在 Word 中重新启动编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
原网址: 访问
创建于: 2023-06-06 14:18:29
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论