Я бы настоятельно рекомендовал использовать табуляцию, так как это значение по умолчанию в 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.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.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();
}
}
Но, как сказано, это не рекомендуется. Недостатки: это влияет на все списки на основе этого определения. Нет возможности определить разрыв между точкой маркера и текстом. Если элементы содержат несколько строк текста, вторая текстовая строка начинается сразу под точкой с маркером, а не с отступом, как в другом примере.