Chinaunix首页 | 论坛 | 博客
  • 博客访问: 564911
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 664
  • 用 户 组: 普通用户
  • 注册时间: 2017-10-27 10:00
  • 认证徽章:
文章分类

全部博文(70)

文章存档

2019年(16)

2018年(45)

2017年(9)

我的朋友

分类: C#/.net

2019-02-28 11:00:06

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


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


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

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



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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Spire.Doc;
  6. using Spire.Doc.Documents;
  7. using Spire.Doc.Fields;
  8. using System.Drawing;

  9. namespace HeadersFootersForOddAndEvenPages
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             //创建Document类,并加载测试文档
  16.             Document document = new Document();
  17.             document.LoadFromFile("test.docx");

  18.             //获取指定节,并设置页眉页脚奇偶页不同的属性为true
  19.             Section section = document.Sections[0];
  20.             section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;

  21.             //设置奇偶数页的页脚
  22.             Paragraph P1 = section.HeadersFooters.EvenFooter.AddParagraph();
  23.             TextRange EF = P1.AppendText("偶数页页脚");
  24.             EF.CharacterFormat.FontName = "Calibri";
  25.             EF.CharacterFormat.FontSize = 12;
  26.             EF.CharacterFormat.TextColor = Color.Green;
  27.             EF.CharacterFormat.Bold = true;
  28.             P1.Format.HorizontalAlignment = HorizontalAlignment.Right;
  29.             Paragraph P2 = section.HeadersFooters.OddFooter.AddParagraph();
  30.             TextRange OF = P2.AppendText("奇数页页脚");
  31.             P2.Format.HorizontalAlignment = HorizontalAlignment.Left ;
  32.             OF.CharacterFormat.FontName = "Calibri";
  33.             OF.CharacterFormat.FontSize = 12;
  34.             OF.CharacterFormat.Bold = true;
  35.             OF.CharacterFormat.TextColor = Color.Blue;

  36.             //设置奇偶数页的页眉
  37.             Paragraph P3 = section.HeadersFooters.OddHeader.AddParagraph();
  38.             TextRange OH = P3.AppendText("奇数页页眉");
  39.             P3.Format.HorizontalAlignment = HorizontalAlignment.Left;
  40.             OH.CharacterFormat.FontName = "Calibri";
  41.             OH.CharacterFormat.FontSize = 12;
  42.             OH.CharacterFormat.Bold = true;
  43.             OH.CharacterFormat.TextColor = Color.Blue;
  44.             Paragraph P4 = section.HeadersFooters.EvenHeader.AddParagraph();
  45.             TextRange EH = P4.AppendText("偶数页页眉");
  46.             P4.Format.HorizontalAlignment = HorizontalAlignment.Right;
  47.             EH.CharacterFormat.FontName = "Calibri";
  48.             EH.CharacterFormat.FontSize = 12;
  49.             EH.CharacterFormat.Bold = true;
  50.             EH.CharacterFormat.TextColor = Color.Green;

  51.             //保存文档
  52.             document.SaveToFile("result.docx", FileFormat.Docx2010);
  53.             System.Diagnostics.Process.Start("result.docx");
  54.         }
  55.     }
  56. }

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



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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Spire.Doc;
  6. using Spire.Doc.Documents;
  7. using Spire.Doc.Fields;
  8. using System.Drawing;

  9. namespace HeaderFootersDifferentFromFirstPage
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             //创建Document类的对象,并加载测试文档
  16.             Document document = new Document();
  17.             document.LoadFromFile("test.docx");

  18.             //获取指定节,并设置页眉页脚首页不同属性为true
  19.             Section section = document.Sections[0];
  20.             section.PageSetup.DifferentFirstPageHeaderFooter = true;

  21.             //加载图片添加到首页页眉
  22.             Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
  23.             paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left;
  24.             DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.png"));
  25.             //添加文字到首页页脚
  26.             Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
  27.             paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
  28.             TextRange FF = paragraph2.AppendText("首页页眉");
  29.             FF.CharacterFormat.FontSize = 12;

  30.             //添加页眉页脚到其他页面
  31.             Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
  32.             paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
  33.             TextRange NH = paragraph3.AppendText("非首页页眉");
  34.             NH.CharacterFormat.FontSize = 12;
  35.             Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
  36.             paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
  37.             TextRange NF = paragraph4.AppendText("非首页页脚");
  38.             NF.CharacterFormat.FontSize = 12;

  39.             //保存文档
  40.             document.SaveToFile("result.docx", FileFormat.Docx2010);
  41.             System.Diagnostics.Process.Start("result.docx");
  42.         }
  43.     }
  44. }

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



