开发者

How do I find the page number for a Word Paragraph?

开发者 https://www.devze.com 2023-03-28 23:48 出处:网络
I am trying to parse a Word document and the information I am looking for should be located on the fi开发者_开发技巧rst page only. Is there any way to get the page number for a paragraph?

I am trying to parse a Word document and the information I am looking for should be located on the fi开发者_开发技巧rst page only. Is there any way to get the page number for a paragraph?

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


From this post VSTO 2007: how do I determine the page and paragraph number of a Range? i could see you could get Page number form 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);
    }

And from this post, how to detect Empty paragraph in Word Document using Microsoft.Office.Interop.Word in C#4.0? i am sure u could find the Range from the paragraph!

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

you should have your solution combining both the answers, i bet!


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

Something like that might be what you're looking for. Read up on the difference between wdActiveEndPageNumber and wdActiveEndAdjustedPageNumber to see which one suits your need.

0

精彩评论

暂无评论...
验证码 换一张
取 消