We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Label Sample Code


Could I have a source code in C# sample?

I have a datatable with data. Each record data is on a label and 2 labels per section.

For example:

John Smith
123 Polk Street
Isabelle Beaudoin
3443 Berri
Demetra Jolie
444 Hello Street
Thu Nguyen
23 Hanoi

I can use a word template or create on fly… as long I have the result.

Please give me the simple solution.

Tan Quan


I’m using ASPOSE.Words component.
I’m looking forward to receive your response.


Thanks for your inquiry. I think the simplest way to achieve what you need is using Mail Merge with regions.
I created simple example for you. Here is code:

//Create dummy datasource
DataTable table = new DataTable("Labels");
//Add few rows
table.Rows.Add(new object[] { "John Smith", "123 Polk Street" });
table.Rows.Add(new object[] { "Isabelle Beaudoin", "3443 Berri" });
table.Rows.Add(new object[] { "Demetra Jolie", "444 Hello Street" });
table.Rows.Add(new object[] { "Thu Nguyen", "23 Hanoi" });
//Open template
Document doc = new Document(@"Test012\in.doc");
//execute mail merge with regions
//Save output document

Template and output document are attached.
I used the following structure of the template:

{ TableStart:Labels }{ Name }{ Address }|{NEXT}{ Name }{Address}{ TableEnd:Labels }
NEXT field is needed to move to the next record in the datasource. (You can press Alt+F9 to see field codes in the template)
Hope this helps.
Best regards.

It works.
Thank you.

After testing with this template(see the attachment file) with the source code that you give me… The result are different and not I expected.
If there is a way to have the result as you mention below with this template.
Could you give me a new sample of source code so I can try?
I have try the source code that you give me a table and it work but not with this template (see the attachment file). What should I do to make it work?
Tan Quan


Thanks for your inquiry. You cannot use ExecuteWithRegions method with template you have attached, because there are no “regions” in your template (TableStart:myRegion and TableEnd:myRegion). So, Aspose.Words will not insert any data in your template if you use ExecuteWithRegions. In case of using Execute method, aspose.Words will insert data into your template, however it will generate separate section for each two records in your data source.
Why you do not use template like the one I have shown in my previous post?
I modified your template and now it should work.
Best regards.

Thank … work like a charm.

I would be grateful if you could post again the sample doc you used for the labels example.

I just seen it, sorry for this post.