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.