简体   繁体   中英

How can I insert a line break in a Word document using DocX?

I'm using DocX (the library, not the file format) to programmatically create a word document from scratch using C#. It's all going really well except for one thing:

I want to add a paragraph that contains a code listing and I want the lines to be all contained in a single paragraph and separated with line breaks. My intent is to apply formatting to the paragraph (ie the entire code listing) once and not have to format each line individually.

By creating a simple document and examining it, I have determined that in the Office XML file, a line break maps to a <w:br/> element, but I can't see any way to add this within DocX. Any ideas?

Not sure I understand what you are looking for but maybe I could help. Maybe the approach with "\\n" will be giving the expected result:

using (DocX document = DocX.Create("Test.docx"))
{               
            Paragraph p = document.InsertParagraph();
            p.Append("Hello World \nHello world!").Font(new FontFamily("Arial Black"));
            document.Save();
}

If I didn't get it right maybe this article will help you solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM