68

I am using openxml WordProcessingDocument to open a Word template and replace placeholder x1 with a string. This works fine unless I need the string to contain a newline. How can I replace x1 with text may contain newlines that word would recognise? I have tried \n \r but these do not work

Just to explain further when the word template is opened I read it into a StreamReader then use .Replace to replace x1.

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Danny
  • 2,771
  • 5
  • 30
  • 42

4 Answers4

111

To insert newlines, you have to add a Break instance to the Run.

Example:

run.AppendChild(new Text("Hello"));
run.AppendChild(new Break());
run.AppendChild(new Text("world"));

The XML produced will be something like:

<w:r>
  <w:t>Hello</w:t>
  <w:br/>
  <w:t>world</w:t>
</w:r>
outofmind
  • 1,430
  • 1
  • 20
  • 37
codeape
  • 97,830
  • 24
  • 159
  • 188
  • I don't think I can use your answer in my situation. I updated my question to explain further – Danny May 20 '10 at 09:18
  • So you're working with the XML directly? In that case, try inserting XML like the example. – codeape May 20 '10 at 10:22
  • 2
    Note, that in PPTX / PresentationML the `Break()` has to be child of the `Paragraph`, not child of the `Run` – outofmind Sep 17 '15 at 13:07
30

Here's a C# function that will take a string, split it on line breaks and render it in OpenXML. To use, instantiate a Run and pass it into the function with a string.

void parseTextForOpenXML( Run run, string textualData )
{
    string[ ] newLineArray = { Environment.NewLine };
    string[ ] textArray = textualData.Split( newLineArray, StringSplitOptions.None );

    bool first = true;

    foreach ( string line in textArray )
    {
        if ( ! first )
        {
            run.Append( new Break( ) );
        }

        first = false;

        Text txt = new Text( );
        txt.Text = line;
        run.Append( txt );
    }
Toolsmythe
  • 411
  • 5
  • 7
  • 1
    niceeeee solution,thank's a lot, i use this solution when my string contains \n\r – habiat Dec 07 '14 at 14:34
  • 4
    Thanks, I modified `string[ ] newlineArray = { Environment.NewLine, "\n", "\r\n", "\n\r" }` to make it work for all type of break. 8 years past but still it's the best answer. – its4zahoor Feb 14 '19 at 12:45
13

Altough this question is already answered I have another approach to solve questions like :

How can I make XXX with OpenXML??

In this cases you could make use of the powerful Microsoft OpenXML productivity tool (also known as OpenXmlSdkTool). Download here.

  1. Create a new office document
  2. Add the parts to the document which you would like to reproduce with OpenXML SDK.
  3. Open the office document with Microsoft OpenXML productivity tool
  4. Click on "Reflect Code"
  5. On the right side you will see now you document reflected into C# code.
Christian Junk
  • 1,000
  • 1
  • 8
  • 22
Mischa
  • 635
  • 6
  • 14
  • This is a great answer! I was after working out some tricky open xml bits and had to google the inner workings of almost every element. Having a tool which can output the trick bits and paste them in is a major timesaver, thanks. – dougajmcdonald Dec 11 '15 at 09:52
12

I have the same issue and in my case <w:br /> tag worked.

cha0site
  • 10,517
  • 3
  • 33
  • 51
parm
  • 121
  • 1
  • 2