How to get page number of specific node using .NET

Hi,

I’m extracting starting page number for doc paragraphs. For some of processed documents, Aspose.Words gave me incorrect result, paragraph page no is greater than number of pages in document. Code to reproduce result is:

[Test]
public void Paragraph_PageNo_Less_Than_PageCount()
{
    const string testFile = @"EPUBLISHER-513.doc";
    Document document = new Document(testFile);
    DocumentBuilder builder = new DocumentBuilder(document);
    IList fields = new List();

    var paragraphs = document.GetChildNodes(NodeType.Paragraph, true);

    foreach (Paragraph paragraph in paragraphs)
    {
        if (paragraph.HasChildNodes)
        {
            builder.MoveTo(paragraph.FirstChild);
        }
        else
        {
            builder.MoveTo(paragraph);
        }
        builder.Font.Hidden = true;
        fields.Add(builder.InsertField("PAGE", "1"));
    }
    document.UpdatePageLayout();
    foreach (var field in fields)
    {
        Assert.LessOrEqual(
        Int64.Parse(field.Result),
        document.PageCount);
    }
}

Attached test document. Tested with Aspose.Words 11.0 and 11.1 .NET. Tast document has 1 page but it says paragraph is on 688 page.

Is there any workaround for this behavior?

Regards
Jacek Bator

Hi Jacek,

Thanks for your query. This is not an issue with Aspose.Words. Aspose.Words to mimic exactly what MS Word do. Please add page number in your document by using MS word. The page number will be 668.

Hi there,

Thanks for your inquriy.

Tahir is correct, the section in your document has a page starting number of “668” which is why you are getting such a result. You will need to set this programmatically before running that code.

There are also a few other problems with your code that will cause an incorrect result:

  • doc.UpdatePageLayout won’t actually update the PAGE fields in the document, instead all page fields will be left with the result “1” (the default that the field was inserted with). You need to call doc.UpdateFields() as well to update the field results.
  • You are inserting page fields into the paragraphs of header and footers. A page field result in a header footer is always returns the total number of pages in the document. This is the correct behavior as the header or footer spans across all pages in the document. You may want to avoid inserting fields into the header or footer in your code.

Please see the modified code below.

Document doc = new Document(featuresDir + "EPUBLISHER-513.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
IList<Field> fields = new List<Field>();
foreach (Section section in doc.Sections)
    section.PageSetup.RestartPageNumbering = false;
var paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph paragraph in paragraphs)
{
    if (paragraph.HasChildNodes)
    {
        builder.MoveTo(paragraph.FirstChild);
    }
    else
    {
        builder.MoveTo(paragraph);
    }
    builder.Font.Hidden = true;
    fields.Add(builder.InsertField("PAGE", "1"));
}
doc.UpdatePageLayout();
doc.UpdateFields();

Thanks,

Thank you for quick answer. So it seems to be connected with PageSetup.PageStartingNumber
and PageSetup.RestartPageNumbering
So to resttart it from begining I should set PageSetup.PageStartingNumber = 1 and PageSetup.RestartPageNumbering = true for first document section and PageSetup.RestartPageNumbering = false for all other sections?

Regards
Jacek Bator

Hi Jacek,

You should be able to just remove any restart page numbering from all sections in the document and it will take on the normal 1…2…3 page numbering scheme. This can be seen in my code above.

Thanks,

Thanks, it works this way.

Regards
Jacek Bator

The issues you have found earlier (filed as WORDSNET-2978) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(66)