← 返回所有博客文章

在前一篇文章《TX TextControl 中通过代码插入页号》中实现了通过代码插入页号的操作,本文将在插入页号的基础上通过代码设置页眉页脚的格式,代码如下:

    private void InsertPageNumber()
    {
        HeaderFooterCollection hfc = textControl1.Sections.GetItem().HeadersAndFooters;
        hfc.Add(HeaderFooterType.Header);
        HeaderFooter h = hfc.GetItem(HeaderFooterType.Header);
        if (h != null)
        {
            h.Selection.Text = "第页、共页";
            h.Selection.Start = 1;
            h.Selection.Length = 0;
            PageNumberField page = new PageNumberField(1, NumberFormat.ArabicNumbers);
            page.Editable = false;
            page.DoubledInputPosition = true;
            h.PageNumberFields.Add(page);
            h.Selection.Start = 4;
            h.Selection.Length = 0;
            PageNumberField totalPages = new PageNumberField();
            totalPages.Editable = false;
            totalPages.DoubledInputPosition = true;
            totalPages.ShowNumberOfPages = true;
            h.PageNumberFields.Add(totalPages);
            h.Selection.Start = 0;
            h.Selection.Length = 5;
            h.Selection.FontName = "微软雅黑";
            h.Selection.FontSize = 300;
        }
    }

 

运行截图:

image