Reporting Mail Merge - Place images horizontally

Hi,

I want to display 2 images in a row.

I am using ExecuteWithRegions with DataSet.Dataset is having two nested tables

Input :

DataSet ds = new DataSet();
DataTable nameDataTable = new DataTable("Names");
//First Row - 123
DataRow row = nameDataTable.NewRow();
row["Slide"] = "Name1";
row["id"] = "123";
nameDataTable.Rows.Add(row);
//Second Row - 345
row = nameDataTable.NewRow(); 
row["Slide"] = "Name2";
row["id"] = "345";
nameDataTable.Rows.Add(row);
DataTable classes = new DataTable("classes"); 
classes.Columns.Add("id");
classes.Columns.Add("gridImage",typeof(byte[]));
classes.Columns.Add("pid");//parent ID
//First Row having single image - 123
DataRow row1 = classes.NewRow();
Image image1 = Image.FromFile(@"G:\I1.bmp", true);
row1["gridImage"] = ImageToByte(image1); 
row1["id"] = "1";
row1["pid"] = "123";//First Row Child
classes.Rows.Add(row1);
//Second Row having two images - 345
row1 = classes.NewRow();
image1 = Image.FromFile(@"G:\I3.bmp", true);
row1["gridImage"] = ImageToByte(image1); 
row1["id"] = "3";
row1["pid"] = "345";
classes.Rows.Add(row1);
row1 = classes.NewRow();
image1 = Image.FromFile(@"G:\I4.bmp", true);
row1["gridImage"] = ImageToByte(image1); 
row1["id"] = "4";
row1["pid"] = "345";
classes.Rows.Add(row1);
ds.Relations.Add(new DataRelation("", nameDataTable.Columns["id"], classes.Columns["pid"], false));
_document.MailMerge.ExecuteWithRegions(ds);

Template MergeFields:

«TableStart:Names»
Slide : «Slide» 
«Image:grid» {NEXT}«Image:grid» 
«TableEnd:Names»

Expected Output:

Name1 
I1 //Displays I1 image
Name2 
I3 I4 //Displays I3 and I4

Output with above code and using Next Field

Name1 
I1 I3//Displays I1 and I3 images which is wrong
Name2 
I3 I4 //Displays I3 and I4

Problem is with DataSet , when no values are there for second image in first row, {Next} pulls images from second Row

Please let us know if you have any other method to display images horizontally.

Thanks,

Sanjay Kumar

Hi,

I want to display 2 images in a row.

I am using ExecuteWithRegions with DataSet.Dataset is having two nested tables

Input :

DataSet ds = new DataSet();
DataTable nameDataTable = new DataTable("Names");
//First Row - 123
DataRow row = nameDataTable.NewRow();
row["Slide"] = "Name1";
row["id"] = "123";
nameDataTable.Rows.Add(row);
//Second Row - 345
row = nameDataTable.NewRow(); 
row["Slide"] = "Name2";
row["id"] = "345";
nameDataTable.Rows.Add(row);
DataTable classes = new DataTable("classes"); 
classes.Columns.Add("id");
classes.Columns.Add("gridImage",typeof(byte[]));
classes.Columns.Add("pid");//parent ID
//First Row having single image - 123
DataRow row1 = classes.NewRow();
Image image1 = Image.FromFile(@"G:\I1.bmp", true);
row1["gridImage"] = ImageToByte(image1); 
row1["id"] = "1";
row1["pid"] = "123";//First Row Child
classes.Rows.Add(row1);
//Second Row having two images - 345
row1 = classes.NewRow();
image1 = Image.FromFile(@"G:\I3.bmp", true);
row1["gridImage"] = ImageToByte(image1); 
row1["id"] = "3";
row1["pid"] = "345";
classes.Rows.Add(row1);
row1 = classes.NewRow();
image1 = Image.FromFile(@"G:\I4.bmp", true);
row1["gridImage"] = ImageToByte(image1); 
row1["id"] = "4";
row1["pid"] = "345";
classes.Rows.Add(row1);
ds.Relations.Add(new DataRelation("", nameDataTable.Columns["id"], classes.Columns["pid"], false));
_document.MailMerge.ExecuteWithRegions(ds);

Template MergeFields:

«TableStart:Names»
Slide : «Slide» 
«Image:grid» {NEXT}«Image:grid» 
«TableEnd:Names»

Expected Output:

Name1 
I1 //Displays I1 image
Name2 
I3 I4 //Displays I3 and I4

Output with above code and using Next Field

Name1 
I1 I3//Displays I1 and I3 images which is wrong
Name2 
I3 I4 //Displays I3 and I4

Problem is with DataSet , when no values are there for second image in first row, {Next} pulls images from second Row

Please let us know if you have any other method to display images horizontally.

Thanks,

Sanjay Kumar

Hi there,

Thanks for your inquiry. Please use MailMerge.UseWholeParagraphAsRegion property to get or set a value indicating whether whole paragraph with TableStart or TableEnd field or particular range between TableStart and TableEnd fields should be included into mail merge region.

There is no need to use Next field in your template document. Please use following code example with attached template document to get the required output. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
doc.MailMerge.UseWholeParagraphAsRegion = false;
doc.MailMerge.ExecuteWithRegions(ds);
doc.Save(MyDir + "Out.docx");
«TableStart:Names»
Slide : «Slide»
«TableStart:classes»«Image:gridImage»«TableEnd:classes»
«TableEnd:Names»

Hi Tahir,

Thanks for your quick response.

The solution which you proposed worked fine, but we have few issues listed below:

  1. How to control the maximum number of images displayed in a row and

  2. How to set the image dimension as per Textbox/cell in the given template.

Thanks,
Sanjay Kumar

Hi Sanjay,

Thanks for your inquiry. Please implement IFieldMergingCallback interface if you want to control how data is inserted into merge fields during a mail merge operation. Please check the code example shared in following link.

How to Apply Custom Formatting during Mail Merge

The images are imported into Aspose.Words’ DOM as Shape nodes. Please use Shape.Width and Shape.Height properties to set the image’s width and height.

If you still face problem, please share your input and expected output documents here for our reference. We will then provide you more information about your query along with code.