Append Header Text after Existing Header

I’ve been trying to append a header text to the existing header in a word file but the new text is just added as a predecessor. How do I change that so the new text appends the existing header?

image.png (4.9 KB)

This is the code I’m using:

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Font.Bold = isBold;
builder.Font.Italic = false;
builder.Font.Size = Convert.ToDouble(fontSize);
builder.Write("Lft");
doc.Save(Common.FilePath + "CreateHeaderTesting.docx");

Any help is appreciated.

@suyashj12 DocumentBuilder.MoveToHeaderFooter method moves DocumentBuilder's cursor to the beginning of the header or footer. You should add the following line after moving to header/footer to move to it’s end:

builder.MoveTo(builder.CurrentStory.LastParagraph);

Please see the following modified code:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.MoveTo(builder.CurrentStory.LastParagraph);
builder.Font.Bold = true;
builder.Font.Italic = false;
builder.Font.Size = 20;
builder.Write("Lft");
doc.Save(@"C:\Temp\out.docx");
1 Like

Thank you! That worked!

1 Like