This is a migrated thread and some comments may be shown as answers.

MailMerge into ONE Table

1 Answer 93 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Heiko
Top achievements
Rank 1
Iron
Veteran
Heiko asked on 04 Mar 2013, 10:28 AM
Hi!

What I want to achieve is to MailMerge a datasource into one single table via Doc API. I have already succeeded in creating a table, add a merge field to each cell and do a MailMerge, but this results in a single table row per record each with two paragraphs between them. Here is my code:

            Table table = new Table();
  
            TableRow row1 = new TableRow();
  
            TableCell cell1 = new TableCell();
            Paragraph p1 = new Paragraph();
            FieldRangeStart start = new FieldRangeStart();
            FieldRangeEnd end = new FieldRangeEnd();
            end.Start = start;
            start.Field = new MergeField() { PropertyPath = "FirstName" };
            Span s1 = new Span();
            s1.Text = "{MERGEFIELD FirstName}";
            p1.Inlines.Add(start);
            p1.Inlines.Add(s1);
            p1.Inlines.Add(end);
            cell1.Blocks.Add(p1);
            row1.Cells.Add(cell1);
  
... (add more table cells)
  
            table.Rows.Add(row1);       
            section.Blocks.Add(table);
  
            doc2.Sections.Add(section);
            doc2.MailMergeDataSource.ItemsSource = ExamplesDataContext.Employees;
            doc2.ChangeAllFieldsDisplayMode(FieldDisplayMode.Result);
  
            doc.GoToBookmark("tm1");
            doc.InsertFragment(new DocumentFragment(doc2.MailMerge(false)));

Regards
Neils

1 Answer, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 07 Mar 2013, 11:00 AM
Hi Neils,

The scenario you are trying to achieve is somewhat strange.

Generally, when executing a mail merge, each record is placed in a new section or is at least separated from the previous (in custom scenarios). That's the default behavior and is the expected one.

What is more, the logic you've implemented inserts a fragment that contains a whole table, so it is only natural for each table to be separate. Generally, RadDocument's structure does not allow adjacent tables, so they cannot be added without the additional paragraphs. Nonetheless, implementing a mail merge as the one explained is not one of the scenarios supported out of the box and you can verify the same behavior if you try it in MS Word.

What I believe will much better suit your need is to build your table with the actual values instead. As you are building your document from code, you can simply add all records to a table, putting each in a single row, and add to the RadDocument this table instead.

I hope this helps!

Regards,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
RichTextBox
Asked by
Heiko
Top achievements
Rank 1
Iron
Veteran
Answers by
Petya
Telerik team
Share this question
or