1

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>
user1772306
  • 449
  • 1
  • 7
  • 20

2 Answers2

3

Add the xml:space="preserve" attribute to a run with text in it, starting or ending with whitespace. For example:

    <w:r>
        <w:t  xml:space="preserve">the text prior to the word </w:t>
    </w:r>
JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • 1
    Type a sentence in Word. Force it to use several runs, either by making a word bold, or by mistyping. Save and unzip the result. You'll see this is how you do it. Hard to say what you are doing wrong, unless you can put your resulting docx somewhere.... – JasonPlutext May 17 '16 at 12:09
0

If you're using Docx4j then,

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

Found the answer from here: