locked
Document/Mail merging RRS feed

  • Question

  • User-833960295 posted
    Hi All, I am not sure whether this the right place for this Post but just giving it a try. I need to implement the document merging facility provided in MSWord without using the any interop's That is basically i have to implement document merging without the use of MSOffice. An Equivalent of that would be the Aspose.Words. what i have to implement is 1. it should substitute the word merge fields with the actual data from the datasource considering an word template.docx 2. show content in HTMl format. Any comments would be of great help. Thanks in Advance
    Wednesday, September 17, 2008 2:08 AM

All replies

  • User-2034328377 posted

    Hi,

    Yes, you can meet this requirement using an independent 'Aspose.Words for .NET' API. Please refer to the following section of documentation:

    Mail Merge and Reporting

    Secondly, you can insert Html during mail merge by using the following code snippet:

    // File 'MailMerge.InsertHtml.doc' has merge field named 'htmlField1' in it.
    // File 'MailMerge.HtmlData.html' contains some valid Html data.
    // The same approach can be used when merging HTML data from database.
    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
    {
        /// <summary>
        /// This is called when merge field is actually merged with data in the document.
        /// </summary>
        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.
        }
    }

    Hope, this helps.

    Monday, October 19, 2015 9:40 AM