Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new line in openxml with powershell

Using powershell with openxml I replace values and output the result as .docx file.

I use

[OpenXmlPowerTools.SearchAndReplacer]::SearchAndReplace($original,"John","Jane"+"<w:br/>",$false)

I got

Jane '<w:br/>' next character goes here

I expect

Jane  
next character goes here

Does anyone see my mistake?

like image 692
jjk Avatar asked Mar 22 '26 16:03

jjk


1 Answers

Line breaks in OpenXML are child nodes of run nodes, and siblings of text nodes.

You're passing in the correct XML node for a break, but it's being inserted into a text node as opposed to being added to the run node.

E.g.

<w:p>
    <w:r>
        <w:t>
            Jane '<w:br/>' next text goes here
        <w:t/>
    <w:r/>
<w:p/>

Instead of:

<w:p>
    <w:r>
        <w:t>
            Jane
        <w:t/>
        <w:br/>
        <w:t>
            next text goes here
        <w:t/>
    <w:r/>
<w:p/>
like image 116
Austin Drenski Avatar answered Mar 24 '26 08:03

Austin Drenski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!