Hi,
I have a Word template with the following three merge fields:
Name: { MERGEFIELD Name \* MERGEFORMAT}
Birthday: { MERGEFIELD Birthday \@ "d. MMMM yyyy" \* MERGEFORMAT}
Speed: { MERGEFIELD Speed \# "0,00" \* MERGEFORMAT}
This sample word file is attached. The following Java-Program works but produces the date and the number in German format.
Document doc = new Document("sample.docx");
String[] fieldNames = { "Name", "Birthday", "Speed" };
Object[] values = { "Eric", new Date(), 12.7 };
doc.getMailMerge().execute(fieldNames, values);
doc.save("output.pdf");
The output is
Name: Eric
Birthday: 24. Juli 2012
Speed: 12,7
But I want to have to following (more English) output for the date and the number field:
Name: Eric
Birthday: 24. July 2012 <- July instead of Juli
Speed: 12.7 <-decimal point instead of comma
I cannot use Java to create preformatted English strings because the author of the word template must have the option to choose the date and number formatting for each merge field individually.
Does Aspose Words support a field type or switch to change the language during the mergefield replacement?
Thanks in advance
Hi Christoph,
Sorry for the delayed response. I’m afraid there is no switch available for locale setting. But you can specify locale by using setLocaleId() method. In this case field will display text in the specified culture format. For example please see the following sample code:
// Specify locale (for instance French).
// All locale ids are available here http://msdn.microsoft.com/ru-ru/goglobal/bb964664(en-us).aspx
builder.getFont().setLocaleId(1036);
// Insert Date field.
builder.insertField("DATE \\@ \"dd MMMM yyyy\"", "");
Please feel free to contact us for any further assitance.
Best Regards,
Hi Christoph,
In addition, I think, you can make use of FieldOptions.FieldUpdateCultureSource property to achieve this. It is used to specify what culture to use to format the field result.
Please note that by default, the culture of the current thread is used and this setting affects only date/time fields with @ format switch.
For exmaple, the following code snippet shows how to specify where the culture used for date formatting during field update and mail merge is chosen from.
// Set the culture used during field update to the culture used by the field.
doc.FieldOptions.FieldUpdateCultureSource = FieldUpdateCultureSource.FieldCode;
doc.MailMerge.Execute(new string[] { "Date2" }, new object[] { new DateTime(2011, 1, 01) });
If we can help you with anything else, please feel free to ask.
Best Regards,