Need help to insert a string text within RTF and get that RTF data

Hello Team,
Could you help us on how we can add/insert a text as a header to the existing RTF file.

@balajisan21 You can use code like the following to insert text into the header of existing document:

Document doc = new Document(@"C:\Temp\in.rtf");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("This is text in document header.");
doc.Save(@"C:\Temp\out.rtf");

Please see our documentation for more information.

Thanks Alexey for the super quick response.
Also ,I have a string data something like Name: Ryan; Age: 28; Address: Mexico
How do I display them in the below format within the header section
Name: Ryan;
Age: 28;
Address: Mexico;

@balajisan21 You can use code like this:

Document doc = new Document(@"C:\Temp\in.rtf");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln("Name: Ryan;");
builder.Writeln("Age: 28;");
builder.Write("Address: Mexico;");
doc.Save(@"C:\Temp\out.rtf");

Please see the documentation to learn more about DocumentBuilder.

Thanks Alexey,
Instead of saving to an rtf document, Could you let me know how do I get/read the doc information in a string format?

@balajisan21 If you need to extract only text content of the document, you can either save document as TXT document or use Document.ToString method and pass SaveFormat.Text as a parameter.

Thanks Alexey,
The below line of code inserts header only on the first page of document, Could you please let us know if it’s possible to have these headers in all the pages? Since I read that the below line sets the header for all pages but when viewed in pdf, this is setting the header only for 1st page.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);`

@balajisan21 In MS Word each section can have different headers and footers, also there are three types of headers/footers: First Page, Primary and Even pages.
Most likely in your document there are several sections and sections following the the first section have their own header, that is why the header from the first section is not inherited.
Could you please attach your document here for review? I will take a look and provide you more information.

Hi! Alexey,
When we use the below lines of code, we are actually trying to insert the header data to existing RTF file and save and read the same as a rtf back again, Currently, it’s getting saved as text instead of RTF such that the existing styles of RTF file are lost. Can you let us know how we can add the headers to the existing RTF file and save and read them in an RTF format without losing any information.

DocumentBuilder builder = new DocumentBuilder(RTF);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Writeln("Name: Ryan;");
doc.Save(rtfStream, saveOptions); //-- tried by updating saveoptions to rtf as well as text but still it is saving as text
doc.Save(@"C:\Temp\out.rtf");

@balajisan21 You can use code like the following to convert the document to RTF string:

private static string DocumentToRtfString(Document doc)
{
    using (MemoryStream rtfStream = new MemoryStream())
    {
        doc.Save(rtfStream, SaveFormat.Rtf);
        return Encoding.UTF8.GetString(rtfStream.ToArray());
    }
}