在word文档中从第n页开始在页脚添加页码

add page number in footer starting from n page in word document

我需要从 word 文档的第 5 页开始添加页码(X 的第 1 页)。怎么做。我添加到整个文档的代码,我无法控制它。 我在 C# 中使用 Word 互操作。 请帮忙。

 oDoc.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;
                        //Object oMissing = System.Reflection.Missing.Value;
                        oDoc.ActiveWindow.Selection.TypeText("\t Page ");
                        Object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;
                        Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
                        oDoc.ActiveWindow.Selection.HeaderFooter.LinkToPrevious = false;
                        oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);
                        oDoc.ActiveWindow.Selection.TypeText(" of ");
                        oDoc.ActiveWindow.Selection.Fields.Add(oDoc.ActiveWindow.Selection.Range, ref TotalPages, ref oMissing, ref oMissing);
                       

为了在文档中(重新)开始编号,需要使用分节符。下面的示例演示了如何在目标页面之前插入一个 "Next Page" 分节符,然后将新部分的页脚格式化为从 1 开始的部分。

注意,我也将分配更改为 TotalPages,假设总页数应该是新部分的页数,而不是整个文档的页数。

        //Go to page where page numbering should start
        string pageNum = "3";
        wdApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToNext, ref missing, pageNum);
        Word.Range rngPageNum = wdApp.Selection.Range;
        //Insert Next Page section break so that numbering can start at 1
        rngPageNum.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage);

        Word.Section currSec = doc.Sections[rngPageNum.Sections[1].Index];
        Word.HeaderFooter ftr = currSec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];

        //So that the footer content doesn't propagate to the previous section    
        ftr.LinkToPrevious = false;
        ftr.PageNumbers.RestartNumberingAtSection = true;
        ftr.PageNumbers.StartingNumber = 1;

        //If the total pages should not be the total in the document, just the section
        //use the field SectionPages instead of NumPages
        object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldSectionPages;
        object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
        Word.Range rngCurrSecFooter = ftr.Range;
        rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref CurrentPage, ref missing, false);
        rngCurrSecFooter.InsertAfter(" of ");
        rngCurrSecFooter.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
        rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref TotalPages, ref missing, false);