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

▼魔方 西西 提交于 2019-11-28 00:54:17

问题


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/>");



来源:https://stackoverflow.com/questions/24193619/inserting-a-line-break-not-a-paragraph-break-programatically-to-a-word-documen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!