Search code examples
javadocx4j

docx4j - Why is the text truncated of spaces?


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:

enter image description here

The heading and trailing spaces are all missing.


Solution

  • 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");
    ...