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

前端 未结 1 1246
南旧
南旧 2021-01-21 07:30

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 indent

相关标签:
1条回答
  • 2021-01-21 08:20

    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:

    0 讨论(0)
提交回复
热议问题