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

Free Support Forum - aspose.com

How to apply mutliple MergingCallback

Hello,


I am performing a mail merge with mulitple regions with Aspose.Word.
I would like to apply multiple MergingCallback.
for exemple : I would like to handle default values with the first callback, then to handle resizing images with the second callback.

Is that possible to do so using Aspose words ?

Thanks

Arnaud Cavat
Hi Arnaud,

Thanks for your inquiry. Yes, you can achieve your requirements by setting MailMerge.FieldMergingCallback multiple times before calling MailMerge.Execute or MailMerge.ExecuteWithRegions. Please check the following code example for your kind reference.

Hope this helps you. Please let us know if you have any more queries.


Document doc = new Document(MyDir + "in.docx");

// Add a handler for the MergeField event.

doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();

// Load some Html from file.

StreamReader sr = File.OpenText(MyDir + "in.htm");

string htmltext = sr.ReadToEnd();

sr.Close();

// Execute mail merge.

doc.MailMerge.Execute(new string[] { "htmlField1" }, new string[] { htmltext });

// Add a handler for the MergeField event.

doc.MailMerge.FieldMergingCallback = new HandleMergeImageFieldFromBlob();

// Execute mail merge.

doc.MailMerge.Execute(new string[] { "Image" }, new string[] { "" });

// Save resulting document with a new name.

doc.Save(MyDir + "MailMerge Out.doc");

private class HandleMergeFieldInsertHtml : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

// All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.

if (e.DocumentFieldName.StartsWith("html"))

{

// Insert the text for this merge field as HTML data, using DocumentBuilder.

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.DocumentFieldName);

builder.InsertHtml((string)e.FieldValue);

// The HTML text itself should not be inserted.

// We have already inserted it as an HTML.

e.Text = "";

}

}

///

/// This is called when mail merge engine encounters Image:XXX merge field in the document.

/// You have a chance to return an Image object, file name or a stream that contains the image.

///

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// Do nothing.

}

}

private class HandleMergeImageFieldFromBlob : IFieldMergingCallback

{

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

// Do nothing.

}

///

/// This is called when mail merge engine encounters Image:XXX merge field in the document.

/// You have a chance to return an Image object, file name or a stream that contains the image.

///

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)

{

// The field value is a byte array, just cast it and create a stream on it.

MemoryStream imageStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\in.png"));

// Now the mail merge engine will retrieve the image from the stream.

e.ImageStream = imageStream;

}

}