问题
I have a paragraph Justified with a PPr object. All paragraph is justified but the last line not.
I know that it's necessary use a LineBreak after last line to give the correct justification. I try with PPr with set a JC object but doesn't work
PPr bothPPr = factory.createPPr();
Jc justified = factory.createJc();
bothPPr.setJc(justified);
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Long text here...");
r.getContent().add(t);
r.getContent().add(factory.createBr());
p.getContent().add(r);
p.setPPr(bothPPr);
All text is correctly justified, but last line isn't.
My actual last justified line:
This is my last line
But I want:
This is my last line
After I open my generate docx file, I can see the wrong justification. But if I press Enter Button at the end of line, the justification works good.
回答1:
It turns out there is actually an obscure compat setting which will give the behaviour you want. From https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/cc844177%28v%3Doffice.14%29
[ISO/IEC 29500-1 1st Edition]
17.15.3.5 doNotExpandShiftReturn (Don't Justify Lines Ending in Soft Line Break)
This element specifies whether applications should fully justify the contents of incomplete lines which end in a soft line break when the parent paragraph is fully justified using the jc element (§17.3.1.13).
Typically, applications shall fully justify all lines in a paragraph when that setting is specified using the jc element except for the last line in the paragraph (the line ending with the paragraph mark). This element, when present with a val attribute value of true (or equivalent), specifies that any line which ends in a soft line break shall also not be fully justified when the paragraph specifies that setting.
来源:https://stackoverflow.com/questions/55614870/how-to-correct-display-last-line-of-justified-paragraph-docx4j