`

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

 删除效果:



 

(本文完)

  • 大小: 9.4 KB
  • 大小: 84.4 KB
  • 大小: 101.5 KB
  • 大小: 97.2 KB
  • 大小: 146 KB
  • 大小: 54.8 KB
  • 大小: 128.5 KB
  • 大小: 88 KB
  • 大小: 132.4 KB
0
0
分享到:
评论

相关推荐

    使用C#实现在word中插入页眉页脚的方法

    主要介绍了使用C#实现在word中插入页眉页脚的方法,是操作Word的常见方法,有一定的学习借鉴价值,需要的朋友可以参考下

    批量添加 Word 文档页眉页脚.

    批量添加 Word 文档页眉页脚的小工具.

    ITextSharp5.0生成PDF(含页眉页脚的生成)

    ITextSharp5.0生成PDF(含页眉页脚的生成)

    word页眉页脚修改器V1.0

    同时有个小问题没能解决,无法删除原有的页眉页脚: this.m_WordApp.ActiveWindow.ActivePane.Selection.Text = null; 该语句根本不起作用,改成其它的也不行,word开发文档实在太少了,微软也说的不表楚,盼大侠...

    不打开OFFICE,插入页眉页脚

    OPEN XML SDK2.0 技术开发,不启动进程WINWORD.EXE进行操作OFFICE

    Html to word by open xml 基于Open Xml 实现的带页眉页脚页码的 Html 转Word 功能

    基于Open Xml 实现的带页眉页脚页码的 Html 转Word 功能 跟 Microsoft.Office.Interop.Word 的 Com 组件相比优势如下: 1、服务器不用安装Office 即可生成Word 格式的文件; 2、服务器不用配置 繁琐的Com组件的权限...

    iText7 html转换为pdf生成页码、页眉、页脚DEMO

    itext7 html转换为pdf;iText7页码、页眉、页脚,itext 的复杂表格实现;完整springboot项目代码

    批量添加 Word 文档页眉页脚源--源代码

    批量添加 Word 文档页眉页脚的源代码。开发环境:Visula Studio 2005 + O2003PIA.MSI(Office 2003 库,压缩包中已经附带)。

    DEMO(word excel visio 添加页眉页脚和内容)

    本demo用C# 实现了了word excel visio 添加页眉页脚和内容,其中word 添加了页脚,excel添加了首行内容,visio 添加了一个矩形容器 并设置了透明背景,增加了指定字号的文字

    C# 打印网页不显示页眉页脚的实现方法

    1.在IE浏览器点“打印”—“页面设置”,IE的默认设置如下图 ... 3.C#代码实现 ...5.法二,使用JS修改注册表,但是失败,有... 您可能感兴趣的文章:C# 添加PDF页眉/页脚的示例代码使用C#实现在word中插入页眉页脚的方法

    itext生成PDF设置页眉页脚的实例详解

    主要介绍了itext生成PDF设置页眉页脚的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

    C# 操作Word

    非常好用的一个C#操作word的类库。能在word文档中任意位子写文字以及段落和表格(带线和不带线以及虚线)、图片、页眉页脚。word能操作的,这个类库基本都可以完成。下载直接使用。

    c#操作word文档+项目

    1. 文件操作 2 1.1 Word初始 2 1.2 新建 2 1.3 打开 2 1.4 保存 2 1.5 不保存 2 1.6 关闭组件 2 2. 文档操作 3 2.1 文本范围 3 2.2 复制粘贴 3 2.3 字体 3 ...6. 页眉页脚 7 7. 页面设置 8 8. 目录 8

    解决Word页码设置问题.

    在专业出版的书籍中,常常看到书籍中奇偶页的页眉会显示不同的内容,以方便用户在书籍中快速查找资料。而在Word 2000中,用户也可以很方便地在文档奇偶页的页眉中显示不同的内容。

    可以在页眉中插入图片的NPOI

    该版本NPOI是可以在页眉中插入图片。但是是inline方式,能实现插入,但是离预期效果有点记录,可以使用

    UEditor富文本编辑器+内容转word导出整合经验分享(SpringMVC+Ueditor+mybatis+Maven)

    UEditor 是由百度「FEX前端研发团队」开发的所见即所得富文本web编辑...笔者使用Springmvc+mybatis+maven搭建,并实现插入图片、文字、表格转word,rar包中有具体的代码。 运行需将jacob下的dll拷贝至jdk/jre/bin目录下

    C#进行操作Word操作

    C#操作word文档。包括创建、保存、文字、图片、表格、页眉、页脚等。其中表格还包括边框,合并单元格,插入图片。其他操作必须先创建word。代码块都有,具体应用需要修改。

    用子窗体实现通用的窗体页眉与页脚

    大部份窗体的页眉和页脚都大同小异,所以可以通过子窗体的方式来实现能用的窗体页眉和页脚,只需创建一个通用的窗体页眉和页脚窗体,然后在每个窗体的页眉和页脚处都通过子窗体的方式来引用对应的页眉和页脚窗体,...

Global site tag (gtag.js) - Google Analytics