Is Aspose Striping spaces from around MailMerge Fields

We have recently upgraded to Aspose Words for .Net 14.7.0.0 from 9.7.0.0

In our word templates we have code like the following that worked before the upgrade:

{ IF { MERGEFIELD Payment.OneTimeAssistance } = " X " " {MERGEFIELD TableStart:ReinstatementPayees } … {MERGEFIELD TableEnd:ReinstatementPayees } " “” }

In the IF statement there is a space before and after the X. This code had been working correctly for the last 3 years. After the update the contents of the table are not being rendered.

I verified the line of business system is passing the " X " to the Aspose Words when generating the merge document.

Can you tell me if the updated API does a trim prior to Merging the field? This would explain the behavior we are seeing.

The Condition is being evaluated as “X” = " X " instead of " X " = " X ".

Hi there,

Thanks for your inquiry. Please implement IFieldMergingCallback interface if you want to control how data is inserted into merge fields during a mail merge operation. In your case, I suggest you please use the following code example to achieve your requirements. Hope this helps you.

private class HandleMergeFieldTest : IFieldMergingCallback
{
    /// 
    /// This is called when merge field is actually merged with data in the document.
    /// 
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder(e.Document);
        builder.MoveToMergeField(e.DocumentFieldName);
        builder.Write(e.FieldValue.ToString().Trim());
        e.Text = "";
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}
Document doc = new Document(MyDir + "in.docx");
// Add a handler for the MergeField event.
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldTest();
// Execute mail merge.
doc.MailMerge.ExecuteWithRegions(........);
// Save resulting document with a new name.
doc.Save(MyDir + "Out.docx");

If the problem still remains, please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code (Aspose.Words code) you used to generate
your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

I have put together a sample application to show the problem.

I have attached a zip file. the zip file contains two folders:

AsposeWordTest9.7 – Application containing a reference to the 9.7 Aspose.Words file.

AsposeWordTest14.7 – Application containing a reference to the 14.7 Aspose.Words File.

Both folders would require a Aspose.Word.lic file to be added to the directory.

Both applications contain the Sourcetestdoc.doc and Sourcetestdoc.docx file. The 9.7 version does not use the Sourcetestdoc.docx file.

Both folder have a folder named Results with Aspose ##.#. These are the resulting files after running the console application.

When running the console application with 9.7 the all of the test pass. When running with 14.7 the 1st test fails.

Please let me know if you have any other questions. The code suggestion did not help to resolve our issue.

Thanks

Brian

Hi there,

Thanks
for sharing the detail. Please note that Aspose.Words mimics the same behavior as MS Word does. If you update the fields using MS Word, you will get the same output. Moreover, please check the following IF field codes. Both are not same in your documents. So this is not an issue.

{ IF X = " X " “Test 1 Passed” “Test 1 Failed” } // 14.7.0
{ IF " X " = " X " “Test 1 Passed” “Test 1 Failed” } // 9.7

Could you please share the console application which you are using to generate the output document? We will then provide you more information about your query along with code.

I have attached the console app as requested.

You will need to add the Aspose.Words.Lic file.

If you look @ the Sourcetestdoc.doc. This is the file used to generate the results document.

The only thing I am changing in the console application is which version of Aspose I am using and the resulting merge file returns the data as you point out.

Hi there,

Thanks for sharing the detail. I
have tested the scenario and have managed to reproduce the same issue
at my side. For the sake of correction, I have logged this problem in
our issue tracking system as WORDSNET-10839. I have linked this forum
thread to the same issue and you will be notified via this forum thread
once this issue is resolved.

We apologize for your inconvenience.

Hi there,

Further to my last post, please note that Aspose.Words mimics the same behavior as MS Word does. If you perform the same scenario using MS Word, you will get the same output. MS Word and Aspose.Words trimmed the spaces before/after mail merge field’s value while performing the mail merge operation. In output document ’ X ’ become ‘X’. So we close WORDSNET-10839 as ‘Not a bug’.

Hope this clears the detail of the issue which you are facing. Please let us know if you have any more queries.

This does not clear the detail.

From our view, there is a change in behavior in Aspose.Words from version 9.7 to 14.7 that is causing quite a bit of re-work on our part.

I will add to my long list of to do’s to investigate this behavior in word.

Hi there,

Thanks for your inquiry. The behavior of Aspose.Words 9.7 is incorrect. This was a bug in Aspose.Words older versions and fixed in latest versions of Aspose.Words.

Please open your input document in MS Word and perform mail merge with same data source which you using in your code. Open MS Word, from Mailings tab, Select Recipients->Type new list-> Customize columns, insert the same data which you are using in your application. MS Word and Aspose.Words output will be same.

Please let us know if you have any more queries.

For what it’s worth, I ran into the same issue as you were. We recently upgraded to Aspose.Words for java v15.4.0 from a Aspose.Words for .NET version from sometime in 2010. Previously, whitespaces in mail merge fields were being preserved and the new version strips them out, causing some formatting issues on client documents that were not going to fly.

I managed to work around the issue in the following way:

— CODE —

public class FormatMergeFields implements IFieldMergingCallback {

    @Override
    public void fieldMerging(FieldMergingArgs arg0) throws Exception {

        /*
         * Apparently, whitespace hasn't been stripped yet when calling
         * getFieldValue() at this point, and using setText suppresses whitespace
         * trimming, so this should preserve leading and trailing whitespaces on
         * merged fields
         */

        if (arg0.getFieldValue() != null &&

                Character.isWhitespace(((String)arg0.getFieldValue()).charAt(0))) {

            arg0.setText((String) arg0.getFieldValue());

        }

    }

    @Override

    public void imageFieldMerging(ImageFieldMergingArgs arg0) throws Exception {}

}

— /CODE —

I hope this helps, it appears to do the trick for us.

Best of luck,

-Arthur

Hi there,

We always appreciate positive feedback from our customers. Please note that Aspose.Words mimics the same behavior as MS Word does. Yes, one can implement IFieldMergingCallback interface and control field’s value during a mail merge operation.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.