微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

前言

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

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

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

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

 

C#代码示例(供参考)

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

using System;
 System.Collections.Generic;
 System.Linq;
 System.Text;
 Spire.Doc;
 Spire.Doc.Documents;
 Spire.Doc.Fields;
 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 = ;
            P1.Format.HorizontalAlignment = HorizontalAlignment.Right;
            Paragraph P2 = section.HeadersFooters.OddFooter.AddParagraph();
            TextRange OF = P2.AppendText(奇数页页脚);
            P2.Format.HorizontalAlignment = HorizontalAlignment.Left ;
            OF.CharacterFormat.FontName = ;
            OF.CharacterFormat.FontSize = ;
            OF.CharacterFormat.Bold = ;
            OF.CharacterFormat.TextColor = Color.Blue;

            设置奇偶数页的页眉
            Paragraph P3 = section.HeadersFooters.OddHeader.AddParagraph();
            TextRange OH = P3.AppendText(奇数页页眉);
            P3.Format.HorizontalAlignment = HorizontalAlignment.Left;
            OH.CharacterFormat.FontName = ;
            OH.CharacterFormat.FontSize = ;
            OH.CharacterFormat.Bold = ;
            OH.CharacterFormat.TextColor = Color.Blue;
            Paragraph P4 = section.HeadersFooters.EvenHeader.AddParagraph();
            TextRange EH = P4.AppendText(偶数页页眉);
            P4.Format.HorizontalAlignment = HorizontalAlignment.Right;
            EH.CharacterFormat.FontName = ;
            EH.CharacterFormat.FontSize = ;
            EH.CharacterFormat.Bold = ;
            EH.CharacterFormat.TextColor = Color.Green;

            保存文档
            document.SavetoFile(result.docx,FileFormat.Docx2010);
            System.Diagnostics.Process.Start();
        }
    }
}

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

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

创建Document类的对象,并加载测试文档
            Document document = 获取指定节,并设置页眉页脚首页不同属性为true
            Section section = document.Sections[];
            section.PageSetup.DifferentFirstPageheaderfooter = 加载图片添加首页页眉
            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 = 添加页眉页脚到其他页面
            Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
            paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange NH = paragraph3.AppendText(首页页眉);
            NH.CharacterFormat.FontSize = ;
            Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
            paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange NF = paragraph4.AppendText(首页页脚);
            NF.CharacterFormat.FontSize = );
        }
    }
}

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

 

 

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

 DifferentPageNumber_Doc
{
    创建Document对象,并加载测试文档
            Document doc =  Document();
            doc.LoadFromFile(实例化headerfooter对象(指定页码添加位置:页眉或页脚)
            headerfooter footer = doc.Sections[].HeadersFooters.Footer;
            添加段落到页脚
            Paragraph footerParagraph = footer.AddParagraph();
            添加页码域到页脚
            footerParagraph.AppendField(page number设置页码右对齐
            footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

            创建段落样式,包括字体名称、大小、颜色
            ParagraphStyle style =  ParagraphStyle(doc);
            style.CharacterFormat.Font = new Font(黑体",10aracterFormat.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 = ;
            doc.Sections[1].PageSetup.PageStartingNumber = 1;此处可任意指定起始页码保存文档
            doc.SavetoFile(output.docx);
        }
    }
}

页码添加效果

 

【示例4】复制页眉页脚

 Spire.Doc;

 copyHeaderAndFooter_Doc
{
    新建Word文档1,并加载带页眉的源文档
            Document doc1 =  Document();
            doc1.LoadFromFile(test1.docx获取文档1的页眉
            headerfooter Header = doc1.Sections[].HeadersFooters.Header;

            新建文档2,并加载目标文档
            Document doc2 = new Document(test2.docx遍历文档2中的所有Section
            foreach (Section section in doc2.Sections)
            {
                foreach (DocumentObject obj  Header.Childobjects)
                {
                    将复制的页眉对象添加到section
                    section.HeadersFooters.Header.Childobjects.Add(obj.Clone());
                }
            }

            保存并打开文档
            doc2.SavetoFile(copyHeader.docxstem.Diagnostics.Process.Start();
        }
    }
}

测试文档:

 

测试结果:

 

【示例5】锁定页眉页脚

 Protectheaderfooter_Doc
{
    加载测试文档
            Document doc = sample.docx获取一个section
            Section section = doc.Sections[];

            保护文档并设置 ProtectionType 为 AllowOnlyFormFields,并设置启用编辑的密码
            doc.Protect(ProtectionType.AllowOnlyFormFields,1)">123设置ProtectForm 为false 允许编辑其他区域
            section.ProtectForm = false);
        }
    }
}

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

 

【示例6】删除页眉页脚

1.删除所有页面的页眉页脚

 Removeheaderfooter_Doc
{
    创建一个Document实例并加载示例文档
            Document doc = );
            删除页眉 
            section.HeadersFooters.Header.Childobjects.Clear();

            删除页脚
            section.HeadersFooters.Footer.Childobjects.Clear();

            保存文档 
            doc.SavetoFile(stem.Diagnostics.Process.Start();
        }
    }
}

删除效果

 

2.删除首页的页眉页脚(适用于文档封面,不需要页眉页脚的情况,或者其他情形)

 Removeheaderfooter2_Doc
{
    设置页眉页脚首页不同
            section.PageSetup.DifferentFirstPageheaderfooter = 删除首页页眉页脚
            section.HeadersFooters.FirstPageHeader.Childobjects.Clear();

            );
        }
    }
}

删除效果

 

@H_502_483@

(本文完)

如需转载,请注明出处!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