【示例3】不连续设置页码
  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using System.Drawing;

  4. namespace DifferentPageNumber_Doc
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //创建Document对象,并加载测试文档
  11.             Document doc = new Document();
  12.             doc.LoadFromFile("test.docx");

  13.             //实例化HeaderFooter对象(指定页码添加位置:页眉或页脚)
  14.             HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
  15.             //添加段落到页脚
  16.             Paragraph footerParagraph = footer.AddParagraph();
  17.             //添加页码域到页脚
  18.             footerParagraph.AppendField("page number", FieldType.FieldPage);
  19.             //设置页码右对齐
  20.             footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

  21.             //创建段落样式,包括字体名称、大小、颜色
  22.             ParagraphStyle style = new ParagraphStyle(doc);
  23.             style.CharacterFormat.Font = new Font("黑体", 10, FontStyle.Bold);
  24.             style.CharacterFormat.TextColor = Color.Black;
  25.             doc.Styles.Add(style);
  26.             //应用段落样式到页脚
  27.             footerParagraph.ApplyStyle(style.Name);

  28.             //将第一节的页码样式设置为罗马数字
  29.             doc.Sections[0].PageSetup.PageNumberStyle = PageNumberStyle.RomanLower;

  30.             //将第二节的页码样式设置为阿拉伯数字,并重新开始编码
  31.             doc.Sections[1].PageSetup.PageNumberStyle = PageNumberStyle.Arabic;
  32.             doc.Sections[1].PageSetup.RestartPageNumbering = true;
  33.             doc.Sections[1].PageSetup.PageStartingNumber = 1;//此处可任意指定起始页码数

  34.             //保存文档
  35.             doc.SaveToFile("output.docx", FileFormat.Docx2010);
  36.             System.Diagnostics.Process.Start("output.docx");
  37.         }
  38.     }
  39. }

页码添加效果:



【示例4】复制页眉页脚
  1. using Spire.Doc;

  2. namespace CopyHeaderAndFooter_Doc
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //新建Word文档1,并加载带页眉的源文档
  9.             Document doc1 = new Document();
  10.             doc1.LoadFromFile("test1.docx");

  11.             //获取文档1的页眉
  12.             HeaderFooter Header = doc1.Sections[0].HeadersFooters.Header;

  13.             //新建文档2,并加载目标文档
  14.             Document doc2 = new Document("test2.docx");

  15.             //遍历文档2中的所有Section
  16.             foreach (Section section in doc2.Sections)
  17.             {
  18.                 foreach (DocumentObject obj in Header.ChildObjects)
  19.                 {
  20.                     //将复制的页眉对象添加到section
  21.                     section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());
  22.                 }
  23.             }

  24.             //保存并打开文档
  25.             doc2.SaveToFile("copyHeader.docx", FileFormat.Docx2013);
  26.             System.Diagnostics.Process.Start("copyHeader.docx");
  27.         }
  28.     }
  29. }

测试文档:



测试结果:



【示例5】锁定页眉页脚

  1. using Spire.Doc;

  2. namespace ProtectHeaderFooter_Doc
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //加载测试文档
  9.             Document doc = new Document();
  10.             doc.LoadFromFile("sample.docx");

  11.             //获取第一个section
  12.             Section section = doc.Sections[0];

  13.             //保护文档并设置 ProtectionType 为 AllowOnlyFormFields,并设置启用编辑的密码
  14.             doc.Protect(ProtectionType.AllowOnlyFormFields, "123");

  15.             //设置ProtectForm 为false 允许编辑其他区域
  16.             section.ProtectForm = false;

  17.             //保存文档
  18.             doc.SaveToFile("result.docx", FileFormat.Docx2013);
  19.             System.Diagnostics.Process.Start("result.docx");
  20.         }
  21.     }
  22. }

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



【示例6】删除页眉页脚

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

  1. using Spire.Doc;

  2. namespace RemoveHeaderFooter_Doc
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //创建一个Document实例并加载示例文档
  9.             Document doc = new Document();
  10.             doc.LoadFromFile("sample.docx");
  11.             //获取第一个section
  12.             Section section = doc.Sections[0];

  13.             //删除页眉
  14.             section.HeadersFooters.Header.ChildObjects.Clear();

  15.             //删除页脚
  16.             section.HeadersFooters.Footer.ChildObjects.Clear();

  17.             //保存文档
  18.             doc.SaveToFile("result.docx", FileFormat.Docx);
  19.             System.Diagnostics.Process.Start("result.docx");
  20.         }
  21.     }
  22. }

删除效果:



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

  1. using Spire.Doc;

  2. namespace RemoveHeaderFooter2_Doc
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //创建一个Document实例并加载示例文档
  9.             Document doc = new Document();
  10.             doc.LoadFromFile("sample.docx");

  11.             //获取第一个section
  12.             Section section = doc.Sections[0];

  13.             //设置页眉页脚首页不同
  14.             section.PageSetup.DifferentFirstPageHeaderFooter = true;

  15.             //删除首页页眉页脚
  16.             section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();

  17.             //保存文档
  18.             doc.SaveToFile("output.docx", FileFormat.Docx);
  19.             System.Diagnostics.Process.Start("output.docx");
  20.         }
  21.     }
  22. }

删除效果:



(本文完)
如需转载,请注明出处!

阅读(35970) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册