翻譯|使用教程|編輯:胡濤|2022-06-24 10:15:59.447|閱讀 397 次
概述:本文討論如何通過Spire.Doc為不同部分添加頁碼。歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
有時,開發人員需要在一個 Word 文檔中為不同的部分添加頁碼,例如封面、目錄和內容位于不同的部分。本文討論如何通過Spire.Doc為不同部分添加頁碼。
這里將在 3 個部分內導入一個測試文檔,如下圖所示。
以下是詳細步驟:
第 1 步:創建一個新文檔并加載測試字文件。
Document document = new Document("test.docx");
第 2 步:為第一部分創建頁腳并在其中添加頁碼。
HeaderFooter footer = document.Sections[0].HeadersFooters.Footer; Paragraph footerParagraph = footer.AddParagraph(); footerParagraph.AppendField("page number", FieldType.FieldPage); footerParagraph.AppendText(" of "); footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages); footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
第 3 步:重新開始下一節的頁碼,并將起始頁碼設置為 1。
document.Sections[1].PageSetup.RestartPageNumbering = true; document.Sections[1].PageSetup.PageStartingNumber = 1;
第 4 步:對其余部分重復第 2 步和第 3 步,因此使用 for 循環更改代碼。
for (int i = 0; i < 3; i++) { HeaderFooter footer = document.Sections[i].HeadersFooters.Footer; Paragraph footerParagraph = footer.AddParagraph(); footerParagraph.AppendField("page number", FieldType.FieldPage); footerParagraph.AppendText(" of "); footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages); footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right; if (i == 2) break; else { document.Sections[i + 1].PageSetup.RestartPageNumbering = true; document.Sections[i + 1].PageSetup.PageStartingNumber = 1; } }
第 5 步:保存并查看。
document.SaveToFile("result.docx", FileFormat.Docx); System.Diagnostics.Process.Start("result.docx");
結果截圖:
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn