How to correct display last line of justified paragraph Docx4j

萝らか妹 提交于 2021-02-11 12:36:05

问题


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

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