Insert rtf text into exists document

I want to insert rtf text into an exists document.
I attached the document I want to edit.
The document has a node with tag “RtfText”,
I want the value of the node will be the following rtf text

"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\rtlpar\fs18\f2\cf0 \cf0\qr\sl75\slmult0{\f2 \rtlpar{\rtlch 87.08 \u1496?\u1489?\u1493?\u1512?\u1497? \u1488?\u1493?\u1508?\u1503? \u1500?\u1512?\u1499?\u1489? - Wheelhubs \u1488?\u1500?\u1493? \u1492?\u1501? \u1495?\u1500?\u1511?\u1497?\u1501? \u1502?\u1502?\u1506?\u1512?\u1499?\u1514? \u1489?\u1500?\u1497?\u1502?\u1492?. \u1505?\u1497?\u1493?\u1493?\u1490?\u1501? \u1489?\u1508?\u1512?\u1496? .87.08.9950 \u1514?\u1497?\u1511? \u1505?\u1497?\u1493?\u1493?\u1490?/293/90 12/90}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  }  }"

How can I do it?
Do you have a code example for me in C# ?
Thanks!
TestTemplate.docx (42.4 KB)

@riky12345 You can use DocumentBuilder.InsertDocument method to achieve this. Please see the following simple code:

string rtfString = @"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\rtlpar\fs18\f2\cf0 \cf0\qr\sl75\slmult0{\f2 \rtlpar{\rtlch 87.08 \u1496?\u1489?\u1493?\u1512?\u1497? \u1488?\u1493?\u1508?\u1503? \u1500?\u1512?\u1499?\u1489? - Wheelhubs \u1488?\u1500?\u1493? \u1492?\u1501? \u1495?\u1500?\u1511?\u1497?\u1501? \u1502?\u1502?\u1506?\u1512?\u1499?\u1514? \u1489?\u1500?\u1497?\u1502?\u1492?. \u1505?\u1497?\u1493?\u1493?\u1490?\u1501? \u1489?\u1508?\u1512?\u1496? .87.08.9950 \u1514?\u1497?\u1511? \u1505?\u1497?\u1493?\u1493?\u1490?/293/90 12/90}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  }  }";

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get SDT with "RtfText" tag
StructuredDocumentTag sdt = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .Where(tag => tag.Tag == "RtfText").FirstOrDefault();
if (sdt != null)
{
    // Move DocumentBuilder cursor to the SDT.
    builder.MoveToStructuredDocumentTag(sdt, 0);
    // Create Document from RTF string and insert it into the target document.
    using (MemoryStream rtfStream = new MemoryStream(Encoding.UTF8.GetBytes(rtfString)))
        builder.InsertDocument(new Document(rtfStream), ImportFormatMode.KeepSourceFormatting);
}

doc.Save(@"C:\Temp\out.docx");

I tried the code you gave me, but it dont works good for me.
If I expose the document in word format I cant edit the value,
also its not guard all the properties in the rtf, like bold font and so…
I have also to delete the original place holder text ,
its still shown in the document
I put you an example of an output document after generated
I hope you can help me with it.
Thank you
the rtf I used in this document is:

