Setting text of a Run to something that has \n does not work

Howdy,

We have Aspose.Word 20.3. I set the text of a Run to something that had “\n” in it. When I download the document afterwords, the “\n” are no where to be found. Any ideas? UPDATE:

Maybe I’m just confused on what \n and \v mean in Word. So when we copy text that has \n in it, and paste it in word, it has hard returns where the \n were image.png (9.8 KB)
. But when setting a Run text using aspose, the \n disappear

I split the text on the “\n” and then made a new paragraph for each array element. Surely there has to be a better way

@derek444 Actually ControlChar.LineFeed (\n) character is not normally used in MS Word especially in Run node text. If you need to have multiline text in the run you should use ControlChar.LineBreak (\v) character - soft line break.
The code like the following:

string text = "test\ntest";

Document doc = new Document();
Run r = new Run(doc);
r.Text = text;
doc.FirstSection.Body.FirstParagraph.AppendChild(r);
doc.Save(@"C:\Temp\out.docx");

Produces the following XML representation in DOCX:

<w:p w:rsidR="00A77B3E">
	<w:r>
		<w:t>
			test
			test
		</w:t>
	</w:r>
</w:p>

As you can see line break is there, but MS Word interprets such line break as whitespace. This is an expected behavior.
If you use \v instead of \n in the text string, a special <w:br /> tag is inserted, which corresponds a soft line break:

<w:p w:rsidR="00A77B3E">
	<w:r>
		<w:t>test</w:t>
		<w:br />
		<w:t>test</w:t>
	</w:r>
</w:p>

Also, you can use ControlChar.ParagraphBreak (\r) character, the paragraph break character inserted into Run.Text will not actually produce paragraph break, but insted a special <w:cr /> break will be inserted:

string text = "test\rtest";
<w:p w:rsidR="00A77B3E">
	<w:r>
		<w:t>test</w:t>
		<w:cr />
		<w:t>test</w:t>
	</w:r>
</w:p>

If you need that \n character produces an actual paragraph break, you can use DocumentBuilder to insert the content. For example:

string text = "test\ntest";

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write(text);
doc.Save(@"C:\Temp\out.docx");

produces two paragraphs:

<w:p w:rsidR="00A77B3E">
	<w:r>
		<w:t>test</w:t>
	</w:r>
</w:p>
<w:p w:rsidR="00A77B3E">
	<w:r>
		<w:t>test</w:t>
	</w:r>
</w:p>

The same output will be produced with the following strings:

string text = "test\rtest";
string text = "test\r\ntest";

When I copy and paste into word with “\n”, it separates them into their own paragraphs

@derek444 MS Word behaves the same way as Aspose.Words, when you use code like this:

string text = "test\ntest";

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write(text);
doc.Save(@"C:\Temp\out.docx");

MS Word does not put text into a Run node, but interprets the string, just like Aspose.Words does.

So instead of initializing a run and setting its text, I should just use a builder and write the text? Can I do that in a StructuredDocumentTag?

@derek444 Sure, you can move DocumentBuilder cursor into SDT and insert content. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create SDT for demonstration purposes.
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
sdt.RemoveAllChildren();
sdt.AppendChild(new Paragraph(doc));
sdt.IsShowingPlaceholderText = false;
doc.FirstSection.Body.AppendChild(sdt);

// Move document builder cursor into the SDT.
builder.MoveTo(sdt.FirstChild);
builder.Write("Test\nTest\nTest");

doc.Save(@"C:\Temp\out.docx");

Appreciate it!

1 Like