Free Support Forum - aspose.com

GetStartPageIndex returns incorrect page number for Table node using C#

When i user layoutCollector.GetStartPageIndex(table) to get the start page index of the table which tableIndex is 20 in this document,it returns the wrong start page(1).The real page index is 9.please help me to find why is wrong!
Thanks.
Printout sample_ modified(1).zip (1.6 MB)

wrong table(with red board):
Printout sample_ modified(1).zip (1.6 MB)

@TobyLiu

Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue and provide you more information on it.

The maximum attachment of the website is 50000kb (about 48mb). My application package is only 20mb, but the website does not allow me to upload, so I can only copy the code to you.

Nuget package: Aspose. Words 19.8.0

.Net core 3.1

using System;

using Aspose.Words;
using Aspose.Words.Layout;
using Aspose.Words.Tables;

namespace StartPageIndexTestDemo
{
class Program
{
static void Main(string[] args)
{
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense(“Aspose.Total.lic”);

        var fileSrc = AppDomain.CurrentDomain.BaseDirectory + "/Printout sample_ modified(1).docx";
        var document = new Document(fileSrc);
        document.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2010);

        LayoutCollector layoutCollector = new LayoutCollector(document);
        var tables = document.GetChildNodes(NodeType.Table,true);
        foreach(Table table in tables)
        {
            var tableIndex = tables.IndexOf(table);
            var startPageIndex = layoutCollector.GetStartPageIndex(table);
            var endPageIndex = layoutCollector.GetEndPageIndex(table);
            if (tableIndex == 21)//The table 21 is which has error start Page Index.
            {
                Console.WriteLine($"Error across page index of table[{tableIndex}]:{startPageIndex}~{endPageIndex}");
            }
            else
            {
                Console.WriteLine($"Across page index of table[{tableIndex}]:{startPageIndex}~{endPageIndex}");
            }
        }
        Console.ReadLine();
    }
}

}

@TobyLiu

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-20229. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Is there some other way to get the start page of the table node?
Because we had to get the start page number urgently in the project in order to keep the logic running. I hope you can give me a feasible plan, I will be very grateful.

@TobyLiu

In your case, we suggest you following solution.

  1. Please insert the bookmarks in the first and last cell of table.
  2. Get the page number using BookmarkStart node.

Please check the following code example. Hope this helps you.

var document = new Document(MyDir + "Printout sample_ modified.docx");
DocumentBuilder builder = new DocumentBuilder(document);

document.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2010);

LayoutCollector layoutCollector = new LayoutCollector(document);
var tables = document.GetChildNodes(NodeType.Table, true);
foreach (Table table in tables)
{
    var tableIndex = tables.IndexOf(table);
    if (tableIndex == 21)//The table 21 is which has error start Page Index.
    {
        builder.MoveTo(table.FirstRow.FirstCell.FirstParagraph);
        builder.StartBookmark("startbm");
        builder.EndBookmark("startbm");

        builder.MoveTo(table.LastRow.LastCell.LastParagraph);
        builder.StartBookmark("endbm");
        builder.EndBookmark("endbm");

        //Console.WriteLine($"Error across page index of table[{tableIndex}]:{startPageIndex}~{endPageIndex}");
    }
    else
    {
        //Console.WriteLine($"Across page index of table[{tableIndex}]:{startPageIndex}~{endPageIndex}");
    }
}

Bookmark bm1 = document.Range.Bookmarks["startbm"];
Bookmark bm2 = document.Range.Bookmarks["endbm"];
document.UpdatePageLayout();

Console.WriteLine("Start table page no. " + layoutCollector.GetStartPageIndex(bm1.BookmarkStart));
Console.WriteLine("End table page no. " + layoutCollector.GetStartPageIndex(bm2.BookmarkStart));

The issues you have found earlier (filed as WORDSNET-20229) have been fixed in this Aspose.Words for .NET 20.5 update and this Aspose.Words for Java 20.5 update.