C# 获得word中某一段落所在页的页码

方式一:通过openxml 从xml结构里获得
不可行。原因如下
A footer is not on a page and a page number in a footer is a field that potentially identifies with multiple pages. In a Word document file, there are no pages. Pages are set as a part of the printing process.
It is possible with the file open in Word to identify a page number using vba but it is not part of the xml.
翻译一下,页脚并不存在于页面上,页脚上的页码是一个有可能经过多个页才能确定的域。在一个word文档中,并没有页,页被认为是一个word页面绘制过程。
使用vba用word打开文档可以确定页码,但页码并不存在于xml上。

下面这种情况可以从xml结构中获得一部分页码。即word中存在目录。但只能获得大纲标题所在页。并不能任意指定段落。而且并不保证准确,因为默认情况下目录并不自动更新。
虽然页码在xml结构中不存在,但是目录上大纲标题对应的页是硬编码在xml结构里的。

方式二:通过vba --因为vba需要打开word,所以是可以获得页码的

/// <summary>
        /// 得到Paragraph所在页码
        /// </summary>
        /// <param name="pa"></param>
        /// <returns></returns>
        public static int GetPageNumberOfParagraph(Paragraph pa)
        {
            return GetPageNumberOfRange(pa.Range);
        }

        private static int GetPageNumberOfRange(Range range)
        {
            return (int)range.get_Information(WdInformation.wdActiveEndPageNumber);
        }
原文地址:https://www.cnblogs.com/xushy/p/8650186.html