C# 獲得word中某一段落所在頁的頁碼
阿新 • • 發佈:2018-03-26
打開文檔 gpo span 多個 proc hat 獲得 ber get
方式一:通過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); }
C# 獲得word中某一段落所在頁的頁碼