We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

What's the page number

Basically I have a document that has many tables. Each table has a headings and that heading may be the same as the previous table heading.

If all the tables for a given page have the same heading blank out the heading for all tables on that page except the first. Using the code below (granted this is a first draft), should achieve the desired effect but what I have noticed is that the layoutCollector.GetStartPageIndex and GetEndPageIndex method are returning the wrong page number for a given table. This causes incorrect formatting of the table headers.

int lastPageNumber = 1; //LayoutCollector is one based.
var lastHeading = "";
var layoutCollector = new LayoutCollector(_document);
var layoutEnumerator = new LayoutEnumerator(_document);
NodeCollection tables = _document.GetChildNodes(NodeType.Table, true);
for (var index = 2; index < tables.Count; index++)
{
    var table = (Table)tables[index];
    //Result is the same as .GetStartPageIndex and .GetEndPageIndex
    //var firstCellParagraphobject = layoutCollector.GetEntity(table.FirstRow.FirstCell.FirstParagraph);
    //var lastCellParagraphobject = layoutCollector.GetEntity(table.LastRow.FirstCell.LastChild);
    // layoutEnumerator.Current = firstCellParagraphobject;
    //var tableStartPage = layoutEnumerator.PageIndex;
    // layoutEnumerator.Current = lastCellParagraphobject;
    //var tableEndPage = layoutEnumerator.PageIndex;
    var tableStartPage = layoutCollector.GetStartPageIndex(table.FirstRow.FirstCell.FirstParagraph);
    var tableEndPage = layoutCollector.GetEndPageIndex(table.LastRow.FirstCell.LastChild);
    var catHeading = table.Rows[0].Cells[0].GetText();
    if (tableStartPage == tableEndPage && lastPageNumber == tableStartPage)
    {
        if (catHeading == lastHeading)
        {
            table.Rows[0].Cells[0].FirstParagraph.Runs.Clear();
            table.Rows[0].Cells[0].FirstParagraph.AppendChild(new Run(_document, ""));
        }
        else
        {
            lastPageNumber = tableStartPage;
            lastHeading = catHeading;
        }
    }
    else
    {
        lastPageNumber = tableStartPage;
        lastHeading = catHeading;
    }

}

I have attached three document that illustrate the issue.

Lorem lpsum - before.docx - Before any formatting, the original document. This document was also generated via Aspose.

Lorem lpsum after.doc - actual - after formatting tables via the code above.

Lorem lpsum - desired.docx - What I want to accomplish. The desired result.

The issue is shown in the after document on pages 1 (the heading Services 1 appears twice), page 5 (heading Services 10 appears twice) and page 6 (first table Service 10 heading is missing). I believe this is the result of the wrong page index returned from the methods mentioned above.

Hi there,

Thanks for your inquiry. We have investigated the issue and noticed that it is expected behavior. Please note last two tables on first page are actually a single table, so second Services 1 row data is not removed. Furthermore, in reference to Services 10 issue, it is expected behavior. When we remove the table header row data in the document, 7 pages’ document reduces to 6 pages. So Services 10 table moves to page 5 from page 6 in resultant document. If you remove repeating heading text manually in MS Word, you will get same results. Please check attached sample documents for reference.

As a workaround, you can change text color of heading row to white. It will resolve the Services 10 issue.

Best Regards,

Thanks Tilal for the response. I did not notice that Services 1 table was a single table. Thanks. Secondly if I change the text color of the heading row to white where applicable then when I save the document as a PDF all the table headings appear as black text. See attached pdf.

Hi there,

Thanks for your feedback. You may set row height to control the text flow, it will help you to accomplish the task.

table.Rows[0].Cells[0].FirstParagraph.Runs.Clear();
table.Rows[0].Cells[0].FirstParagraph.AppendChild(new Run(_document, ""));
table.Rows[0].RowFormat.Height = 10;

Best Regards,

Hello,

Still having one heck of a time determining what page a node is located. The attached document was created using Aspose.Words. Using the code below when I attempt to determine the page number that a FootNote resides it returns a page number that does not exist;

_document = new Document(@"FootNoteOnPageNumber");
var existingFootNotes = _document.GetChildNodes(NodeType.Footnote, true);
var xFootNotes = existingFootNotes.Cast()
    .Where(existingFootNote => existingFootNote.ToString(SaveFormat.Text).Trim() ==
    "suscipit eum qui rerum molestias quam eum velit aperiam eos vel officiis modi libero voluptatem quibusdam ipsam officia facere sunt aut natus alias sunt enim");
foreach (var xFootNote in xFootNotes)
{
    var layoutCollector = new LayoutCollector(_document);
    var existingFootNotePageNumber = layoutCollector.GetStartPageIndex(xFootNote);
    //existingFootNotePageNumber returns page 6. Page 6 does not exist. 
}

I know I am missing something, please advise.

Thanks for the help.

Hi there,

Thanks for your inquiry. The document has some missing or “invalid” width for tables and by default Aspose.Words does not recalculate table widths. Please call Document.UpdateTableLayout() as following. It will fix the issue and will return correct page number of Footnote.

Document _document = new Document(@"D:\Downloads\FootNoteOnPageNumber.docx");
_document.UpdateTableLayout();

….

Best Regards,

Thanks Tilal. I’ll need to find where that is occurring. How did you see the invalid Table width?

Hi there,

Thanks for your inquriy. Please implement IWarningCallback to capture the TableLayout warnings and call UpdateTableLayout. You do not need to call this method if the tables appear correct in the output document.

public class HandleTableLayout : IWarningCallback
{
    public void Warning(WarningInfo info)
    {
        {
            Console.WriteLine(info.Description);
        }
    }
}

Please feel free to contact us for any further assistance.

Best Regards,