# 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);
        }
    }
}
相关推荐
爆更小哇2 小时前
MyBatis的TypeHandler :优雅地实现数据加密与解密
数据库·后端·mybatis
j***63082 小时前
Springboot项目中线程池使用整理
java·spring boot·后端
w***15312 小时前
Spring boot启动原理及相关组件
数据库·spring boot·后端
a***56062 小时前
Spring Boot接收参数的19种方式
java·spring boot·后端
z***75153 小时前
SpringBoot集成MQTT客户端
java·spring boot·后端
码事漫谈3 小时前
C++语言演进之路:从“C with Classes”到现代编程基石
后端
码事漫谈3 小时前
跨越语言的藩篱:论不同语言间调用的难点与实践
后端
n***84074 小时前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
合作小小程序员小小店4 小时前
web网页开发,在线%图书管理%系统,基于Idea,html,css,jQuery,java,ssm,mysql。
java·前端·后端·mysql·jdk·intellij-idea
IUGEI4 小时前
【MySQL】SQL慢查询如何排查?从慢查询排查到最终优化完整流程
java·数据库·后端·mysql·go