Apache-poi Java: 如何更改 WORD 文档中列表编号的字体名称和大小?

huangapple go评论75阅读模式
英文:

Apache-poi Java: How can I change the font name and size of a list numbering in a WORD document?

问题

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;
}
英文:

I wrote a method to add numbering to some paragraphs of a word document, however I do not seem to be able to customize the font name and font size of the list numbering (example: a. my item 1, b. my item 2). I am able to customize the font in the paragraphs, however I am not able to customize the font name and font size of the list numbering. Is there anything I might be missing in the code below?

    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;
	 }

答案1

得分: 1

你的代码仅为文档创建了一个十进制编号格式,并获取了适当的 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 for second 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();
    }
}
英文:

Your code only creates a decimal numbering format for the document and gets the appropriate NumID. That NumID then needs to be applied to paragraphs in the document which shall be numbered. The format is determined by the paragraphs.

The format of the numbering itself is determined by the paragraph's run properties. Unfortunately there is no method in XWPFParagraph to set paragraph run properties until now. So we need using low level ooxml-schemas classes to do so. In following code the method setParagraphRunProperties(XWPFParagraph paragraph, String fontFamily, int fontSize) is doing this.

The format of the numbered paragraphs is determined by the format of it's text runs.

Complete example:

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();
}
}

huangapple
  • 本文由 发表于 2020年9月26日 13:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64074191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定