上一篇 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); }
第二部分,頁腳表格及奇偶欄內容不同設置(你也可以同樣來處理頁眉):

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); //移去尾部多余的段落 }
這樣可以處理一些需要在頁眉頁腳進行特殊欄目的設置。
好了,用開源組件DocX來處理Word文檔的基本操作就介紹完了,謝謝你能來閱讀,並希望能對你有些許幫助,就是我的最大安慰了。