在C#中实现Word页眉页脚的所有功能

页眉页脚常用于文章排版,在Word工具栏里,我们可以添加页眉,页脚,页码,日期和时间,图片等信息和内容。页眉/页脚有两个额外选项:首页不同,奇偶页不同。有时在不同的节(section)里插入不同的页眉页脚。从零开始在C#实现这些功能,工作量巨大。所以,今天向大家推荐一款免费的API库,Free Spire.Doc可以从CSDN官网,和 Nuget直接下载。功能强大,容易上手。

这篇文章分为三个部分:
1. 如何在C#里为Word添加页码。
2. 如何在C#里实现Word页眉页脚的图文混排。
3. 如何在C#里实现Word页眉页脚的奇偶页不同和首页不同。

友情提示:Free Spire.Doc 能够独立创建和加载Word文档,这里的微软Word仅用于查看效果。

第一部分:在C#里为Word添加页码

如果Word文档包含许多页,我们可以在页眉页脚处添加页码。该页码可显示总页数,当前页数。添加Free Spire.Doc Bin 文件夹里的.dll至Visual Studio作为引用,我使用了以下代码在C#中了实现对Word页码的添加。

            //Create a Word document and add a section
            Document doc = new Document();
            Section section = doc.AddSection();

            //Initial a HeaderFooter class 
            HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
            Paragraph headerParagraph = header.AddParagraph();

            //Use the AppendField method to get the FieldPage and FieldNumpages
            headerParagraph.AppendField("page number", FieldType.FieldPage);
            headerParagraph.AppendText(" of ");
            headerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
            headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //Save and launch the document
            doc.SaveToFile("Test.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Test.docx");

在页脚处添加页码的方法与上述代码相似,这里我就不再赘述了。有需要的朋友可以参考上面的部分。由于我在文档中没有添加额外的页数,所以截图部分显示的是 1 of 1.

这里写图片描述

第二部分:在C#里实现Word页眉页脚的图文混排

与文本相比,图片更容易吸引人注意。我们经常同时使用文本和图片(即图文混排)来引人注目。在一些正式报告,法律文件中的页眉页脚会使用到图文混排,以达到上述目的。这里我使用了维基百科的标志和关于它的一段介绍来展示在C#里实现页脚部分的图文混排。同时,大家别忘了添加系统引用”System. Drawing”。

            //Create a Word document and initial the footer class
            Document document = new Document();
            Section section = document.AddSection();
            HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;

            //Add text and image to the footer
            Paragraph paragraph = footer.AddParagraph();
            DocPicture footerImage = paragraph.AppendPicture(Image.FromFile("Wiki.bmp"));
            TextRange TR = paragraph.AppendText("Supported and Hosted by the non-profit Wikimedia Foundation.");

            //Format the text and image
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
            TR.CharacterFormat.FontName = "Calibri";
            TR.CharacterFormat.FontSize = 13;
            TR.CharacterFormat.TextColor = Color.Black;
            TR.CharacterFormat.Bold = true;

            // Save the document and launch to see the output
            document.SaveToFile("Test.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Test.docx");

这里写图片描述

值得一提的是,可以使用 ” TextWrappingStyle” 和 ”TextWrappingType”来编辑图片在文本中的位置和自动换行:

            footerImage.TextWrappingStyle = TextWrappingStyle.Through;
            footerImage.TextWrappingType = TextWrappingType.Left;

我尝试过使用free Spire.Doc在页眉页脚中添加表格,竟然可行。有这需求的朋友,也可以测试一下。

第三部分:在C#里实现Word页眉页脚的奇偶页不同和首页不同

Word文件有默认设置每一页的页眉页脚都相同。然而在报告、书籍等排版中往往需要不同的页眉页脚来美化排版。日常编程中,如果我们只需要首页页眉页脚不同,我们可以把首页单独成节。Spire.Doc 提供了更加简便快速的方法来设置页眉页脚首页不同,奇偶页不同。这里,我先以在C#中实现页眉奇偶页不同为例。代码如下:

            //Create a document and section
            Document document = new Document();
            Section section = document.AddSection();

            //Set the bool true and add the Odd Header and Even Header
            section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;
            Paragraph oddHeader = section.HeadersFooters.OddHeader.AddParagraph();
            TextRange oddHT = oddHeader.AppendText("Coding is the art of life");
            Paragraph evenHeader = section.HeadersFooters.EvenHeader.AddParagraph();
            TextRange evenHT = evenHeader.AppendText("Time is the most valuable thing");

            //Format the headers
            oddHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;
            oddHT.CharacterFormat.FontName = "Calibri";
            oddHT.CharacterFormat.FontSize = 20;
            oddHT.CharacterFormat.TextColor = Color.Green;
            oddHT.CharacterFormat.Bold = true;
            evenHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;
            evenHT.CharacterFormat.FontName = "Calibri";
            evenHT.CharacterFormat.FontSize = 20;
            evenHT.CharacterFormat.TextColor = Color.Green;
            evenHT.CharacterFormat.Bold = true;

            //Launch to see effects
            document.SaveToFile("R.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("R.docx");

这里写图片描述

使用该工具,还可以在奇数页,偶数页中设置不同的页码格式,添加不同的图片和表格,并设置不同的格式。实现代码可参照第一部分和第二部分。至于首页不同,代码与奇偶页不同相差无几,此处我仅列举两者代码中不同的部分:

            //set the first page header and footer 
            section.PageSetup.DifferentFirstPageHeaderFooter = true;
            Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
            Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
            //set the rest page header and footer 
            Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
            Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();

如果你只需要首页的页眉页脚,不设置其他页的页眉页脚就可以了。这样就只会有首页的页眉页脚。

结论:
在我看来, free Spire.Doc完美满足了我在C#中为Word添加页眉页脚的需求,是一款值得花时间测试使用的工具,对项目效率有极大的提升。
感谢阅读,欢迎留下宝贵意见。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值