Search code examples
c#parsingms-wordopenxmloffice-interop

How to Preserve string with formatting in OpenXML Paragraph, Run, Text?


I am following this structure to add text from strings into OpenXML Runs, Which are part of a Word Document.

The string has new line formatting and even paragraph indentions, but these all get stripped away when the text gets inserted into a run. How can I preserve it?

Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

String txt = "Some formatted string! \r\nLook there should be a new line here!\r\n\r\nAndthere should be 2 new lines here!"

// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));

Solution

  • You need to use a Break in order to add new lines, otherwise they will just be ignored.

    I've knocked together a simple extension method that will split a string on a new line and append Text elements to a Run with Breaks where the new lines were:

    public static class OpenXmlExtension
    {
        public static void AddFormattedText(this Run run, string textToAdd)
        {
            var texts = textToAdd.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
    
            for (int i = 0; i < texts.Length; i++)
            {
                if (i > 0)
                    run.Append(new Break());
    
                Text text = new Text();
                text.Text = texts[i];
                run.Append(text);
            }
        }
    }
    

    This can be used like this:

    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(@"c:\somepath\test.docx", true))
    {
        var body = wordDoc.MainDocumentPart.Document.Body;
    
        String txt = "Some formatted string! \r\nLook there should be a new line here!\r\n\r\nAndthere should be 2 new lines here!";
    
        // Add new text.
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
    
        run.AddFormattedText(txt);
    }
    

    Which produces the following output:

    enter image description here