Free Support Forum - aspose.com

Joining two tables

Hello,
I have a requirement to create a master child table in the following manner.

1. Master parameter A

Child parameter1

blah blah blah(data from source)

Child parameter2

blah blah blah(data from source)

2.Master parameter B

Child parameter1

blah blah blah(data from source)

Child parameter2

blah blah blah(data from source)



Since Aspose doesnt support nested tables,i am creating the tables by creating it and appending it one by one.
The template document with merge fields is as follows <o:p></o:p>

<>.<>

<>Child parameter1

<>

Child parameter2

<><<<>


The logic is as follows:

a)Deep clone the template(with merge fields) in order to use the appending logic.
b)Create a mailmerge.execute for the “master” parameter.

c)Then create a mailmerge.executewithregions(IMailMergeDataSource) for the child parameters and the corresponding data.

d)Use the logic to append the tables to the destination document repeatedly.

But this logic gives the following result:

1. Master parameter A

Child parameter1

blah blah blah(data from source)

Child parameter1

blah blah blah(data from source)



2.Master parameter A

Child parameter1

blah blah blah(data from source)

Child parameter1

blah blah blah(data from source)


The tables are getting created separately.Could you please help me as to how i can create such a data as part of one huge table.Please let me know if you need any more inputs from my side.

Thanks,
Varun

Hi

Thanks for your inquiry. Could you please provide me code that you use to perform mail merge. Also attach your template document. I will investigate this and try to help you.

Best regards.

Hi Alexey,
Thanks for the immediate reply.

Before i get down to the problem i was talking about in the earlier post,I have another small clarification.I want to populate tables in a horizontal manner.Let me attach a template and explain my request for clarification.

«city»

«TableStart:dummy»Season

«season»

Temperature

«temp»«TableEnd:dummy»

The output i am looking for is something like this:

Bangalore

Season

Summer

Temperature

30

Season

winter

Temperature

15

Season

autumn

Temperature

20

Season

spring

Temperature

25


<o:p> So the problem i am facing is that,the table is actually a horizontal table.Usually the value titles(season and temperature in this case) would be column headers and the values from the data source are obtained and filled in the table column wise.</o:p>

<o:p> In this case the value titles are row headers and the table has to built up row wise.</o:p>

<o:p>Does Aspose support this?I am asking this because I got the following exception using the template i used.</o:p>

<o:p>Mail merge region ‘dummy’ is badly formed. TableStart and TableEnd should be in the same section, same table row or same table cell.</o:p>

<o:p>If it is not supported,is there a work around to build such tables?</o:p>

<o:p>Thanks a lot for your help.</o:p>

<o:p>Varun</o:p>

<o:p>PS:As of now,the option of building up the table using a document builder is not open.I want to be able to do this using a mailmerge.
</o:p>


Hi

Thanks for your inquiry. As workaround you can try using the following code. Also see attached template.

C#

ArrayList listOfEmptyParagraps = new ArrayList();

public void TestMailMerge_106693()

{

DataTable table = new DataTable("dummy");

table.Columns.Add("season");

table.Columns.Add("temp");

for (int i = 0; i < 10; i++)

{

DataRow row = table.NewRow();

row[0] = "test";

row[1] = "+10";

table.Rows.Add(row);

}

string[] names = { "city" };

string[] values = { "Test" };

Document doc = new Document(@"428_106693_vamurthy\in.doc");

doc.MailMerge.RemoveEmptyParagraphs = true;

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_106693);

doc.MailMerge.Execute(names, values);

doc.MailMerge.ExecuteWithRegions(table);

//remove empty paragraphs between tables

foreach (Node node in listOfEmptyParagraps)

{

if (node.ParentNode != null)

{

node.Remove();

}

}

doc.Save(@"428_106693_vamurthy\out.doc");

}

void MailMerge_MergeField_106693(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "season")

