如何使用.NetOffice库添加标头

Haz*_*zel 5 c# ms-word netoffice

我正在使用NetOffice创建Word文档.

几乎没有文档,我很难添加标题.有人可以帮忙吗?

Tob*_*bon 4

您必须使用该Word.Section.Headers属性,在下面的示例中,我在页眉上放置了右对齐的图像

    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;
        }
Run Code Online (Sandbox Code Playgroud)

要添加一些文本,请使用:

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

希望这有助于进一步调查。