# C#:删除 Word 中的页眉或页脚

C#:删除 Word 中的页眉或页脚

在处理Word文档批量操作时,我们经常需要清除页眉页脚------比如合并文档后去除冗余信息,或为标准化报告格式。手动操作不仅繁琐,更难以集成到自动化流程中。使用Spire.Doc,只需几行C#代码就能精准删除所有或指定页面的页眉页脚,轻松实现文档规范化处理。


一、环境配置要点

通过NuGet快速安装组件:

复制代码
  Install-Package Spire.Doc -Version 10.8.9
功能 免费版 商业版
页面限制 ≤500页 无限制
水印 强制保留 支持去除
页眉/页脚删除 ✔️ ✔️

注意:本文代码在免费版环境下验证通过


二、删除 Word 中的页脚

csharp 复制代码
 using Spire.Doc;
using Spire.Doc.Documents;

namespace RemoveHeader
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("HeaderFooter.docx");

            //Get the first section
            Section section = doc.Sections[0];

            //Iterate through all paragraphs in the section
            foreach (Paragraph para in section.Paragraphs)
            {
                //Iterate through all child objects in each paragraph
                foreach (DocumentObject obj in para.ChildObjects)
                {
                    //Delete footer in the first page
                    HeaderFooter footer;
                    footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage];
                    if (footer != null)
                        footer.ChildObjects.Clear();

                    //Delete footer in the odd page
                    footer = section.HeadersFooters[HeaderFooterType.FooterOdd];
                    if (footer != null)
                        footer.ChildObjects.Clear();

                    //Delete footer in the even page
                    footer = section.HeadersFooters[HeaderFooterType.FooterEven];
                    if (footer != null)
                        footer.ChildObjects.Clear();
                }
            }

            //Save the result document
            doc.SaveToFile("RemoveFooter.docx", FileFormat.Docx);
        }
    }
}

三、删除 Word 中的页眉

csharp 复制代码
 using Spire.Doc;
using Spire.Doc.Documents;

namespace RemoveHeader
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("HeaderFooter.docx");

            //Get the first section
            Section section = doc.Sections[0];

            //Iterate through all paragraphs in the section
            foreach (Paragraph para in section.Paragraphs)
            {
                //Iterate through all child objects in each paragraph
                foreach (DocumentObject obj in para.ChildObjects)
                {
                    //Delete header in the first page
                    HeaderFooter header;
                    header = section.HeadersFooters[HeaderFooterType.HeaderFirstPage];
                    if (header != null)
                        header.ChildObjects.Clear();

                    //Delete headers in the odd pages
                    header = section.HeadersFooters[HeaderFooterType.HeaderOdd];
                    if (header != null)
                        header.ChildObjects.Clear();

                    //Delete headers in the even pages
                    header = section.HeadersFooters[HeaderFooterType.HeaderEven];
                    if (header != null)
                        header.ChildObjects.Clear();
                }
            }

            //Save the result document
            doc.SaveToFile("RemoveHeader.docx", FileFormat.Docx);
        }
    }
}
相关推荐
why技术22 分钟前
在我眼里,这就是天才般的算法!
后端·面试
绝无仅有23 分钟前
Jenkins+docker 微服务实现自动化部署安装和部署过程
后端·面试·github
程序视点26 分钟前
Escrcpy 3.0投屏控制软件使用教程:无线/有线连接+虚拟显示功能详解
前端·后端
zhuyasen1 小时前
当Go框架拥有“大脑”,Sponge框架集成AI开发项目,从“手写”到一键“生成”业务逻辑代码
后端·go·ai编程
东皋长歌2 小时前
SpringBoot集成ELK
spring boot·后端·elk
布列瑟农的星空2 小时前
大话设计模式——关注点分离原则下的事件处理
前端·后端·架构
现在就干3 小时前
Spring事务基础:你在入门时踩过的所有坑
java·后端
该用户已不存在3 小时前
Gradle vs. Maven,Java 构建工具该用哪个?
java·后端·maven
JohnYan4 小时前
Bun技术评估 - 23 Glob
javascript·后端·bun
二闹4 小时前
聊天怕被老板发现?摩斯密码来帮你
后端·python