Several APIs exist, e.g. docx4j, Apache POI, Microsoft's SDK, which can manipulate docx files. I used docx4j to create the following snippet. However, it does not create a space between "text prior to the word" and "this is text". I wonder if this is a bug or I can do anything about it.
You can unzip a docx file and zip it again (only the content; not the folder!) in order to make changes on the document.xml, hence the text of your docx. The logic a space is created seems weird to me. Does anyone have an anwser on how to sufficiently create
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>the text prior to the word</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r>
<w:t> </w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:commentRangeStart w:id="1"/>
<w:r>
<w:t>this is text</w:t>
</w:r>