资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#Word文档操作——添加Word页眉、页脚和页码

在Word文档中,我们可以通过添加页眉、页脚的方式来丰富文档内容。添加页眉、页脚时,可以添加时间、日期、文档标题,文档引用信息、页码、内容解释、图片/LOGO等多种图文信息。同时也可根据需要调整文字或图片在页眉、页脚处的位置。因此,本文将介绍如何在C#中使用社区版控件Free Spire. Doc for .NET来添加页眉、页脚以及页码方法。

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都网站制作、海门网络推广、成都微信小程序、海门网络营销、海门企业策划、海门品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供海门建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

提示:下载安装该组件后注意在你的VS项目程序中引用dll文件(该dll文件可在安装文件下的Bin文件夹中获取)

一、添加文本、图片页眉

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;

namespace AddHeaderAndFooter
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Document类实例,添加section和Paragraph
            Document document = new Document(@"C:\Users\Administrator\Desktop\Test.docx");
            Section sec = document.AddSection();
            Paragraph para = sec.AddParagraph();

            //声明一个HeaderFooter类对象,添加页眉、页脚
            HeaderFooter header = sec.HeadersFooters.Header;
            Paragraph headerPara = header.AddParagraph();
            HeaderFooter footer = sec.HeadersFooters.Footer;
            Paragraph footerPara = footer.AddParagraph();           

            //添加图片和文本到页眉,并设置文本格式
            DocPicture headerImage = headerPara.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\2.jpg"));
            TextRange TR = headerPara.AppendText("The Word Trade Organization, WTO");
            TR.CharacterFormat.FontName = "Andalus";
            TR.CharacterFormat.FontSize = 12;
            TR.CharacterFormat.TextColor = Color.Green;
            TR.CharacterFormat.Bold = false;
            headerImage.TextWrappingType = TextWrappingType.Right;

            //添加文本到页脚,并设置格式
            TR = footerPara.AppendText("The World Trade Organization is an intergovernmental organization that regulates international trade.The WTO officially commenced on 1 January 1995 under the Marrakesh Agreement, signed by 123 nations on 15 April 1994, replacing the General Agreement on Tariffs and Trade, which commenced in 1948. ");
            TR.CharacterFormat.Bold = false;
            TR.CharacterFormat.FontSize = 9;           

            //保存文档并运行该文档
            document.SaveToFile("图文页眉.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("图文页眉.docx");
        }
    }
}

运行结果:
C# Word文档操作——添加Word页眉、页脚和页码

PS:对于需要设置图片在文字中的位置的情况,我们可以通过TextWrappingStyle或TextWrappingTpye 来实现。
Eg:

headerImage.TextWrappingStyle = TextWrappingStyle.Through;
或
headerImage.TextWrappingType = TextWrappingType.Right;

二、添加页码

添加页码,我们可以选择在页眉或者页脚处添加。

using Spire.Doc;
using Spire.Doc.Documents;

namespace AddPageNumber_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document类,添加section和Paragraph
            Document document = new Document();
            Section sec = document.AddSection();
            Paragraph para = sec.AddParagraph();

            //添加文本到paragraph,设置BreakType为分页
            para.AppendText("第1页");
            para.AppendBreak(BreakType.PageBreak);
            para.AppendText("第2页");

            //创建一个HeaderFooter类实例,添加页脚
            HeaderFooter footer = sec.HeadersFooters.Footer;
            Paragraph footerPara = footer.AddParagraph();

            //添加字段类型为页码,添加当前页、分隔线以及总页数
            footerPara.AppendField("页码", FieldType.FieldPage);
            footerPara.AppendText(" / ");
            footerPara.AppendField("总页数", FieldType.FieldNumPages);
            footerPara.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //保存文档
            document.SaveToFile("添加页码.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("添加页码.docx");
        }
    }
}

运行结果:
C# Word文档操作——添加Word页眉、页脚和页码

以上是本文关于Word如何添加页眉、页脚和页码的代码操作。如果喜欢,欢迎转载(转载请注明出处)
感谢浏览!


网站名称:C#Word文档操作——添加Word页眉、页脚和页码
链接URL:http://cdkjz.cn/article/jpodep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220