使用Apache POI docx在MS Word文档中进行间距和边距设置 [英] Spacing and Margin settings in MS Word document using Apache POI docx

查看:1027
本文介绍了使用Apache POI docx在MS Word文档中进行间距和边距设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两段,我想在每一行之前有100磅的空间。我们有办法在 Apache POI 中做什么?

I have two paragraphs and i want 100 pt space before each line. Is there a way we can do in Apache POI?

以下是代码段

XWPFDocument doc = new XWPFDocument();
XWPFParagraph documentTitle = doc.createParagraph();

documentTitle.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = documentTitle.createRun();

run.setText("Paragraph 1");
run.setBold(true);
run.setFontFamily("Calibri");
run.setFontSize(13);
run.setColor("4F81BD");

run.addBreak();

run.setText("Paragraph 2");
run.setBold(true);
run.setFontFamily("Calibri");
run.setFontSize(13);
run.setColor("4F81BD");

这里如何添加 100 pt 之间的空格两段?有什么方法可以实现这个目标吗?
addBreak()两行之间没有任何空格。

Here how to add 100 pt space between two paragraphs? Is there any way we can achieve this? addBreak() is not keeping any space between two lines.

以及如何设置边距在docx?

And how to set margin spacing in docx?

任何帮助都将不胜感激。

Any help would be appreciated.

谢谢。

推荐答案

得到答案..

    documentTitle.setAlignment(ParagraphAlignment.CENTER);
    // This does the trick
    documentTitle.setSpacingBefore(100);

它在文本的每一行之间留下100pt的空间

It left me 100pt space between each line of the text

如果要为文档添加自定义边距。使用此代码。

If you want to add custom margins to your document. use this code.

    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    CTPageMar pageMar = sectPr.addNewPgMar();
    pageMar.setLeft(BigInteger.valueOf(720L));
    pageMar.setTop(BigInteger.valueOf(1440L));
    pageMar.setRight(BigInteger.valueOf(720L));
    pageMar.setBottom(BigInteger.valueOf(1440L));

这篇关于使用Apache POI docx在MS Word文档中进行间距和边距设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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