Need to set specific Line Space in Document

I want to set up specific line space in my document like I’m setting the font size and letter type.

for line spacing, I’ve tried the following code snippet to apply the line space. But it somehow results incorrect spacing (Not aligned with windows line spacing)

doc.sections.forEach { section ->
        section.body.paragraphs.forEach { paragraph ->
            paragraph.paragraphFormat.lineSpacing = 2.0
        }
    }

Also please suggest if there are any other options is there instead of iterating through each paragraph.
result.zip (12.9 KB)

@nobilex In your case paragraphs has LineSpacingRule set to MULTIPLE. In this case one line equals 12 points. So if you would like to set double line spacing, you should set it as two lines, i.e. 24 points.

Iterable<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph p : paragraphs)
{
    if (p.getParagraphFormat().getLineSpacingRule() == LineSpacingRule.MULTIPLE)
        p.getParagraphFormat().setLineSpacing(24);
}

You can set formatting applied to the paragraph in styles, but formatting that is explicitly specified in the paragraph always overrides the formatting inherited from the style.

Thanks, @alexey.noskov
It’s working for the attached document.

I just wanted to confirm that, Can I consider one line equal to 12 points for all the LineSpacingRule? (Of course, the impact is required when no formatting is explicitly specified in the paragraph).

@nobilex

No, this applies only to LineSpacingRule.MULTIPLE for other LineSpacingRules the specified value is considered as a value in points.
If you need to apply 2 lines line spacing to all paragraphs, you can also specify LineSpacingRule.MULTIPLE to all paragraphs.

Hello @alexey.noskov,
I’ve some doubts about your comment that “For other LineSpacingRules” the specified value is considered as a value in points.
In that, I’ve checked that If I want to set line space to 1.0 (considering this can be changed/configurable) with LineSpacingRule.EXACTLY. It required to be multiplied by 12.
So should I consider that 12 points are Equals to 1 line for all LineSpacingRule?

@nobilex No, 12 is not considered as one line unless LineSpacingRule.MULTIPLE is used. Line height depends on the font size. For example consider the following simple code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

String test = "First line"+ ControlChar.LINE_BREAK +"second line";

// Use 24 font size for testing.
builder.getFont().setSize(24);

// insert paragraph with LineSpacingRule.EXACTLY
builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.EXACTLY);
builder.getParagraphFormat().setLineSpacing(12);
builder.writeln(test);

// And another paragraph with LineSpacingRule.MULTIPLE
builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE);
builder.getParagraphFormat().setLineSpacing(12);
builder.write(test);

doc.save("C:\\Temp\\out.docx");

In the first paragraph the lines will overlap, while in the second they will not, because line spacing is calculated as 1 line.

Thanks, @alexey.noskov for clearing my doubts.

Now can you please suggest which LineSpacingRule I should set for the configurable line space?

Suppose I implement as following. will it work?

builder.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE);
builder.getParagraphFormat().setLineSpacing(ConfugurableValue * 12);

Please note configurable values could be 1.0, 1.15, 2.0, 3.0

@nobilex Yes, your code is correct. Line spacing in this case is specified in lines, i.e. one line 1.15 lines, double (two lines), and three lines respectively.