c# - 在word文档的页脚中添加从第n页开始的页码

标签 c# ms-word office-interop

我需要添加页码(第 1 页,共 X 页),从 Word 文档中的第 5 页开始。怎么做。我的代码添加到整个文档中,我无法控制它。 我在 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);
                       

最佳答案

为了在文档中(重新)开始编号,需要分节符。下面的示例演示如何在目标页面之前插入“下一页”分节符,然后将新部分的页脚格式化为从该部分的 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);

关于c# - 在word文档的页脚中添加从第n页开始的页码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53093260/

相关文章:

java - 使用 apache poi 更新 Word 2010 中的自定义属性

ms-word - MS Word 是邪恶的!有没有好的替代方案?

.NET MS Interop Word 不在 UTF8 网页中保存文档

outlook - 以编程方式设置 Outlook 邮件项目的类别?

c# - 如何在 LINQ 的表达式树中创建连接?

c# - 使用 ASP.NET Entity Framework 连接到 MySQL 数据库

c# - 为什么这个 View 模型字符串字段被视为必需的?

c# - 如何从文档中的特定位置读取表格?

c# - 在 C# 中为多个 ID 读取多个 Outlook 电子邮件

c# - WPF 静态属性绑定(bind)不适用于 ListView