翻譯|使用教程|編輯:胡濤|2022-06-24 10:15:59.447|閱讀 397 次
概述:本文討論如何通過(guò)Spire.Doc為不同部分添加頁(yè)碼。歡迎查閱!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
有時(shí),開(kāi)發(fā)人員需要在一個(gè) Word 文檔中為不同的部分添加頁(yè)碼,例如封面、目錄和內(nèi)容位于不同的部分。本文討論如何通過(guò)Spire.Doc為不同部分添加頁(yè)碼。
這里將在 3 個(gè)部分內(nèi)導(dǎo)入一個(gè)測(cè)試文檔,如下圖所示。
以下是詳細(xì)步驟:
第 1 步:創(chuàng)建一個(gè)新文檔并加載測(cè)試字文件。
Document document = new Document("test.docx");
第 2 步:為第一部分創(chuàng)建頁(yè)腳并在其中添加頁(yè)碼。
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 步:重新開(kāi)始下一節(jié)的頁(yè)碼,并將起始頁(yè)碼設(shè)置為 1。
document.Sections[1].PageSetup.RestartPageNumbering = true; document.Sections[1].PageSetup.PageStartingNumber = 1;
第 4 步:對(duì)其余部分重復(fù)第 2 步和第 3 步,因此使用 for 循環(huán)更改代碼。
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");
結(jié)果截圖:
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn