Two columns in nested document

Hi,
I wanted to know how one would go about having multiple columns in a document, and then inserting this into another document.
My example is this. I have a master document (the actual report), and then a document (a section of the report) that needs to be inserted. That document to insert has various information in it (various generic test fields), but also has a list of items (one could think of a order detail document, listing items being ordered, quantity, cost, etc).
We would like to possibly make just this list, not the whole subdocument or master document into two columns to save space. I tried to make this section on the subdocument to work, but once it gets inserted into the master document, it looses these columns.
Also, the data in these columns are part of a mail merge. Another way to get arround this (and maybe easier) would be to have to tables side by side. Is this possible? An example of what we would like to see is below:

Information about your order
Item 1 Item 3
Cost $2 Cost $3
Item 2 Item 4
Cost $1 Cost $550
Thank you for your order
----
As you can see, just the items are listed side by side, with text above and below not in columns.
Thanks.

Hi

Thanks for your inquiry. Maybe example provided here could help you.
https://forum.aspose.com/t/94886
Also, please attach your template and code that you use to insert multi-column document. Maybe you should use AppendDocument method in your case:
https://docs.aspose.com/words/net/insert-and-append-documents/
Best regards.

I am going to try adding the NEXT mail merge field to my document using your linked example above.

Hi

Please let me know in case of any issues, I will be glad to help you.
Best regards.

The provided example helped a bit, but still didn’t solve the problem. I now have a table with 4 columns, which I would want repeat. But the two ‘columns’ share the same data per row
ex:
1 1
2 2
rather than
1 2
3 4
Also, does it make a difference with how the merge fields are inserted? For example, All of my fields look like this - <>. In your linked example, it shows { NEXT }. I inserted this NEXT field using the NEXT field. If I press alt+f9, I see {NEXT} inserted, but it doesnt appear to have used it.
Jon

Hi

Thanks for your inquiry. Could you please attach your template for testing? I will investigate the issue and provide you more information.
Best regards.

attached is the file.

Hi

Thank for your inquiry. The template you have attached works fine on my side. I used the following code for testing:

// Create dummy datasource
DataTable tab = new DataTable("ICRProperty");
tab.Columns.Add("propSeqNum");
tab.Columns.Add("propType");
tab.Columns.Add("propStatus");
tab.Columns.Add("propArtType");
tab.Columns.Add("propDesc");
tab.Columns.Add("propTagNum");
tab.Columns.Add("propOther");
// add few rows
tab.Rows.Add(new object[] { "1", "first", "propStatus", "propArtType", "propDesc", "1", "propOther" });
tab.Rows.Add(new object[] { "2", "second", "propStatus", "propArtType", "propDesc", "2", "propOther" });
tab.Rows.Add(new object[] { "3", "third", "propStatus", "propArtType", "propDesc", "3", "propOther" });
tab.Rows.Add(new object[] { "4", "forth", "propStatus", "propArtType", "propDesc", "4", "propOther" });
// open template
Document doc = new Document(@"Test029\incident_crime_report.doc");
// execute mail merge with regions
doc.MailMerge.ExecuteWithRegions(tab);
// save output document
doc.Save(@"Test029\out.doc");

I get the expected output
item 1 item 2
Item 3 item 4
Best regards.

My code looks very similar to yours, except two items:

  • each of the fields are using a doc.mailmerge.mappedDataField
  • I also have an eventhandler for the ‘propOther’ field, which builds out a bunch of extra rows depending on what else is availabe in the database.

Otherwise, I have the table coming from a database and filling these fields, with a new row for each item. For some reason, with the {NEXT} it doesnt seem to go to the next row.

Hi

Thank you for additional information. Could you please create sample code that allow me to reproduce the problem? I will try to reproduce the problem and provide you more information.
Best regards.

My code looks like this

if (icrDS.Tables.Count > 0)
{
    dtICRProperty = icrDS.Tables[0];
    dtICRProperty.TableName = "ICRProperty";
    tempICRDoc.MailMerge.MappedDataFields.Add("propSeqNum", "property_sequence_number");
    tempICRDoc.MailMerge.MappedDataFields.Add("propType", "property_type_description");
    tempICRDoc.MailMerge.MappedDataFields.Add("propStatus", "property_status_description");
    tempICRDoc.MailMerge.MappedDataFields.Add("propArtType", "article_type_description");
    tempICRDoc.MailMerge.MappedDataFields.Add("propDesc", "property_description");
    tempICRDoc.MailMerge.ExecuteWithRegions(dtICRProperty);
    tempICRDoc.MailMerge.MappedDataFields.Clear();
    dtICRProperty.Clear();
    dtICRProperty.Dispose();
}

I commented out my handler for this part of code, and i still get the same result.

Hi

Thank you for additional information. I still cannot reproduce the problem on my side. Here is the code I use for testing:

// Create dummy datasource
DataTable tab = new DataTable("ICRProperty");
tab.Columns.Add("property_sequence_number");
tab.Columns.Add("property_type_description");
tab.Columns.Add("property_status_description");
tab.Columns.Add("article_type_description");
tab.Columns.Add("property_description");
tab.Columns.Add("propTagNum");
tab.Columns.Add("propOther");
// add few rows
tab.Rows.Add(new object[] { "1", "first", "propStatus", "propArtType", "propDesc", "1", "propOther" });
tab.Rows.Add(new object[] { "2", "second", "propStatus", "propArtType", "propDesc", "2", "propOther" });
tab.Rows.Add(new object[] { "3", "third", "propStatus", "propArtType", "propDesc", "3", "propOther" });
tab.Rows.Add(new object[] { "4", "forth", "propStatus", "propArtType", "propDesc", "4", "propOther" });
// open template
Document doc = new Document(@"Test029\incident_crime_report.doc");
// Add mapped fields
doc.MailMerge.MappedDataFields.Add("propSeqNum", "property_sequence_number");
doc.MailMerge.MappedDataFields.Add("propType", "property_type_description");
doc.MailMerge.MappedDataFields.Add("propStatus", "property_status_description");
doc.MailMerge.MappedDataFields.Add("propArtType", "article_type_description");
doc.MailMerge.MappedDataFields.Add("propDesc", "property_description");
// execute mail merge with regions
doc.MailMerge.ExecuteWithRegions(tab);
// save output document
doc.Save(@"Test029\out.doc");
Maybe you should check the data that comes from your data source.For example try to output data to console.
foreach (DataRow row in tab.Rows)
{
    foreach (DataColumn col in tab.Columns)
    {
        Console.Write("{0}\t", row[col]);
    }
    Console.WriteLine();
}

Best regards.

After moving this template and code into another document, i managed to get it to work! I actually use the AppendDocument method you mentioned above to add this back in. I may move this around to using a DocumentBuilder and InsertDocument method to place the document where needed.
Im not really sure what was causing this. If I dumped out all the data in the table, things came back fine. If I used your code and table, it worked great as well.
I do have one remaining question though.
In some cases, I obviously will have an odd ammount of items. It looks like this
1 2
3 4
5 xyz
where XYZ would be my template fields.
Is there a way to hide all of these feilds easily if there is nothing to bind them to?

Hi

Thanks for your inquiry. I think you can to remove non-merged mergefields from the document. You should call MailMerge.DeleteFields() after executing mail merge. See the following link for more information:
https://reference.aspose.com/words/net/aspose.words.mailmerging/mailmerge/deletefields/
Hope this helps.
Best regards.