Insert text at the beginning of Word document

Hello team,

I would like to add text at the beginning of document with left alignment, but when I add it it affects the document’s text too.

My code is:

Document doc = new Document(@"C:\...\NewFolder\sample.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentStart();

Font font = builder.Font;
font.Size = 12;
font.Bold = false;
font.Name = "Calibri";

ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.Alignment = ParagraphAlignment.Left;
paragraphFormat.KeepTogether = false;
paragraphFormat.KeepWithNext = false;

builder.InsertParagraph();
builder.Write("Sample Text");
builder.InsertBreak(BreakType.LineBreak);

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

The input document is:
sample.docx (15.7 KB)

@maant Please modify your code like this:

Document doc = new Document(@"C:\Temp\sample.docx");

// Add an empty paragraph at the beginning of the document.
doc.FirstSection.Body.PrependChild(new Paragraph(doc));

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentStart();
            
builder.Font.Size = 12;
builder.Font.Bold = false;
builder.Font.Name = "Calibri";

ParagraphFormat paragraphFormat = builder.ParagraphFormat;
paragraphFormat.Alignment = ParagraphAlignment.Left;
paragraphFormat.KeepTogether = false;
paragraphFormat.KeepWithNext = false;

builder.Write("Sample Text");

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