Repeated Region: How to Achieve This?

I want to a summary of orders group by order date and subtotal and grand total for the records.
Output would be

Sales
From 11 Nov 2011 to 13 Nov 2011

Date:11 Nov 2011
SR# ITEM_NAME Quantity
1 Pizza 2
2 Cold Drink 2
total orders=2
Date:12 Nov 2011
3 Pizza 2

4 Cold Drink 6
5 Pizza 7
6 Cold Drink 3

total orders=4
Date:13 Nov 2011
7 Pizza 2
8 Cold Drink 6

9 Pizza 7
total orders=3
Plz note that one first row of the result is header row. How can i do that? Should i make two templates.
i.e one containing bookmarks and the other containing a data table. I have to use table for records. and bookmarks for the date. Any idea? should do that programticaly?
code so far:

Document doc = new Document("D:\\Templates\\MyTemplate.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

// We call this method to start building the table.

builder.startTable();
ResultSetMetaData metaData = rs.getMetaData();

// header row for columns header
System.out.println(metaData.getColumnCount());
int rowCount = 1;
for (int j = 1; j < 2; j++)
{
    System.out.println("Header Row" + rowCount);
    for (int i = 1; i <= metaData.getColumnCount(); i++)

    {
        builder.insertCell();
        builder.write(metaData.getColumnName(i));
        System.out.print(" - " + metaData.getColumnName(i));

    }
    builder.endRow();
}
System.out.println();

// columns values

while (rs.next())
{
    rowCount++;
    System.out.println("Row Started" + rowCount);
    for (int i = 1; i <= metaData.getColumnCount(); i++)
    {
        builder.insertCell();
        builder.write(rs.getObject(i).toString());
        System.out.print("----" + i);
    }
    System.out.println("Row Ended" + rowCount);
    builder.endRow();
}

// Signal that we have finished building the table.

builder.endTable();

// Save the document to disk.

doc.save("D:\\templates\\MyTemplate.doc");

rs.close();

Hi Bilal,

Thanks for your inquiry. I think, in your case, it would be easy for you if you use mail merge feature of Aspose.Words for reporting purpose. Please see the following link for more details:
https://docs.aspose.com/words/java/mail-merge-and-reporting/

Moreover, to be able to achieve what you are looking for, I would like to suggest you to read the following article:
https://docs.aspose.com/words/net/nested-mail-merge-with-regions/

I hope, this will help.

Best Regards,

Thank u awais for the quick reply.

I already read the article u mentioned at

https://docs.aspose.com/words/net/nested-mail-merge-with-regions/

but problem is that i want to repeat the table with the header.

Plz see my desired output. And I also want to do it in programing way . what should i do then? Am i on the wrong path?

Hi
Thanks for your request. It would be great if you attach your sample data, template, output and expected output documents. We will check them and provide you more information.
Best regards,

Attached is the output file i got through above code. I want to group it by order date and want to do a total and sub total for each date. Then at the end i want to do grand total. Output like in the first post.

Hi
Thanks for your request. I think to achieve this you will have to use mail merge with nested regions and perform pre-processing of your data.
https://docs.aspose.com/words/net/nested-mail-merge-with-regions/
Unfortunately, there is no direct way to achieve what you need, so you will have to pre-process your data.
Best regards,

Thank u very much for the reponses and help,

The link to article is for data that is hierarichal. i.e parent child relation. I have result set that i need to iterate on a list of dates. And i want to do that dynamicly (with code). I am creating a document with document builder and then saving the result set by iterating on date.
for each date in datelist

doc.getMailMerge().executeWithRegions(tableName, rs);

Hi
Thank you for additional information. But it is not clear for me what the problem is.
For example, in your template you have nested regions. In this case, you should create a data source with structure that match structure of nested regions. You should do this during pre-processing your input data.
In addition, I think, in your case, you can use ImailMergeDatasource:
https://reference.aspose.com/words/net/aspose.words.mailmerging/imailmergedatasource/
Best regards,

ok
i will be back after i get the desired ouput. Thank u for the time and support.

Hi,

I hope all the aspose members are doing good. I have achieved the desired ouptut after some modifications in the required output. I repeated a table (no of iterations = no od dates ) inside a document.
Aspose programmers guide helped me a lot, Thank u all.

Hi,

Thanks for your request. It is perfect that you managed to achieve the desired output. Please let us know if you need more assistance, we will be glad to help you.

Best Regards,

Now the problem is that my table is splitting on multiple pages, I have 100 or more tables repeated according to date. But the table is splitting across pages. If i want to restrict the table to split across pages of a word what should i do?

instead of

// Disable breaking across pages for all rows in the table.
for(Row row : table)
    row.getRowFormat().setAllowBreakAcrossPages(false);

or

// To keep a table from breaking across a page we need to enable KeepWithNext
// for every paragraph in the table except for the last paragraphs in the last
// row of the table.
for (Cell cell: (Iterable) table.getChildNodes(NodeType.CELL, true))
    for (Paragraph para: cell.getParagraphs())
        if (!(cell.getParentRow().isLastRow() && para.isEndOfCell()))
            para.getParagraphFormat().setKeepWithNext(true);

i want some think like

NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
Iterator nodIter = tables.iterator();
while (nodIter.hasNext())
{
    Table table = (Table) nodIter.next(); **
    for (Row row: table) **

        row.getRowFormat().setAllowBreakAcrossPages(false);
}

but the bold line creates error;
Type mismatch: cannot convert from element type Node to Row

Hi
Thanks for your request. Maybe you should use something like this:

NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
Iterator <Node> nodIter = tables.iterator();
while (nodIter.hasNext())
{
    Table table = (Table) nodIter.next();
    for (Row row: table.getRows())
        row.getRowFormat().setAllowBreakAcrossPages(false);
}

Best regards,

Thank u for ur help. I am stuck at this.

NodeCollection tables = dstDocBookMark.getChildNodes(NodeType.TABLE, true);
Iterator nodIter = tables.iterator();
while (nodIter.hasNext())
{
    Table table = (Table) nodIter.next();
    Rows rows = table.getRows(); **
    for (Row row: rows) **
        row.getRowFormat().setAllowBreakAcrossPages(false);
}

NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
Iterator nodIter = tables.iterator();
while (nodIter.hasNext())
{
    Table table = (Table) nodIter.next();
    for (Row row: table.getRows()) 
        row.getRowFormat().setAllowBreakAcrossPages(false);
}

I get the same error :Type mismatch: cannot convert from element type Node to Row. I got same error when i used insertdocument code in my application,

I changed

// Loop through all sections in the source document.
for (Section srcSection: srcDoc.getSections())
{

to

for (Node section: doc.getSections())
{

beacuse i was getting same type of error: Type mismatch: cannot convert from element type Node to Section

What is the problem in casting?

Hi
Thanks for your request. Which version of Aspose.Words for Java do you use? There were problems with typed collection in old versions of Aspose.Words for Java. So please try using the latest version and let me know how it goes on your side. You can download the latest version from here:
https://releases.aspose.com/words/java
Also, as I can see in the code example here:
https://docs.aspose.com/words/java/insert-and-append-documents/
we use:

for (Section srcSection: srcDoc.getSections())

getSections(), getRows(), getCells() methods returns typed collection. So you can iterate through items in them without casting.
Best regards,

Thank u for help.How i can do it with older version. any workaround?

Hi
Thanks for your request. Use code like this:

for (int i = 0; i <doc.getSections().getCount(); i++)
{
    Section sect = doc.getSections().get(i);
    // Do something with section.
    // ...........................
}

Best regards,

End:

Table table = null;
Rows rows = null;
int rowsCount = 0;
while (nodIter.hasNext())
{
    table = (Table) nodIter.next();
    rows = table.getRows();
    rowsCount = rows.getCount();
    for (int i = 0; i <rowsCount; i++)
    {
        rows.get(i).getRowFormat().setAllowBreakAcrossPages(false);
    }
}

Thank u.

Hi
It is perfect that you managed to implement what you need. Please feel free to ask in case of any issues, we will be glad to help you.
Best regards,