我正在做一个项目,以生成一个有项目符号的word文档。默认情况下,项目符号后面在文本之前有一个很大的间隙。在Word中,在“列表缩进跟随数字”选项下,可以将此空格设置为制表符、空格字符或“无”。默认情况下,在POI中将其设置为选项卡。如何将其更改为使用空格字符?
发布于 2019-10-09 14:41:23
我强烈建议使用制表位,因为这是Word中的默认设置,正确使用它也可以正确地显示项目符号后面的多行文本。
项目符号和文本之间的间距由段落缩进设置决定。左缩进决定了文本的位置,悬挂缩进决定了项目符号在文本之前的悬空程度。
示例:
import java.io.File;
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.util.ArrayList;
import java.util.Arrays;
import java.math.BigInteger;
public class CreateWordSimplestBulletList {
public static void main(String[] args) throws Exception {
ArrayList<String> documentList = new ArrayList<String>(
Arrays.asList(
new String[] {
"One",
"Two",
"Three"
}));
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
//Next we set the AbstractNumId. This requires care.
//Since we are in a new document we can start numbering from 0.
//But if we have an existing document, we must determine the next free number first.
cTAbstractNum.setAbstractNumId(BigInteger.valueOf(0));
//Bullet list
CTLvl cTLvl = cTAbstractNum.addNewLvl();
cTLvl.setIlvl(BigInteger.valueOf(0)); // set indent level 0
cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET);
cTLvl.addNewLvlText().setVal("•");
XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
XWPFDocument document = new XWPFDocument();
XWPFNumbering numbering = document.createNumbering();
BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
BigInteger numID = numbering.addNum(abstractNumID);
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The default list:");
for (String string : documentList) {
paragraph = document.createParagraph();
paragraph.setNumID(numID);
// font size for bullet point in half pt
paragraph.getCTP().getPPr().addNewRPr().addNewSz().setVal(BigInteger.valueOf(48));
run = paragraph.createRun();
run.setText(string);
run.setFontSize(24);
}
paragraph = document.createParagraph();
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("The list having defined gap between bullet point and text:");
for (String string : documentList) {
paragraph = document.createParagraph();
paragraph.setNumID(numID);
paragraph.getCTP().getPPr().addNewRPr().addNewSz().setVal(BigInteger.valueOf(48));
// set indents in Twips (twentieth of an inch point, 1440 Twips = 1 inch
paragraph.setIndentFromLeft(1440/4); // indent from left 360 Twips = 1/4 inch
paragraph.setIndentationHanging(1440/4); // indentation hanging 360 Twips = 1/4 inch
// so bullet point hangs 1/4 inch before the text at indentation 0
run = paragraph.createRun();
run.setText(string);
run.setFontSize(24);
}
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordSimplestBulletList.docx");
document.write(out);
out.close();
document.close();
}
}要进一步回答有关列表在项目符号和文本之间有空格的问题,请参阅以下内容。
该设置必须在列表定义本身中通过在列表级别设置中将级别后缀设置为空格来完成。
cTLvl.addNewSuff().setVal(STLevelSuffix.SPACE);
完整示例:
import java.io.File;
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 org.openxmlformats.schemas.wordprocessingml.x2006.main.STLevelSuffix;
import java.util.ArrayList;
import java.util.Arrays;
import java.math.BigInteger;
public class CreateWordSimplestBulletListSpace {
public static void main(String[] args) throws Exception {
ArrayList<String> documentList = new ArrayList<String>(
Arrays.asList(
new String[] {
"One",
"Two",
"Three"
}));
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
//Next we set the AbstractNumId. This requires care.
//Since we are in a new document we can start numbering from 0.
//But if we have an existing document, we must determine the next free number first.
cTAbstractNum.setAbstractNumId(BigInteger.valueOf(0));
//Bullet list
CTLvl cTLvl = cTAbstractNum.addNewLvl();
cTLvl.setIlvl(BigInteger.valueOf(0)); // set indent level 0
cTLvl.addNewNumFmt().setVal(STNumberFormat.BULLET);
cTLvl.addNewSuff().setVal(STLevelSuffix.SPACE);
cTLvl.addNewLvlText().setVal("•");
XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
XWPFDocument document = new XWPFDocument();
XWPFNumbering numbering = document.createNumbering();
BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
BigInteger numID = numbering.addNum(abstractNumID);
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The list having space between bulltet point and text:");
for (String string : documentList) {
paragraph = document.createParagraph();
paragraph.setNumID(numID);
// font size for bullet point in half pt
paragraph.getCTP().getPPr().addNewRPr().addNewSz().setVal(BigInteger.valueOf(48));
run = paragraph.createRun();
run.setText(string);
run.setFontSize(24);
}
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordSimplestBulletList.docx");
document.write(out);
out.close();
document.close();
}
}但如上所述,这并不是推荐的。缺点是:这会影响基于该定义的所有列表。无法确定项目符号和文本之间的间距。如果项目包含多行文本,则第二行文本紧接在项目符号下开始,而不是像另一个示例中那样缩进。
https://stackoverflow.com/questions/58287694
复制相似问题