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)
@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).
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.
@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.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.