# 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);
        }
    }
}
相关推荐
2601_962055976 小时前
跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围
spring boot·后端·tomcat
Lost of 程序猿8 小时前
ASP.NET Core API 幂等性设计深度实战:从一次重复申领事故说起
后端·asp.net
QQ_21696290969 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端
郑州光合科技余经理10 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
IT_陈寒10 小时前
Vue的嵌套组件竟然吃掉了我的事件?
前端·人工智能·后端
大辉狼_音频架构11 小时前
进阶:从源码编译 SOF 固件与 topology
后端
大辉狼_音频架构11 小时前
上板:让 SOF 在 FRDM-i.MX8MP 上跑起来
后端
大辉狼_音频架构12 小时前
FRDM-IMX8MP UUU 烧录 eMMC 指南
后端
运行时异常12 小时前
【WMS 仓储系统集成 AI Agent 实战】第 3 讲:Spring Security 6 + JWT——addFilterBefore 一词之差,全站 401
java·后端
LinMINGJing00713 小时前
PageHeaderData:Page 的页面头
后端