C#开源组件DocX处理Word文档基本操作(二)

上一篇 C#开源组件DocX处理Word文档基本操作(一) 介绍了DocX的段落、表格及图片的处理,本篇介绍页眉页脚的处理。

示例代码所用DocX版本为:1.3.0.0。关于版本的区别,请参见上篇,而对于版本不同的起因,请参见 开源组件DocX版本区别点滴 一文。

代码如下:

第一部分:基本的页眉页脚处理(包括图片插入)

private void DocXSetHeaderFooter(DocX document)
        {
            document.AddHeaders();  //增加页眉
            document.AddFooters();  //增加页脚

            document.DifferentFirstPage = true;         // 首页有不同的页眉页脚
            document.DifferentOddAndEvenPages = true;   // 奇偶页有不同的页眉页脚
            Header header_first = document.Headers.First;   //首页页眉
            Header header_odd = document.Headers.Odd;       //奇数页眉
            Header header_even = document.Headers.Even;     //偶数页眉

            Footer footer_first = document.Footers.First;   //首页页脚
            Footer footer_odd = document.Footers.Odd;       //奇数页脚
            Footer footer_even = document.Footers.Even;     //偶数页脚
            var HFFont = "微软雅黑";

            //首页页眉 header_first
            Paragraph pHeaderFirst = header_first.Paragraphs.Count > 0 ? header_first.Paragraphs.First() : header_first.InsertParagraph();
            pHeaderFirst.AppendPicture(document.AddImage(@"_Word.jpg").CreatePicture());
            pHeaderFirst.Append("首页页眉").Font(HFFont).FontSize(11).Bold().Alignment = Alignment.center;

            //奇数页眉
            Paragraph pHeaderOdd = header_odd.Paragraphs.Count > 0 ? header_odd.Paragraphs.First() : header_odd.InsertParagraph();
            pHeaderOdd.Append("奇数页页眉").Font(HFFont).FontSize(10).Alignment = Alignment.center;
            //偶数页眉
            Paragraph pHeaderEven = header_even.Paragraphs.Count > 0 ? header_even.Paragraphs.First() : header_even.InsertParagraph();
            pHeaderEven.Append("偶数页页眉").Font(HFFont).FontSize(10).Alignment = Alignment.center;

            //首页页脚
            Paragraph pFooterFirst = footer_first.Paragraphs.Count > 0 ? footer_first.Paragraphs.First() : footer_first.InsertParagraph();
            pFooterFirst.Append("第页 共页").Font("微软雅黑").FontSize(10);
            pFooterFirst.InsertPageNumber(PageNumberFormat.normal, 1);
            pFooterFirst.InsertPageCount(PageNumberFormat.normal, 5);  //normal阿拉伯数字   roman罗马数字
            pFooterFirst.Alignment = Alignment.center;
            
            //奇数页脚
            Paragraph pFooterOdd = footer_odd.Paragraphs.Count > 0 ? footer_odd.Paragraphs.First() : footer_odd.InsertParagraph();
            //现在可以同上面一样处理基本的设置,下面是页眉插入表格及奇偶页的不同设置
            DocXSetFooter(pFooterOdd.FontSize(10), HFFont);

            //偶数页脚
            Paragraph pFooterEven = footer_even.Paragraphs.Count > 0 ? footer_even.Paragraphs.First() : footer_even.InsertParagraph();
            //现在可以同上面一样处理基本的设置,下面是页眉插入表格及奇偶页的不同设置
            DocXSetFooter(pFooterEven.FontSize(10), HFFont, false);
        }
View Code

 第二部分,页脚表格及奇偶栏内容不同设置(你也可以同样来处理页眉):

private void DocXSetFooter(Paragraph pFooter, string hfFont = null, bool IsOdd = true)
        {
            Table tbl = pFooter.InsertTableBeforeSelf(1, 3);
            tbl.Design = TableDesign.None; //TableDesign.TableGrid; //
            //tbl.AutoFit =  AutoFit.Contents;//内容   //AutoFit.ColumnWidth;//字段宽度    //AutoFit.Fixed;//固定
            tbl.SetWidthsPercentage(new float[] { 30f, 40f, 30f }, 1500f);
            //tbl.SetWidths(new float[] { 300f, 600f, 300f });

            if (IsOdd)
            {
                tbl.Rows[0].Cells[0].Paragraphs.First().InsertText(DateTime.Now.ToString("yyyy年MM月dd日"));
                tbl.Rows[0].Cells[1].Paragraphs.First().InsertText("这里加入公司名称或其它信息");
                tbl.Rows[0].Cells[2].Paragraphs.First().InsertText("第页 共页");
                tbl.Rows[0].Cells[2].Paragraphs.First().InsertPageNumber(PageNumberFormat.normal, 1);
                tbl.Rows[0].Cells[2].Paragraphs.First().InsertPageCount(PageNumberFormat.normal, 5);
            }
            else
            {
                tbl.Rows[0].Cells[0].Paragraphs.First().InsertText("第页 共页");
                tbl.Rows[0].Cells[0].Paragraphs.First().InsertPageNumber(PageNumberFormat.normal, 1);
                tbl.Rows[0].Cells[0].Paragraphs.First().InsertPageCount(PageNumberFormat.normal, 5);
                tbl.Rows[0].Cells[1].Paragraphs.First().InsertText("这里加入公司名称或其它信息");
                tbl.Rows[0].Cells[2].Paragraphs.First().InsertText(DateTime.Now.ToString("yyyy年MM月dd日"));
            }
            tbl.Rows[0].Cells[0].Paragraphs.First().Font(hfFont).FontSize(10).Alignment = Alignment.left;
            tbl.Rows[0].Cells[1].Paragraphs.First().Font(hfFont).FontSize(10).Alignment = Alignment.center;
            tbl.Rows[0].Cells[2].Paragraphs.First().Font(hfFont).FontSize(10).Alignment = Alignment.right;

            pFooter.Remove(false);  //移去尾部多余的段落
        }
View Code

这样可以处理一些需要在页眉页脚进行特殊栏目的设置。

好了,用开源组件DocX来处理Word文档的基本操作就介绍完了,谢谢你能来阅读,并希望能对你有些许帮助,就是我的最大安慰了。

原文地址:https://www.cnblogs.com/hnllhq/p/12230436.html