We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Word(version 20.11.0.0) Replace text in word document BUT unexpected space character appears

Hello Aspose Supporter.

We are using your Appose.Word(version 20.11.0.0) in our project to replace text in template word document(docx).

My template is very simple like that:
WordTemplate.png (10.7 KB)

And I used the code like below to replace text inside the template:
SourceCode.png (39.6 KB)

BUT I got output like that:
Output.png (37.0 KB)

I also tried your latest version Aspose.Word(version 22.1.0.0), and I got the same problem.
Can you please check the thing I mentioned to see if the problem comes from Appose.Word library?

Thank you very much for your checking and feedback.
If you need more information about my problem, please let me know.

Yours.
Nghia

@nguyen.xuan.nghia You have encountered an expected behavior. When you use \r\n in your replacement value you do not actually insert a paragraph breaks as you might expect. In this case \r\n is inserted as simple text and is interpreted by MS Word. If you unzip DOCX document you will see the following xml

		<w:p w:rsidR="002B2CA3" w14:paraId="3ABE5424" w14:textId="317B547E">
			<w:r>
				<w:t>Hello World</w:t>
				<w:cr />
				<w:t xml:space="preserve">
Hello </w:t>
				<w:cr />
				<w:t>
					World
				</w:t>
			</w:r>
		</w:p>

As you can see all the text is in once paragraph. If you would like to insert a real paragraph break you should use Aspose.Words metacharacters:

  • &p - paragraph break
  • &b - section break
  • &m - page break
  • &l - manual line break
    For example see the following code"
Document doc = new Document(@"C:\Temp\in.docx");
doc.Range.Replace("[FirstData1]", "Hello World&pHello &pWorld", new FindReplaceOptions());
doc.Save(@"C:\Temp\out.docx");

Here the output document produced by this code: out.docx (9.3 KB)
In the document.xml you can notice the text is inserted into separate paragraphs:

		<w:p w:rsidR="002B2CA3">
			<w:r>
				<w:t>Hello World</w:t>
			</w:r>
		</w:p>
		<w:p w:rsidR="002B2CA3">
			<w:r>
				<w:t xml:space="preserve">Hello </w:t>
			</w:r>
		</w:p>
		<w:p w:rsidR="002B2CA3" w14:paraId="3ABE5424" w14:textId="317B547E">
			<w:r>
				<w:t>World</w:t>
			</w:r>
		</w:p>

@alexey.noskov
Thank you very much for your answer. Looks like my explanation is not enough clear for you.
I wanted to replace text to have all the text in once paragraph. I did not want to insert a real paragraph.

Please see again my problem and my expected thing in the picture:
ProblemAndExpectedResult.png (42.9 KB)

Base on your checking result for my output, I saw a strange thing that:
There was a attribute xml:space=“preserve” appears for the text “Hello”. Other text did not have that attribute.
Watch:

    <w:p w:rsidR="002B2CA3" w14:paraId="3ABE5424" w14:textId="317B547E">
			<w:r>
				<w:t>Hello World</w:t>
				<w:cr />
				<w:t xml:space="preserve">
Hello </w:t>
				<w:cr />
				<w:t>
					World
				</w:t>
			</w:r>
</w:p>

Did it cause my problem?

Hope my information is enough for you now to understand my problem. If not, please let me know.
Thank you very much for your checking and answer.

Yours.
Nghia

@nguyen.xuan.nghia Carriage return followed by line feed character ("\x000d\x000a" or "\r\n") is not used as such in Microsoft Word documents, but commonly used in text files for paragraph breaks. "\r" character is interpreted as a paragraph break in MS Word documents, in your case is inserted as <w:cr /> special character and then interpreted by MS Word as a paragraph break character.
xml:space="preserve" is required because the text in the run contains whitespaces - at the beginning ("\n") and at the end (whitespace).
Normally in MS Word if you need to insert a line break within the paragraph you should use line break character: “\x000b” or “\v” (Shift+Enter in MS Word) this inserts soft line break and does not create a new paragraph.
So in your case, to get the desired result, you should either use metacharacter &p or soft line break character "\v" instead of "\r\n" sequence in your replacement.

@alexey.noskov
Thank you very much for your support. After implementing your solution, my application works as expected.

Yours.
Nghia