Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add header with .NetOffice library

I'm using NetOffice to create Word documents.

There is little documentation and I'm struggling to add a header. Can anybody help?

like image 270
Hazel Avatar asked Oct 04 '22 14:10

Hazel


1 Answers

You have to use the Word.Section.Headers property, in the example below I've put an image right-aligned on the page header

    foreach (Word.Section section in newDocument.Sections)
        {
            string picturePath = @"D:\Desktop\test.png";
            section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddPicture(picturePath);
            section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
        }

To add some text use:

    foreach (Word.Section section in newDocument.Sections)
       section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "TEST";

Hope this helps to investigate further.

like image 176
Tobia Zambon Avatar answered Oct 10 '22 02:10

Tobia Zambon