Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docx4j - Why is the text truncated of spaces?

Tags:

java

docx4j

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.

like image 825
CDT Avatar asked Feb 11 '23 09:02

CDT


1 Answers

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");
...
like image 57
Ben Avatar answered Feb 14 '23 01:02

Ben