Spacing and Margin settings in MS Word document using Apache POI docx

自古美人都是妖i 提交于 2019-12-09 01:50:39

问题


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

Here is the code snippet

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

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.

And how to set margin spacing in docx?

Any help would be appreciated.

Thanks.


回答1:


Got the answer..

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

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


来源:https://stackoverflow.com/questions/17787176/spacing-and-margin-settings-in-ms-word-document-using-apache-poi-docx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!