如何查找Word段落的页码?

时间:2011-08-17 18:08:03

标签: c# ms-word

我正在尝试解析Word文档,我要查找的信息应仅位于第一页。有没有办法获取段落的页码?

foreach (Word.Paragraph p in document.Paragraphs)
{
    // pageNo = .....
    // if(pageNo == 1 && p.Range.Text.StartsWith("This")) { 
    //     /* do some processing with the paragraph */ 
    // }
}

2 个答案:

答案 0 :(得分:16)

从这篇文章VSTO 2007: how do I determine the page and paragraph number of a Range?我可以看到你可以从一个范围获得页码

/// <summary>
    /// Determines the pagenumber of a range.
    /// </summary>
    /// <param name="range">The range to be located.</param>
    /// <returns></returns>
    private static int GetPageNumberOfRange(Word.Range range)
    {
        return (int)range.get_Information(Word.WdInformation.wdActiveEndPageNumber);
    }

从这篇文章how to detect Empty paragraph in Word Document using Microsoft.Office.Interop.Word in C#4.0?我相信你可以从段落中找到范围!

for each p in Doc.Content.Paragraphs
    if (p.Range.End - p.Range.Start) > 1 then (The paragraph is empty)
Next

你应该把你的解决方案结合起来,我敢打赌!

答案 1 :(得分:4)

foreach (Word.Paragraph p in document.Paragraphs)
{
   int page = p.Range.Information[Word.WdInformation.wdActiveEndAdjustedPageNumber];
   Console.WriteLine(p.Range.Text + " is on page " + page);
}

这样的东西可能就是你要找的东西。阅读wdActiveEndPageNumber和wdActiveEndAdjustedPageNumber之间的区别,看看哪一个适合您的需要。