• C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉页脚


    前言

    本文是对Word页眉页脚的操作方法的进一步的阐述。在“C# 添加Word页眉页脚、页码”一文中,介绍了添加简单页眉页脚的方法,该文中的方法可满足于大多数的页眉页脚添加要求,但是对于比较复杂一点的文档,对页眉页脚的添加要求比较严格的,如需要设置奇、偶页的页眉页脚不同、首页页眉页脚不同、设置页码时需要对不同章节的内容设置不同页码、对包含重要信息的页眉页脚需要设置编辑权限、相同性质的文档需要复制指定页眉页脚等等操作,则可以参考本文中的方法。鉴于此,本文就以上操作要求分以下几个示例要点来进行:

    •   设置Word奇偶页页眉页脚不同
    •   设置Word首页页眉页脚不同
    •   不连续设置页码(即对不同章节的内容设置不同页码)
    •   复制页眉页脚
    •   锁定页眉页脚
    •   删除页眉页脚

    使用工具Free Spire.Doc for .NET(社区版)

    :编程时注意在相应程序中添加引用Spire.Doc.dll,dll文件可在安装路径下的Bin文件夹中获取。


    C#代码示例(供参考)

    【示例1】设置Word奇偶页页眉页脚不同

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    using System.Drawing;
    
    namespace HeadersFootersForOddAndEvenPages
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建Document类,并加载测试文档
                Document document = new Document();
                document.LoadFromFile("test.docx");
    
                //获取指定节,并设置页眉页脚奇偶页不同的属性为true
                Section section = document.Sections[0];
                section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;
    
                //设置奇偶数页的页脚
                Paragraph P1 = section.HeadersFooters.EvenFooter.AddParagraph();
                TextRange EF = P1.AppendText("偶数页页脚");
                EF.CharacterFormat.FontName = "Calibri";
                EF.CharacterFormat.FontSize = 12;
                EF.CharacterFormat.TextColor = Color.Green;
                EF.CharacterFormat.Bold = true;
                P1.Format.HorizontalAlignment = HorizontalAlignment.Right;
                Paragraph P2 = section.HeadersFooters.OddFooter.AddParagraph();
                TextRange OF = P2.AppendText("奇数页页脚");
                P2.Format.HorizontalAlignment = HorizontalAlignment.Left ;
                OF.CharacterFormat.FontName = "Calibri";
                OF.CharacterFormat.FontSize = 12;
                OF.CharacterFormat.Bold = true;
                OF.CharacterFormat.TextColor = Color.Blue;
    
                //设置奇偶数页的页眉
                Paragraph P3 = section.HeadersFooters.OddHeader.AddParagraph();
                TextRange OH = P3.AppendText("奇数页页眉");
                P3.Format.HorizontalAlignment = HorizontalAlignment.Left;
                OH.CharacterFormat.FontName = "Calibri";
                OH.CharacterFormat.FontSize = 12;
                OH.CharacterFormat.Bold = true;
                OH.CharacterFormat.TextColor = Color.Blue;
                Paragraph P4 = section.HeadersFooters.EvenHeader.AddParagraph();
                TextRange EH = P4.AppendText("偶数页页眉");
                P4.Format.HorizontalAlignment = HorizontalAlignment.Right;
                EH.CharacterFormat.FontName = "Calibri";
                EH.CharacterFormat.FontSize = 12;
                EH.CharacterFormat.Bold = true;
                EH.CharacterFormat.TextColor = Color.Green;
    
                //保存文档
                document.SaveToFile("result.docx", FileFormat.Docx2010);
                System.Diagnostics.Process.Start("result.docx");
            }
        }
    }

    奇偶页页眉页脚不同设置效果:

    【示例2】设置Word首页页眉页脚不同

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    using System.Drawing;
    
    namespace HeadersFootersForOddAndEvenPages
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建Document类的对象,并加载测试文档
                Document document = new Document();
                document.LoadFromFile("test.docx");
    
                //获取指定节,并设置页眉页脚首页不同属性为true
                Section section = document.Sections[0];
                section.PageSetup.DifferentFirstPageHeaderFooter = true;
    
                //加载图片添加到首页页眉
                Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
                paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left;
                DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.png"));
                //添加文字到首页页脚
                Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
                paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
                TextRange FF = paragraph2.AppendText("首页页脚");
                FF.CharacterFormat.FontSize = 12;
    
                //添加页眉页脚到其他页面
                Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
                paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
                TextRange NH = paragraph3.AppendText("非首页页眉");
                NH.CharacterFormat.FontSize = 12;
                Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
                paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
                TextRange NF = paragraph4.AppendText("非首页页脚");
                NF.CharacterFormat.FontSize = 12;
    
                //保存文档
                document.SaveToFile("result.docx", FileFormat.Docx2010);
                System.Diagnostics.Process.Start("result.docx");
            }
        }
    }

    首页页眉页脚不同设置效果:

     

    【示例3】不连续设置页码

    using Spire.Doc;
    using Spire.Doc.Documents;
    using System.Drawing;
    
    namespace DifferentPageNumber_Doc
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建Document对象,并加载测试文档
                Document doc = new Document();
                doc.LoadFromFile("test.docx");
    
                //实例化HeaderFooter对象(指定页码添加位置:页眉或页脚)
                HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
                //添加段落到页脚
                Paragraph footerParagraph = footer.AddParagraph();
                //添加页码域到页脚
                footerParagraph.AppendField("page number", FieldType.FieldPage);
                //设置页码右对齐
                footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
    
                //创建段落样式,包括字体名称、大小、颜色
                ParagraphStyle style = new ParagraphStyle(doc);
                style.CharacterFormat.Font = new Font("黑体", 10, FontStyle.Bold);
                style.CharacterFormat.TextColor = Color.Black;
                doc.Styles.Add(style);
                //应用段落样式到页脚
                footerParagraph.ApplyStyle(style.Name);
    
                //将第一节的页码样式设置为罗马数字
                doc.Sections[0].PageSetup.PageNumberStyle = PageNumberStyle.RomanLower;
    
                //将第二节的页码样式设置为阿拉伯数字,并重新开始编码
                doc.Sections[1].PageSetup.PageNumberStyle = PageNumberStyle.Arabic;
                doc.Sections[1].PageSetup.RestartPageNumbering = true;
                doc.Sections[1].PageSetup.PageStartingNumber = 1;//此处可任意指定起始页码数
    
                //保存文档
                doc.SaveToFile("output.docx", FileFormat.Docx2010);
                System.Diagnostics.Process.Start("output.docx");
            }
        }
    }

    页码添加效果:

     

    【示例4】复制页眉页脚

    using Spire.Doc;
    
    namespace CopyHeaderAndFooter_Doc
    {
        class Program
        {
            static void Main(string[] args)
            {
                //新建Word文档1,并加载带页眉的源文档
                Document doc1 = new Document();
                doc1.LoadFromFile("test1.docx");
    
                //获取文档1的页眉
                HeaderFooter Header = doc1.Sections[0].HeadersFooters.Header;
    
                //新建文档2,并加载目标文档
                Document doc2 = new Document("test2.docx");
    
                //遍历文档2中的所有Section
                foreach (Section section in doc2.Sections)
                {
                    foreach (DocumentObject obj in Header.ChildObjects)
                    {
                        //将复制的页眉对象添加到section
                        section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());
                    }
                }
    
                //保存并打开文档
                doc2.SaveToFile("copyHeader.docx", FileFormat.Docx2013);
                System.Diagnostics.Process.Start("copyHeader.docx");
            }
        }
    }

    测试文档:

     

    测试结果:

     

    【示例5】锁定页眉页脚

    using Spire.Doc;
    
    namespace ProtectHeaderFooter_Doc
    {
        class Program
        {
            static void Main(string[] args)
            {
                //加载测试文档
                Document doc = new Document();
                doc.LoadFromFile("sample.docx");
    
                //获取第一个section
                Section section = doc.Sections[0];
    
                //保护文档并设置 ProtectionType 为 AllowOnlyFormFields,并设置启用编辑的密码
                doc.Protect(ProtectionType.AllowOnlyFormFields, "123");
    
                //设置ProtectForm 为false 允许编辑其他区域
                section.ProtectForm = false;
    
                //保存文档
                doc.SaveToFile("result.docx", FileFormat.Docx2013);
                System.Diagnostics.Process.Start("result.docx");
            }
        }
    }

    运行程序生成的文档中,页眉将不允许被编辑,正确输入密码后,方可编辑页眉。

     

    【示例6】删除页眉页脚

    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");
            }
        }
    }

    删除效果:

     

    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");
            }
        }
    }

    删除效果:

     

    (本文完)

    如需转载,请注明出处!

  • 相关阅读:
    理解全虚拟、半虚拟以及硬件辅助的虚拟化
    使用PowerShell向SharePoint中写入数据
    Python的时间模块小结(转自:不懂真人)
    Windows下如何安装Python的第三方库
    Python的正则表达式笔记
    PG, Pool之间的一些数量关系
    Ceph与OpenStack的Nova相结合
    rbd命令
    rados命令
    ceph命令
  • 原文地址:https://www.cnblogs.com/Yesi/p/10442140.html
Copyright © 2020-2023  润新知