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