Free Support Forum -

MailMerge with WordML from Aspose.Editor into PDF


I'm using the Aspose.Editor on an aspx page (C# .NET 2.0 with VS2005) and saving the contents in WordML format into my database. I need to be able to MailMerge this WordML content along with other fields with a Word template (.doc rather than .dot, if that makes a difference). Ultimately, I will need to generate the final document in PDF.

I've been successful in doing simple MailMerge with the non-WordML content and generating the document in PDF. Is it possible to MailMerge WordML content and keep all the formatting? Any guidance you could provide would be greatly appreciated. I will probably need to use MailMerge with Regions.

By the way, I'd like to mention that the support I've received on the Editor has been tremendous. I want to commend Michael on his efforts in helping me get to where I am with my project today.

Regards - Dave


Thanks fro your inquiry. I think that you can try using MergeField event to achieve this. See the following link for more information.

Also here is code example for you. Template is attached.

public void TestMailMerge_106928()


string connString = "server=Web1;database=TestDB;uid=sa;pwd=password;";

System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connString);

//create DataSet

DataSet ds = new DataSet();

//Create sql command

string commandString = "SELECT * FROM WMLDocs";

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);

//create adapter

System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(command);


//fill dataset



ds.Tables[0].TableName = "WMLDocs";

Document doc = new Document(@"433_106928_heywasabi\in.doc");

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




void MailMerge_MergeField_106928(object sender, MergeFieldEventArgs e)


if (e.FieldName == "body")


byte[] body = Encoding.UTF8.GetBytes(e.FieldValue.ToString());

MemoryStream stream = new MemoryStream(body);

Document doc = new Document(stream);

DocumentBuilder builder = new DocumentBuilder(e.Document);


// Content of the specified document are inserted after the paragraph, containing the merge field.

InsertDocument(builder.CurrentParagraph, doc);

// If the paragraph, containing merge field, does not contain anything else - delete it.

if (builder.CurrentParagraph.ToTxt().Trim() == "")


// The field value text is not needed anymore, eraze it.

// Otherwise, it will be inserted at merge field location too.

e.Text = string.Empty; ;



WML document is stored as XML. Here is WMLDocs table.








InsertDocument method you can find here.

I hope that this will help you.

Best regards.


Thank you for the code sample. I was able to accomplish the task using MailMerge with the MergeFieldEventHandler as instructed.

My dilemma now is that the text in is cut off on the left side (please see attached zipped MS Word doc). How can I resolve this? Is this a possible issue with the template, or is it the WordML document as captured with Aspose.Editor, or is it something that can be controlled from Aspose.Words?

Thanks again...



Thanks for your inquiry. Could you please attach your template for testing? I will try to reproduce this problem and provide you more information.

Best regards.

I've attached the template as requested. Thank You.


Thanks for additional information. Your template works fine on my side. It seems that there is something wrong with your WML file. Could you please also attach your WML file?

Best regards.