Mail Merge - preserving paragraph formats

I’m using the Mail Merge functionality to generate Word reports. I’ve created a custom IFieldMergingCallback class in order to insert HTML directly into the document. Everything works pretty much as expected except for some formatting issues. One specific scenario is I where I include a mail merge field in an indented paragraph (Left Indent moved right approx .5 inch). As long as the HTML doesn’t contain any
or
or other block elements, the inserted HTML is indented correctly. The moment a block level element is encountered, the left indent is reset on the block element’s paragraph and the text doesn’t vertically align with the paragraphs above it. Is there a way to continue the paragraphs format when inserting HTML?

Thanks in advance.

Hi,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  1. Your input Word document.
  2. Aspose.Words generated output document which shows the undesired behavior.
  3. Your expected Word document which shows the correct result. Please create this document using MS Word for our reference.
  4. Please create a standalone (runnable) console application that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issues and provide you more information.

Best regards,

Attached is a word document that contains the output of the report along with the expected output. Also attached is a sample application that generated the report output.

Thanks.

Hi,

Thanks for the additional information. Please note that, when you insert content by using Insert HTML, whole formatting is taken from HTML snippet. However, I think you can achieve the desired formatting by using the following code:

public class ASPOSECustomFieldMerging: IFieldMergingCallback
{
    public ASPOSECustomFieldMerging()
    {}
    public void FieldMerging(FieldMergingArgs args)
    {
        if (args.DocumentFieldName.StartsWith("Html:"))
        {
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.MoveToMergeField(args.DocumentFieldName);
            // builder.InsertHtml((string)args.FieldValue);
            InsertHtmlWithBuilderFormatting(builder, args.FieldValue.ToString());
            args.Text = "";
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // do nothing
    }
    public static void InsertHtmlWithBuilderFormatting(DocumentBuilder builder, string html)
    {
        new DocumentBuilderHelper(builder).InsertHtmlWithBuilderFormatting(html);
    }
}
public class DocumentBuilderHelper: INodeChangingCallback
{
    public DocumentBuilderHelper(DocumentBuilder builder)
    {
        mBuilder = builder;
        // Duplicate original List Pargraph containing the mergefield
        mPara = (Paragraph) mBuilder.CurrentParagraph.Clone(false);
    }
    ///
    /// Inserts an HTML snippet into document using a combination of the current formatting from the supplied DocumentBuilder object
    /// and the input HTML.
    ///
    public void InsertHtmlWithBuilderFormatting(string html)
    {
        Document doc = mBuilder.Document;
        // Store any callback already set on this document
        INodeChangingCallback origCallback = doc.NodeChangingCallback;
        // Stores nodes inserted during the InsertHtml call.
        doc.NodeChangingCallback = this;
        // Insert HTML.
        mBuilder.InsertHtml(html);
        // Restore the original callback
        doc.NodeChangingCallback = origCallback;
        if (mPara != null)
        {
            foreach(Paragraph para in mParagraphs)
            {
                para.ParagraphFormat.Style = mPara.ParagraphFormat.Style;
                para.ParagraphFormat.LeftIndent = mPara.ParagraphFormat.LeftIndent;
            }
        }
        // mPara.Remove();
    }
    void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
    {
        if (args.Node.NodeType == NodeType.Run)
        {
            if (!mParagraphs.Contains(((Run) args.Node).ParentParagraph))
                mParagraphs.Add(((Run) args.Node).ParentParagraph);
        }
    }
    void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
    {}
    void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
    {}
    void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
    {}
    private ArrayList mParagraphs = new ArrayList();
    private DocumentBuilder mBuilder;
    private Paragraph mPara;
}

I hope, this helps.

Best regards,

Thanks for the example, it looks like that is what I need.