Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

ms-word

I am trying to parse a Word document and the information I am looking for should be located on the first 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 */ 
    // }
}
like image 246
leoinfo Avatar asked Aug 17 '11 18:08

leoinfo


2 Answers

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.

like image 179
Paul H. Avatar answered Nov 19 '22 02:11

Paul H.


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!

like image 45
ioWint Avatar answered Nov 19 '22 03:11

ioWint