docx4j - Why is the text truncated of spaces?

☆樱花仙子☆ 提交于 2019-12-21 06:00:50

问题


Here is the code:

    P para = factory.createP();
    R run = factory.createR();
    Text text = factory.createText();

    text.setValue( "              abc                " );
    run.getContent().add( text );
    para.getContent().add( run );
    wordMLPack.getMainDocumentPart().add( para );

Here is the docx generated:

The heading and trailing spaces are all missing.


回答1:


You need to tell docx4j to explicitly preserve whitespace in your Text instances (the underlying format is XML of course, which tends not to pay much heed to whitespace). Something like this:

text.setValue("              abc             ");
text.setSpace("preserve");
...


来源:https://stackoverflow.com/questions/27400878/docx4j-why-is-the-text-truncated-of-spaces

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