Apache poi XWPF Paragraph line spacing Apache poi XWPF Paragraph line spacing apache apache

Apache poi XWPF Paragraph line spacing


I also faced the same problem and found a solution after going through the ooxml-schemas source and the internals of the docx file generated. Here is the code

public void setSingleLineSpacing(XWPFParagraph para) {    CTPPr ppr = para.getCTP().getPPr();    if (ppr == null) ppr = para.getCTP().addNewPPr();    CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();    spacing.setAfter(BigInteger.valueOf(0));    spacing.setBefore(BigInteger.valueOf(0));    spacing.setLineRule(STLineSpacingRule.AUTO);    spacing.setLine(BigInteger.valueOf(240));}


I recently have to use the line spacing with POI,and my approach was get the paragraph and get the ooxml-schema

XWPFParagraph paragraph;xml = paragraph->getCTP()->ToString();

then the schema will be stored as a String in that xml variable and you have to retrieve the value from this with some string function

<w:spacing w:lineRule='atLeast' w:line='240' />

the w:line is the one you need

Hope this helps!


paragraph.setSpacingBetween(1);

It will create a line spacing of 1