Aspose.words for Java MailMerge with format switch not working

Aspose.words for Java MailMerge with format switch not working.

I am using the latest version “Aspose.Words for Java 10.6”.

I am trying to format a number field in word document with the switch like “# $###,##.00” and I am not getting the values come after merge. If I remove the field formatting switch then I see the values.

Please send us a quick response…

Sample field:

ENTRIES_TRX_PRICE # $####,##.00

Hi
Thanks for your request. It seems you pattern is not valid. I used the following switch:
{ MERGEFIELD ENTRIES_TRX_PRICE \# $#,##0.00 }
And the following code for testing:

Locale L = new Locale("en", "nz");
Locale.setDefault(L);
Document doc = new Document("C:\\Temp\\in.doc");
String[] names = {
    "ENTRIES_TRX_PRICE"
};
Object[] obj = {
    123456.7
};
doc.getMailMerge().execute(names, obj);
doc.save("c:\\Temp\\out.doc");
doc.save("c:\\Temp\\out.pdf");

The output is correct. Could you please try modifying your switch and let me know if this helps.
Best regards,

Hi alexey,

I tried changing the format switch to what you suggested the merge result still comes as blank.

I did set the Locale as en-nz.

Attaching the word document and its corresponding PDF output after mail merge…

Hi
Thank you for additional information. Have you tried calling Document.UpdatePageLayout method before converting your document to PDF? This might help.
Best regards,

Hi alexey,

I just tested the cahnge you suggested ( call dstDoc.updatePageLayout() before converting to PDF)
it still doesn’t work, the field comes out empty if I use format switches…

I am just including the inpuit XML for your tests…

We use XML and the earlier attached word doc to do mail merge with your JAVA package and produce the PDF file…

Hi
Thank you for additional information. But unfortunately, I do not see XML. Could you please reattach it again? I will check it and provide you more information.
Best regards,

Here is the attachment…

Hi
Thank you for additional information. Sorry, I missed a mistake in your template. In your template ENTRIES_PRICE field is formatted incorrectly. Here is field code of this merge field:
{ MERGEFIELD “ENTRIES_PRICE \# $#,##0.00” }
As you can see both name and formatting switch are enclosed into double quotes. That is why Aspose.Words considers all text within quotes as name of merge field. Since there is no such name in your data source, Aspose.Words does not insert value in this field. You should modify this merge field as shown below:
{ MERGEFIELD ENTRIES_PRICE # $#,##0.00 }
Hope this helps.
Best regards.

Hi Alexey,

But the same exact document works with your earlier version (2.X).

We have several customers who could potentially have this :(.

Please let me know how this could be addressed.

Hi Ravikumar,
Thanks for your request. I am not sure this could work in old versions. As I can see even MS Word incorrectly determines field name of this merge field. I think, the best option is to modify your template and correct this merge field.
Best regards,

Hi,

Here are two pdf’s:

  1. orig.pdf --generated from older version of Aspose
  2. debug.pdf – generated from latest version of Aspose

They both use the same word document template and same data xml.

I have 2 different servers with old and new Aspose libraries…

Can you think of any java side fixes for this issue?

Hi,

I have done some more tests different combinations of field and format switch.

I am attaching the word template, xml and corresponding generated PDF’s from old and new aspose versions.

Please help me with this migration.

Hi there,

Thanks for this additional information.

I’m afraid the PDF files you attached are actually HTML files of one of your source pages. Could you please reattach the output PDFs you are generating?

In regards to your template, you will need to modify the fields in the way that Alexey has described to you earlier. From memory, the older versions of Aspose.Words were incorrectly parsing such field codes and ignoring the enclosing speechmarks. This would allowed you to merge such fields with the older versions. However this was actually a bug as the proper way to read the name of the merge field (the same as how MS Word does it) is take the entire text between the speechmarks. This is why mail merge is not working in your case.

Since it appears the names of your merge fields do not use any spaces, you can just remove the speechmarks all together. These are only needed when the name of merge field has a space in it.

Please let us know if we can help with anything else.

Thanks,

Hi Adam,

I attaching the PDF files again.

I understand your fix and that is fine, but I have too many such documents.

Is there any way for us fix the field names on the fly and move with the merging process? or there are any other solutions?

Hi there,

Thanks for this additional information.

I believe you will need to programmatically remove these speech marks in such a case. You can retrieve the field code of a merge field and use regex to check if this is required.

The code on the following thread may be helpful as an example: https://forum.aspose.com/t/99796 However please note it in C# and not Java. Please let us know if you need any assistance.

Thanks,