# 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);
        }
    }
}
相关推荐
Java爱好狂.28 分钟前
挑战一周速通Spring+SpringBoot+SpringCloud!
java·后端·spring·springboot·架构师·java面试·java程序员
柠檬味拥抱34 分钟前
基于 YOLO11 的 18 类户外生活垃圾智能检测系统:从模型到 Streamlit 全流程实战
后端
得物技术43 分钟前
动态规则的 Agent 实践:用确定性工程驾驭 LLM 的资产智能验收
人工智能·后端·llm
柠檬味拥抱1 小时前
基于 YOLO11 的低空空域反无人机预警系统:红外热成像 + Streamlit 全流程实战
后端
付威20231 小时前
rpi-acp 源码解读:以 Zed 为例,一个 ACP Client 如何启动并驱动 rpi Agent
后端
我的xiaodoujiao1 小时前
Django 基础知识详细图文教程 15-Django 内置Admin系统
后端·python·django
柠檬味拥抱1 小时前
皮肤病变检测数据集 | 2500张YOLO皮肤科数据集
后端
我的xiaodoujiao1 小时前
Django 基础知识详细图文教程 16-Django 内置Admin认证系统
后端·python·django
程序员鱼皮1 小时前
Personal Agent 是什么?为什么大厂都抢着做?连 DeepSeek Bot 都来了。。
前端·后端·ai编程
geovindu1 小时前
rust: Chain of Responsibility Pattern
开发语言·后端·设计模式·rust·责任链模式·行为模式