Apache POI项目符号间距 [英] Apache POI bullet spacing

查看:88
本文介绍了Apache POI项目符号间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中生成带有项目符号的Word文档.默认情况下,项目符号后跟文本前有一个较大的空格.在Word中,在缩进列表中的数字后跟选项"下,可以将此空格设置为制表符,空格字符或不设置.默认情况下,在POI中将其设置为选项卡,以显示它.如何更改为使用空格字符?

I am working on a project to generate a word document that has bullets. By default the bullets are followed by a large gap before the text. In Word, under List Indents Follow number with option, you can set this space to be a tab character, space character or none. By default in POI it is set to tab it appears. How can I change this to use space character?

推荐答案

我强烈建议使用制表位,因为这是 Word 中的默认设置,正确使用此选项还可以正确显示项目符号指向后的多行文本.

I would highly recommend to use tab stop as this is the default in Word and correctly using this also leads to a proper presentation of multi lined text after the bullet points.

项目符号和文本之间的间距由段落缩进设置确定.左凹痕确定文本的位置,而悬空凹痕确定项目符号要在文本之前悬挂多少.

The gap between bullet point and text is determined by the paragraphs indentation settings. The left indentation determines the text position and the hanging indentation determines how much the bullet point hangs before the text.

示例:

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

 }
}


要进一步回答有关列表在项目符号和文本之间有空格的问题,


To additional answer the question about a list having space between bullet point and text, here it is.

必须在列表定义本身中通过在列表级别设置中将级别后缀设置为空格来完成该设置.

That setting must be done in list definition itself by setting level suffix to space in list levels settings.

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

 }
}

但是,如上所述,不建议这样做.缺点是:这会影响基于该定义的所有列表.无法确定项目符号和文本之间的距离.如果项目包含多行文本,则第二个文本行将立即在项目符号点下方开始,而不像其他示例中那样缩进.

But as said, this is not to recommend. Disadvantages are: This affects all lists based on that definition. There is no possibility to determine the gap between bullet point and text. If items contain multiple line of text then the second text line starts immediately under the bullet point, not indented as in the other example.

这篇关于Apache POI项目符号间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