Aspose LINQ Reporting automatically starts a new page

145 Views Asked by At

I am using Aspose LINQ Reporting to generate word document. and the word template shows as below what i want is:

  1. When the amount of data is less than the number of rows in the table, will add blank column with description blank below.
  2. When the amount of data is greater than the number of rows in the table, will automatically starts a new page with header and footer

word template

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

1

There are 1 best solutions below

1
On

I am afraid there is no direct way to achieve what you need. You can put table header into the document header and table footer into the document footer. enter image description here enter image description here

NOTE: In MS word table cannot be the last node of the story (body, header or footer) and there is always empty paragraph after table. So to avoid empty space after the footer you can decrease font size of the empty paragraph: enter image description here

This will allow to repeat table footer and header on each page of the report. But since MS Word documents are flow by their nature there is no concept of page and there is no direct way to determine whether there is empty space on the page. Aspose.Words has it's own layout engine and using LayoutCollector and LayoutEnumerator to calculate empty space after the table. This will allow to add empty rows to the table after building the report. For example see the following code:

// Some dummy data.
string[] dummyData = new string[100];
for (int i = 0; i < dummyData.Length; i++)
{
    dummyData[i] = $"Dummy Data {i}";
}

// Open template.
Document doc = new Document(@"C:\Temp\in.docx");
// Build report.
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dummyData, "items");

// Suppose there is one table in the table.
// Use LayoutCollector and LayoutEnumerator to calculate last table row bounds
// to check whether additional empty rows must be added.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Calculate visible page bounds
PageSetup ps = doc.FirstSection.PageSetup;
RectangleF pageRect = new RectangleF(
    (float)ps.LeftMargin,
    (float)ps.TopMargin,
    (float)(ps.PageWidth - ps.LeftMargin - ps.RightMargin),
    (float)(ps.PageHeight - ps.TopMargin - ps.BottomMargin));

// Get bounds of the last row of the table.
Table table = doc.FirstSection.Body.Tables[0];
enumerator.Current = collector.GetEntity(table.LastRow.FirstCell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Row)
    enumerator.MoveParent();
RectangleF lastRowBounds = enumerator.Rectangle;

// Check therer there is empty space avaialbel on the page.
if (lastRowBounds.Bottom < pageRect.Bottom)
{
    float availableEmptySpace = pageRect.Bottom - lastRowBounds.Bottom;
    Row emptyRow = (Row)table.LastRow.Clone(true);
    emptyRow.GetChildNodes(NodeType.Paragraph, true).Clear();
    int emptyRowsCount = (int)(availableEmptySpace / lastRowBounds.Height) - 1;
    for (int i = 0; i < emptyRowsCount; i++)
        table.AppendChild(emptyRow.Clone(true));
}

doc.Save(@"C:\Temp\out.docx");

Here is result: enter image description here