Get the page number a table is on

hi,
how to get the page number of the document where the table exists?
regards,
sheeba

Hi
Thanks for your request. Unfortunately, there is no direct way to determine on which page a particular node is located. However, you can try using code like the following to achieve this:

private int GetTableStartPageNumber(Table table) throws Exception
{
    // Create a DocumentBuilder and move it to the bookmark.
    DocumentBuilder builder = new DocumentBuilder((Document) table.getDocument());
    builder.moveTo(table.getFirstRow().getFirstCell().getFirstParagraph());
    // Insert a PAGE field and update it.
    Field page = builder.insertField("PAGE");
    builder.getDocument().updatePageLayout();
    page.update();
    int pageNumber = Integer.parseInt(page.getResult());
    // Remove PAGE field.
    page.remove();
    return pageNumber;
}

Hope this helps.
Best regards,

hi,
Thanks. I get error on ‘new DocumentBuilder()’ since it is abstract.
Please help.
Regards,
Sheeba

Hello
Thanks for your inquiry. Please just add the following line to your project:

import com.aspose.words.DocumentBuilder;

Best regards,

Appreciate your great help.
It works fine. Thank you.
Regards,
Sheeba

Hi,
I have some problems in getting page number. consider the case when page-1 has a small table and some blank lines (new line char); page-2 has another small table. Then page number of table-2 is retrieved as 1.
regards,
Sheeba

Hello
Thanks for your request. Could you please attach the document you are getting problem with? I will check it on my side and provide you more information.
Best regards,

hi Andrey,
please find attached file. the problem is when a table is split into two pages, page number is given as second page.
sincerely
Sheeba

Hello
Thanks for your request. I cannot reproduce the problem on my side. I use the following code for testing:

Document doc = new Document("C:\\Temp\\TestTable.doc");
NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
for (int i = 0; i <tables.getCount(); i++)
{
    System.out.println("Tabele " + i + " is started on page " + GetTableStartPageNumber((Table) tables.get(i)));
}

There are 8 tables inside your document, and my output looks like this:
Tabele 0 is started on page 1
Tabele 1 is started on page 1
Tabele 2 is started on page 1
Tabele 3 is started on page 3
Tabele 4 is started on page 3
Tabele 5 is started on page 3
Tabele 6 is started on page 3
Tabele 7 is started on page 5
Best regards,

hi,

Thank you very much.It was my mistake that I did not call page.remove();

Sorry for the inconvenience caused.
regards,
sheeba

Hi
It is perfect, that you already resolved the problem. Please let us know in case of any issues. We will be glad to help you.

Best regards,