Word控件Spire.Doc 【页眉页脚】教程(7): 添加不同的首页页眉和页脚

Spire.DOC是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

在MS Word页眉&页脚工具选项中,我们可以选择“不同的首页”和“不同的奇数和偶数页”。文章“如何为奇数和偶数页创建不同的页眉/页脚”介绍了使用Spire.Doc设置不同奇数和偶数页的方法。Spire.DOC还提供了一个简单快捷的方法来添加不同的首页页眉&页脚。本文将介绍添加不同首页页眉页脚的方法&。

仅供参考,如果您只需要第一页的页眉和页脚,请只设置第一页的页眉&页脚,其余的就不用管了。通过这种方式,您的Word文档将只有页眉&页脚在第一页,这提供了一个更简单的方法来添加一个页眉只到第一页的文件比文章中提到的方法“如何添加一个页眉只到第一页的文件“。

注意 :在开始之前,请下载Spire.Doc的最新版本,并在bin文件夹中添加Spire.Doc .dll作为Visual Studio的引用。

步骤1:加载仅包含文本的示例文档。

Document document = new Document();
document.LoadFromFile("T.docx");

步骤2:获取节并设置属性true。

Section section = document.Sections[0];
section.PageSetup.DifferentFirstPageHeaderFooter = true;

步骤3:设置第一个页眉。这里我们附加一张图片作为标题。

Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.bmp"));

步骤4:设置第一个页脚。

paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;

步骤5:设置其他页眉&页脚。如果你只需要第一页的页眉&页脚,不要设置这个。

Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange FF = paragraph2.AppendText("First Page Footer");
FF.CharacterFormat.FontSize = 20;

第6步:保存文档并启动以查看效果。

document.SaveToFile("R.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("R.docx");

影响 :

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Mirror_Margin
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("T.docx");

Section section = document.Sections[0];
section.PageSetup.DifferentFirstPageHeaderFooter = true;

Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;
DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.bmp"));

Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange FF = paragraph2.AppendText("First Page Footer");
FF.CharacterFormat.FontSize = 20;

Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NH = paragraph3.AppendText("If you only need first page header, don't set this.");
NH.CharacterFormat.FontSize = 20;

Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NF = paragraph4.AppendText("If you only need first page footer, don't set this.");
NF.CharacterFormat.FontSize = 20;

document.SaveToFile("R.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("R.docx");
}
}
}

以上便如何添加不同的首页页眉和页脚,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/129831908