10

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.

user3731528
  • 333
  • 1
  • 2
  • 9

4 Answers4

23

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.

Community
  • 1
  • 1
user3731528
  • 333
  • 1
  • 2
  • 9
3

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.

Shadman
  • 131
  • 1
  • 10
2

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.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
  • Nice find! I did not know these existed – Forest Kunecke Jun 12 '14 at 20:56
  • Thank you for the quick reply. Unfortunately, when I use '\u2028', it adds space to the existing line, but doesn't move to the next line. Strangely, the same thing happens with '\u2029'. – user3731528 Jun 12 '14 at 20:57
  • @user3731528 I've updated the answer. I believe you need `"apple\u2028\nbanana"` will put a line break between apple and banana, and `"apple\u2029\nbanana"` will put a paragraph break. – Timothy Shields Jun 12 '14 at 21:00
  • Thanks again for the help. Unfortunately, it still seems to be giving me paragraph breaks even when using "\u2028\n". When I press (for a line break in word), it adds less space between the lines than the text the program generated did. – user3731528 Jun 12 '14 at 21:10
  • @user3731528 Can you get the `string` content of a section of a word document that contains some "shift+enter" line breaks? If you have such a `string`, inspect the individual `char` elements of the `string` to figure out what Unicode characters constitute a MS Word "shift+enter" line break. My answer was making the assumption that a line break was represented as the appropriate Unicode character, but that's partially a guess. If you do the suggested inspection, or provide code in your question that parses a document like you are, I can try to give you a better answer. – Timothy Shields Jun 13 '14 at 03:38
  • Turns out that word does not use '\u2028' as a line break. They use vertical tabs characters for line breaks as seen in the second answer here: http://stackoverflow.com/questions/3380538/what-is-a-vertical-tab – user3731528 Jun 26 '14 at 20:26
  • This solution worked for me, while '\v' didn't (I am using docx library for creating the word document). – omerts Aug 07 '16 at 18:27
2

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