如何使用.NetOffice库添加标题头

5

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

由于缺乏文档,我无法很好地添加页眉。有人能帮忙吗?


什么是“header”?如果您指的是字体大小/颜色/粗细等问题,您可以通过Word.Application对象的Selection属性进行调整。 - Tobia Zambon
像给文档添加页眉,使其出现在每一页上。 - Hazel
1个回答

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;
        }

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

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

希望这可以帮助进一步调查。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接