C# 删除Word页眉页脚
1、下载安装后,注意在编辑程序时,添加引用Spire.Doc.dll,dll文件在安装路径下的Bin文件夹中获取。

1、测试文档:

2、1. 删除所有页眉页脚:
using Spire.Doc;
namespace RemoveHeaderFooter_Doc
{
class Program
{
static void Main(string[] args)
{
//创建一个Document实例并加载示例文档
Document doc = new Document();
doc.LoadFromFile("sample.docx");
//获取第一个section
Section section = doc.Sections[0];
//删除页眉
section.HeadersFooters.Header.ChildObjects.Clear();
//删除页脚
section.HeadersFooters.Footer.ChildObjects.Clear();
//保存文档
doc.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");
}
}
}
3、删除效果:

4、2.删除首页页眉页脚
using Spire.Doc;
namespace RemoveHeaderFooter2_Doc
{
class Program
{
static void Main(string[] args)
{
//创建一个Document实例并加载示例文档
Document doc = new Document();
doc.LoadFromFile("sample.docx");
//获取第一个section
Section section = doc.Sections[0];
//设置页眉页脚首页不同
section.PageSetup.DifferentFirstPageHeaderFooter = true;
//删除首页页眉页脚
section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();
//保存文档
doc.SaveToFile("output.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("output.docx");
}
}
}
5、删除效果:
