Mail Merge Questions

I’m looking into options for generating Word and PDF documents from an ASP.Net application. I think using mail merge with Aspose.Words will do the trick, but I have a few questions I need answered before I can request purchasing this product. In particular, my requirements are as follows:

  1. Some data fields being merged will contain HTML, others fields will contain plain text
  2. HTML fields may contain tags
  3. I must be able to set the page header and footer

From what I’ve read, #1 can be handled without to much issue. On #2, I haven’t run across any documentation regarding support for tags inside HTML. Will these get rendered in the merged document? And finally #3, I’m guessing that setting the header and footer after the merge is completed also won’t be a problem, but I do have to verify that.
Thanks in advance.

Hi,

Thanks for your interest in Aspose.Words and sorry for the delayed response. I am glad to inform you that you can easily achieve all three of your requirements by using Aspose.Words.

  1. Please see the following code that demonstrate how to mail merge HTML data into a document:
public void MailMergeInsertHtml()
{
    Document doc = new Document(MyDir + "MailMerge.InsertHtml.doc");
    // Add a handler for the MergeField event.
    doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
    // Load some Html from file.
    StreamReader sr = File.OpenText(MyDir + "MailMerge.HtmlData.html");
    string htmltext = sr.ReadToEnd();
    sr.Close();
    // Execute mail merge.
    doc.MailMerge.Execute(new string[]
    {
        "htmlField1"
    }, new string[]
    {
        htmltext
    });
    // Save resulting document with a new name.
    doc.Save(MyDir + "MailMerge.InsertHtml Out.doc");
}
private class HandleMergeFieldInsertHtml: IFieldMergingCallback
{
    ///
    /// This is called when merge field is actually merged with data in the document.
    ///
    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 = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}
  1. Sure, Aspose.Words automatically downloads the image before inserting into the document if you specify a remote URL in ‘src’ attribute of in Html. Therefore, Aspose.Words supports importing images from element and the output Word document will contain the images .

  2. Please refer to the following article of the documentation which outlines everything you need to know about Headers/Footers:
    https://docs.aspose.com/words/net/working-with-headers-and-footers/

If we can help you with anything else, please feel free to ask.

Best regards,