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

Free Support Forum - aspose.com

Subtotal and carry over subtotal from previous page

Hi,
I’m using aspose words for mail merge.
I need to create word templates, with subtotal on each page and carryover the subtotal to the next page. When creating the word template, I don’t know how many items will be inserted (1, 2, … or 1000), so the result will have a variable number of pages.
How could I do this?
Is there a way with Aspose Words mail merge? Or with the LINQ Reporting engine?
As attachment, an example word document how it should look like after mail merge.
Example Subtotal and Carry over.zip (10.2 KB)

@GEDAT,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. MS Word document is flow document and does not contain any information about its layout into lines and pages. Therefore, technically there is no “Page”, “Line” concept in Word document. Pages and lines are created by Microsoft Word on the fly.

Aspose.Words uses its own Rendering Engine to layout documents into pages and lines. The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

In your case, we suggest you following solution.

  1. After mail merge, please iterate through all rows of table for a page and sum the cell’s content (numbered value).
  2. You can get the page index of row/cell using LayoutCollector.GetStartPageIndex. This method returns 1-based index of the page where node begins.
  3. Insert two rows in the table before the last row on the table. Please use Please use NodeCollection.Insert method to insert a node into the collection at the specified index.
  4. Insert the sub total and “carry over from previous page” into newly added rows.

Best Regards,
Tahir Manzoor

Hi,
thank you for your answer.
I will try the next weeks.
You don’t have already an example, which do something similar?

@GEDAT,

Thanks for your inquiry. Please check the following code example. You can use the same approach to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");

//Perform mail merge
//Your code...

int pagecount = doc.PageCount;
for (int i = 2; i <= pagecount; i++)
{
    InsertRows(doc, i);
}

doc.Save(MyDir + "out.docx");

public static void InsertRows(Document doc, int page)
{
    LayoutCollector layoutCollector = new LayoutCollector(doc);
    var collection = doc.GetChildNodes(NodeType.Row, true);

    double pageSum = 0.0;
    foreach (Row row in collection)
    {
        if (layoutCollector.GetStartPageIndex(row) == page - 1)
        {
            if (row.FirstCell.ToString(SaveFormat.Text).Trim() != "carry over from previous page")
            {
                double sum = 0.0;
                bool isNumeric = double.TryParse(row.LastCell.ToString(SaveFormat.Text).Trim(), out sum);
                if (isNumeric)
                    pageSum = pageSum + sum;
            }
        }

        //Insert two rows
        if (layoutCollector.GetStartPageIndex(row) == page)
        {
            Row r = (Row)row.PreviousSibling.Clone(true);
            foreach (Cell cell in r.Cells)
            {
                cell.RemoveAllChildren();
                cell.EnsureMinimum();                        
            }

            Run run = new Run(doc, "Subtotal");
            run.Font.Size = 20;
            r.Cells[0].FirstParagraph.AppendChild(run);

            run = new Run(doc, pageSum.ToString());
            run.Font.Size = 20;

            r.Cells[1].FirstParagraph.AppendChild(run); 
            row.ParentTable.Rows.Insert(row.ParentTable.Rows.IndexOf(row) - 1, r);

            r = (Row)row.PreviousSibling.Clone(true);
            foreach (Cell cell in r.Cells)
            {
                cell.RemoveAllChildren();
                cell.EnsureMinimum();
            }

            run = new Run(doc, "carry over from previous page");
            run.Font.Size = 20;
            r.Cells[0].FirstParagraph.AppendChild(run);

            run = new Run(doc, pageSum.ToString());
            run.Font.Size = 20;

            r.Cells[0].FirstParagraph.AppendChild(run);
            r.Cells[1].FirstParagraph.AppendChild(run);
            row.ParentTable.Rows.Insert(row.ParentTable.Rows.IndexOf(row) - 1, r);

            break;
        }
    }
}

Best Regards,
Tahir Manzoor