user3731528
user3731528

Reputation: 333

Inserting A Line Break (Not A Paragraph Break) Programatically To A Word Document

I am using the Office Developer Tools for Visual Studio 2013 in C#. Ever since Word 2007, adding a "\n" character adds a paragraph break (which adds more space than the line break in Word). How can I add a line break to a document? I've tried "\n", "\r", and "\r\n" all of which seem to add a paragraph break.

Upvotes: 10

Views: 29158

Answers (4)

Shadman
Shadman

Reputation: 151

I've tried to use several items such as:

  • </br>
  • \n\r
  • \u2028\n
  • <w:br/>

which they don't work but when simply replace desired character with <br/> it works very well!

I have to mention that i use VS2013, and MS Office2016.

Upvotes: 3

Alexander Buturlakin
Alexander Buturlakin

Reputation: 29

That works for me: stringBuilder.Append("<w:br/>");

Upvotes: 2

user3731528
user3731528

Reputation: 333

It turns out that Word uses Vertical Tabs for its line breaks. I was able to add them using the "\v" character to ranges. See What is a vertical tab? for more details.

Upvotes: 23

Timothy Shields
Timothy Shields

Reputation: 79611

Word is using the "Line Separator" and "Paragraph Separator" Unicode characters, with codepoints 2028 and 2029, respectively, to represent those respective kinds of breaks.

Use the Unicode character "Line Separator," expressed in C# as '\u2028'. Use this in combination with the newline character \n.

Upvotes: 2

Related Questions