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 */
// }
}
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With