{

Table table = (e.Field.Start.ParentParagraph.ParentNode as Cell).ParentRow.ParentTable;

listOfEmptyParagraps.Add(table.PreviousSibling);

listOfEmptyParagraps.Add(table.NextSibling);

}

}

You are using java, right? If you need I will translate this code.

Best regards.

Wow!!!Thats a really cool work around.Thanks a lot.

Yes,I am using Java.Can you please translate this one line for me.

<span style=“font-size: 10pt; font-family: “Courier New”;” lang=“EN-US”>Table table =
(e.Field.Start.ParentParagraph.ParentNode as Cell).ParentRow.ParentTable;

Thanks,
Varun

Kinda figured it out.
Can you please confirm this for me??

Table table=((Cell)e.getField().getStart().getParentParagraph().getParentNode()).getParentRow().getParentTable();

Hi

Here is code that you can use.

Cell cell = (Cell)e.getField().getStart().getParentParagraph().getParentNode();

Table table = cell.getParentRow().getParentTable();

Best regards.

Hello Alexy,
Thanks for the help.I implemented your logic with the template you sent and i was able to implement a horizontal table.Now getting back to the problem of joining tables.I have attached the code as well as the template file and the generated file.

1)Launcher.java: Main file which initializes the documents and calls the mailmerge statements.
2)CATimail.java: The implementation of the iMailmergeDatasource interface.
3)mergefieldeventhandler.java: Class which holds the implementation of the MergeFieldEventHandler (the mergeField method)

4)in.doc:template file
5)dummy.doc:generated file.

The output i want should have all the 4 tables(one for each city) joined,just like the way the last 3 tables are.Why is the first table appearing separately on the first page.How can i join all the 4 tables and make it appear as a single table?

Thanks,
Varun

Hi

Thanks for your request. I think that you can try using two separate templates. I tried to solve your task and created this code in C#. Also I attached two templates and output document.

ArrayList listOfRows = new ArrayList();

public void TestMailMerge_106693()

{

DataTable cities = new DataTable("cities");

cities.Columns.Add("city");

for (int i = 0; i < 10; i++)

{

DataRow row = cities.NewRow();

row[0] = "test"+i.ToString();

cities.Rows.Add(row);

}

//perform mailmerge

Document doc = new Document(@"428_106693_vamurthy\in.doc");

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_Dummy_106693);

doc.MailMerge.ExecuteWithRegions(cities);

//create result table

Table mainTable = (listOfRows[0] as Row).ParentTable;

for (int i = 1; i < listOfRows.Count; i++)

{

mainTable.Rows.Add((Row)listOfRows[i]);

}

doc.Save(@"428_106693_vamurthy\out.doc");

}

void MailMerge_MergeField_Dummy_106693(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "city")

{

DataTable dummy = new DataTable("dummy");

dummy.Columns.Add("season");

dummy.Columns.Add("temp");

for (int i = 0; i < 10; i++)

{

DataRow row = dummy.NewRow();

row[0] = "test";

row[1] = "+10";

dummy.Rows.Add(row);

}

//add row to list

Row wordRow = (e.Field.Start.ParentParagraph.ParentNode as Cell).ParentRow;

listOfRows.Add(wordRow);

//perform mail merge

Document doc = new Document(@"428_106693_vamurthy\in1.doc");

doc.MailMerge.ExecuteWithRegions(dummy);

NodeCollection rows = doc.GetChildNodes(NodeType.Row, true);

foreach (Row row in rows)

{

listOfRows.Add(e.Document.ImportNode(row, true));

}

}

}

I hope that this will help you.

Best regards.

Hello Alexey,
Thanks a lot!!!I tried out the code you came up with and its exactly what i required.Its a very effective work around.Thanks a ton.

Regards,
Varun

This feature is now available in this .NET update and in this Java update. You can now avoid empty paragraphs in repeating regions in this version by setting RemoveEmptyParagraphs to true.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(2)