Aspose.Words Mail merge formatting issue

Hi Aspose Support,
I have merge field data which should contain formatting data “Hello \u000a\u000ad World .”

I would like to get the output rendered in my document as newline

Hello
World

\u000a is NewLine character .
I want to give similar unicode charcters and it should be formatted .

How can i achieve that ?

Hi there,

Thanks for your inquiry. You just need to pass unicode as part of your field data. Please check following sample code snippet for the purpose. Hopefully, it will help you to accomplish the task.

// Open an existing document.
Document doc = new Document("MailMerge.ExecuteArray.doc");
// Trim trailing and leading whitespaces mail merge values
doc.MailMerge.TrimWhitespaces = false;
// Fill the fields in the document with user data.
doc.MailMerge.Execute(
new string[] { "FullName", "Company", "Address", "Address2", "City" },
new object[] { "James\u000a\u000dBond", "MI5 Headquarters", "Milbank", "", "London" });
doc.Save("MailMerge.ExecuteArray_out.doc");

Please feel free to contact us for any further assistance.

Best Regards,

Hi Thank you for your reply.
If i have html content it does not render properly.

doc.MailMerge.FieldMergingCallback = new HandleMergeFields();
doc.MailMerge.Execute(
new string[] { "FullName", "Company", "Address", "Address2", "City" },
new object[] { "James\u000a\u000dBond", "<b>ABC</b>", "Milbank", "", "London" });
doc.Save("MailMerge.ExecuteArray_out.doc");

If i use HTML call back code as below HTML its showing properly . But James Bond its not coming in new line. What is the solution for this ? Thanks for your reply .

class HandleMergeFields : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldValue != null)
        {
            // Create document builder
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            // Move cursor to field
            builder.MoveToField(e.Field, true);
            // Insert HTML
            builder.InsertHtml(e.FieldValue.ToString(), true);
            // Remove the string representation of HTML
            e.Text = string.Empty;
        }

    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // DO NOTHING
    }
}

Hi there,

Thanks for your feedback. You may exclude mail merge field with unicode values from IFieldMergingCallback as following, it will help you to accomplish the task.

…
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
    if (e.FieldName !="FullName")
    {
        // Create document builder
        DocumentBuilder builder = new DocumentBuilder(e.Document);
....

Best Regards,

Hi Thanks for your reply .
It does not workout for me since few field data might contains both Unicode and HTML data.

Ex : James\u000a\u000dBond Thanks in Bold hello in italic

if my data is in this format it does not work out. Please suggest me any other approach .

Thanks,

Hi there,

Thanks for your feedback. Unicode characters do not work in HTML text, as a workaround you may replace these characters to HTML line break tag by using the following code. Hopefully, it will help you to achieve your requirements.

…
// Insert HTML
builder.InsertHtml(e.FieldValue.ToString().Replace("\n", "").Replace("\r", "<br>"), true);
....

Please feel free to contact us for any further assistance.

Best Regards,

Ok Thank you very much for response .