我写了一个方法来为Word文档的某些段落添加编号,但是我似乎无法自定义列表编号的字体名称和字体大小(例如:a.我的项目1,b.我的项目2 )。我可以自定义段落中的字体,但是无法自定义列表编号的字体名称和字体大小。下面的代码中是否有我可能遗漏的内容?
public static BigInteger listNumbering(XWPFDocument doc, 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 = doc.createNumbering();
abstractNumID = numbering.addAbstractNum(abstractNum);
BigInteger numID = numbering.addNum(abstractNumID);
return numID;
}
您的代码仅为文档创建十进制编号格式并获取适当的
NumID
。然后需要将 NumID
应用到文件中应编号的段落。格式由段落决定。
编号本身的格式由段落的运行属性决定。不幸的是,到目前为止,
XWPFParagraph
中还没有方法可以设置段落运行属性。所以我们需要使用低级 ooxml-schemas
类来做到这一点。在下面的代码中,方法 setParagraphRunProperties(XWPFParagraph paragraph, String fontFamily, int fontSize)
正在执行此操作。
编号段落的格式由其文本串的格式决定。
完整示例:
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 CreateWordNumberingsFormatted {
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;
}
static void setParagraphRunProperties(XWPFParagraph paragraph, String fontFamily, int fontSize) {
if (!paragraph.getCTP().isSetPPr()) paragraph.getCTP().addNewPPr();
if (!paragraph.getCTP().getPPr().isSetRPr()) paragraph.getCTP().getPPr().addNewRPr();
if (!paragraph.getCTP().getPPr().getRPr().isSetRFonts()) paragraph.getCTP().getPPr().getRPr().addNewRFonts();
if (!paragraph.getCTP().getPPr().getRPr().isSetSz()) paragraph.getCTP().getPPr().getRPr().addNewSz();
if (!paragraph.getCTP().getPPr().getRPr().isSetSzCs()) paragraph.getCTP().getPPr().getRPr().addNewSzCs();
paragraph.getCTP().getPPr().getRPr().getRFonts().setAscii(fontFamily);
paragraph.getCTP().getPPr().getRPr().getRFonts().setHAnsi(fontFamily);
paragraph.getCTP().getPPr().getRPr().getSz().setVal(BigInteger.valueOf(fontSize*2)); //measurement unit is half pt
paragraph.getCTP().getPPr().getRPr().getSzCs().setVal(BigInteger.valueOf(fontSize*2)); //measurement unit is half pt
}
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:");
int abstractNumID = 0;
//get NumID for first decimal numbering
BigInteger numID = getNewDecimalNumberingId(document, BigInteger.valueOf(abstractNumID++));
//now apply that NumID to first list
String fontFamily = "Times New Roman";
int fontSize = 24;
//first paragraph
paragraph = document.createParagraph();
//apply NumID
paragraph.setNumID(numID);
//set paragraph run properties
setParagraphRunProperties(paragraph, fontFamily, fontSize);
//create text runs and format
run = paragraph.createRun();
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
run.setText("One");
//further numbered paragraphs
paragraph = document.createParagraph();
paragraph.setNumID(numID);
setParagraphRunProperties(paragraph, fontFamily, fontSize);
run = paragraph.createRun();
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
run.setText("Two");
paragraph = document.createParagraph();
paragraph.setNumID(numID);
setParagraphRunProperties(paragraph, fontFamily, fontSize);
run = paragraph.createRun();
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
run.setText("Three");
paragraph = document.createParagraph();
//get NumID forsecond decimal numbering
numID = getNewDecimalNumberingId(document, BigInteger.valueOf(abstractNumID++));
//now apply that NumID to second list having different font
fontFamily = "Courier New";
fontSize = 32;
paragraph = document.createParagraph();
paragraph.setNumID(numID);
setParagraphRunProperties(paragraph, fontFamily, fontSize);
run = paragraph.createRun();
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
run.setText("One");
paragraph = document.createParagraph();
paragraph.setNumID(numID);
setParagraphRunProperties(paragraph, fontFamily, fontSize);
run = paragraph.createRun();
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
run.setText("Two");
paragraph = document.createParagraph();
paragraph.setNumID(numID);
setParagraphRunProperties(paragraph, fontFamily, fontSize);
run = paragraph.createRun();
run.setFontFamily(fontFamily);
run.setFontSize(fontSize);
run.setText("Three");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordNumberingsFormatted.docx");
document.write(out);
out.close();
document.close();
}
}
我正在使用 POI 5.2.3,并且在查找 getRPr 方法 isSetRFonts、isSetSz、isSetSzCs 以及上面答案中 setParagraphRunProperties 中相应的“gets”时遇到问题。
显然,自从 Axel 上面的回复以来,事情发生了变化,但我不知道在用谷歌搜索没有成功后该去哪里寻找。
任何帮助将不胜感激。