我想在Word文档中通过Apache设置选项卡大小。
我有一个标题,应该在它的标题行中进行两次冲突,如下所示:
| filed1 -> field2 |垂直线表示页面的边缘。我希望两个字段之间的选项卡都是大的,这样第一个字段在页面上是左对齐的,而右边的字段对页是对齐的。
使用Word本身非常容易,但我只知道如何用POI添加选项卡,而不是如何设置选项卡的宽度。
我试着用Apaches工具调查Word文件,但没有看到标签大小被埋在文件中的地方。
谢谢你的帮助,麦克
发布于 2018-09-26 15:28:42
制表符是Word段落中的设置。虽然使用制表符是一件很常见的事情,也是文字处理中一个非常古老的过程,但如果不使用apache poi的底层ooxml对象,就不可能做到这一点。
示例:
注:制表机止回阀的测量单位是twips (二十英寸点)。
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;
import java.math.BigInteger;
public class CreateWordHeaderWithTabStops {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The Body...");
// create header
XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
// header's first paragraph
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
// create tab stops
int twipsPerInch = 1440; //measurement unit for tab stop pos is twips (twentieth of an inch point)
CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
tabStop.setVal(STTabJc.CENTER);
tabStop.setPos(BigInteger.valueOf(3 * twipsPerInch));
tabStop = paragraph.getCTP().getPPr().getTabs().addNewTab();
tabStop.setVal(STTabJc.RIGHT);
tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));
// first run in header's first paragraph, to be for first text box
run = paragraph.createRun();
run.setText("Left");
// add tab to run
run.addTab();
run = paragraph.createRun();
run.setText("Center");
// add tab to run
run.addTab();
run = paragraph.createRun();
run.setText("Right");
FileOutputStream out = new FileOutputStream("CreateWordHeaderWithTabStops.docx");
doc.write(out);
doc.close();
out.close();
}
}https://stackoverflow.com/questions/52497367
复制相似问题