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

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

问题描述

我有两个段落,我希望每行前有 100 pt 的空间.我们可以在 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?

任何帮助将不胜感激.

谢谢.

推荐答案

得到答案..

    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天全站免登陆