{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}{\f3\fcharset0 David;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\rtlpar\fs18\f2\cf0 \cf0\qr\sl75\slmult0{\f2 \rtlpar{\lang1037\ul\rtlch \u1504?\u1505?\u1497?\u1493?\u1503?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\f2 \rtlpar{\lang1037\b\rtlch \u1513?\u1493?\u1512?\u1492? \u1512?\u1488?\u1513?\u1493?\u1504?\u1492? \u1502?\u1493?\u1491?\u1490?\u1513?\u1514?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\f3 \rtlpar{\lang1037\i\rtlch \u1513?\u1493?\u1512?\u1492? \u1513?\u1504?\u1497?\u1492? \u1506?\u1501? \u1504?\u1496?\u1497?\u1492? \u1493?\u1508?\u1493?\u1512?\u1502?\u1496? \u1488?\u1495?\u1512?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\fs24\f2 \rtlpar{\lang1037\rtlch \u1513?\u1493?\u1512?\u1492? \u1513?\u1500?\u1497?\u1513?\u1497?\u1514? \u1490?\u1493?\u1491?\u1500? \u1488?\u1495?\u1512?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\fs24\f2 \rtlpar{\lang1037\rtlch }\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\fs24\f2 \rtlpar{\lang1037\rtlch }\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}    {\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880{{\fs21\intbl \rtlpar{\lang1037\b\rtlch \u1496?\u1493?\u1512? \u1489?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  {\fs21\intbl \rtlpar{\lang1037\b\rtlch \u1496?\u1493?\u1512? \u1488?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  }{\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880\row}}    {\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880{{\intbl \rtlpar{\lang1037\rtlch \u1513?\u1493?\u1512?\u1492? 1 \u1489?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  {\intbl \rtlpar{\lang1037\rtlch \u1513?\u1493?\u1512?\u1492? 1 \u1488?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  }{\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880\row}}  {\fs24\f2 \rtlpar{\lang1037\rtlch }\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  }  }

template (3).Docx (43.6 KB)

!

@riky12345 Please modify your code like this:

string rtfString = @"{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}{\f3\fcharset0 David;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\rtlpar\fs18\f2\cf0 \cf0\qr\sl75\slmult0{\f2 \rtlpar{\lang1037\ul\rtlch \u1504?\u1505?\u1497?\u1493?\u1503?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\f2 \rtlpar{\lang1037\b\rtlch \u1513?\u1493?\u1512?\u1492? \u1512?\u1488?\u1513?\u1493?\u1504?\u1492? \u1502?\u1493?\u1491?\u1490?\u1513?\u1514?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\f3 \rtlpar{\lang1037\i\rtlch \u1513?\u1493?\u1512?\u1492? \u1513?\u1504?\u1497?\u1492? \u1506?\u1501? \u1504?\u1496?\u1497?\u1492? \u1493?\u1508?\u1493?\u1512?\u1502?\u1496? \u1488?\u1495?\u1512?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\fs24\f2 \rtlpar{\lang1037\rtlch \u1513?\u1493?\u1512?\u1492? \u1513?\u1500?\u1497?\u1513?\u1497?\u1514? \u1490?\u1493?\u1491?\u1500? \u1488?\u1495?\u1512?}\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\fs24\f2 \rtlpar{\lang1037\rtlch }\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  {\fs24\f2 \rtlpar{\lang1037\rtlch }\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}    {\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880{{\fs21\intbl \rtlpar{\lang1037\b\rtlch \u1496?\u1493?\u1512? \u1489?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  {\fs21\intbl \rtlpar{\lang1037\b\rtlch \u1496?\u1493?\u1512? \u1488?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  }{\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880\row}}    {\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880{{\intbl \rtlpar{\lang1037\rtlch \u1513?\u1493?\u1512?\u1492? 1 \u1489?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  {\intbl \rtlpar{\lang1037\rtlch \u1513?\u1493?\u1512?\u1492? 1 \u1488?}\li0\ri0\sa0\sb0\fi0\ql\sl75\slmult0\cell  }  }{\trowd\trleft0\trgaph-0\trbrdrt\brdrs\brdrw8\brdrcf0\trbrdrb\brdrs\brdrw8\brdrcf0\trbrdrr\brdrs\brdrw8\brdrcf0\trbrdrl\brdrs\brdrw8\brdrcf0\trbrdrv\brdrs\brdrw8\brdrcf0\trbrdrh\brdrs\brdrw8\brdrcf0\trftsWidth1\trftsWidthB3\trpaddl10\trpaddr10\trpaddb10\trpaddt10\trpaddfl3\trpaddfr3\trpaddft3\trpaddfb3\trql\ltrrow\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx1440\clvertalt\clbrdrt\brdrs\brdrw8\brdrcf0\clbrdrl\brdrs\brdrw8\brdrcf0\clbrdrb\brdrs\brdrw8\brdrcf0\clbrdrr\brdrs\brdrw8\brdrcf0\clftsWidth1\clwWidth0\cellx2880\row}}  {\fs24\f2 \rtlpar{\lang1037\rtlch }\li0\ri0\sa0\sb0\fi0\qr\sl75\slmult0\par}  }  }";

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get SDT with "RtfText" tag
StructuredDocumentTag sdt = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .Where(tag => tag.Tag == "RtfText").FirstOrDefault();
if (sdt != null)
{
    // Remove placeholder content.
    sdt.RemoveAllChildren();
    if (sdt.Level == MarkupLevel.Block)
        sdt.AppendChild(new Paragraph(doc));
    else if(sdt.Level == MarkupLevel.Inline)
        sdt.AppendChild(new Run(doc));

    // Reset IsShowingPlaceholderText to let MS Word know that SDT shows final content.
    sdt.IsShowingPlaceholderText = false;

    // Move DocumentBuilder cursor to the SDT.
    builder.MoveToStructuredDocumentTag(sdt, 0);
    // Create Document from RTF string and insert it into the target document.
    using (MemoryStream rtfStream = new MemoryStream(Encoding.UTF8.GetBytes(rtfString)))
        builder.InsertDocument(new Document(rtfStream), ImportFormatMode.KeepSourceFormatting);
}

doc.Save(@"C:\Temp\out.docx");

I d not see that some properties were lost. Content formatting inserted into the SDT is the same as in source RTF.