当前位置: 移动技术网 > IT编程>开发语言>.net > C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉页脚

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

2019年02月28日  | 移动技术网IT编程  | 我要评论

全蝎蛇蚁胶囊,vps 推荐,宅门劫

前言

本文是对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");
        }
    }
}

删除效果:

 

(本文完)

如需转载,请注明出处!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网