Inserting A Line Break (Not A Paragraph Break) Pro

2019-01-25 19:41发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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