Locale specific field update / locale on a per-document base instead of Locale.getDefault()

Hi

The attached word document contains four lines, on each line the name of the language and a field for the date / time. Via “Language Ribbon / Language / Set Proofing language” the language for each line was set accordingly.
When updating the fields (F9) in Word, the date will be rendered in the language specified (see screenshot).

Code
final boolean updateFields = true;
final String folder = “c:/temp/”;

try (
FileInputStream fis = new FileInputStream(folder + “fields.docx”);
FileOutputStream fosDocx = new FileOutputStream(folder + “fields.out.docx”);
FileOutputStream fosPdf = new FileOutputStream(folder + “fields.out.pdf”);
FileOutputStream fosHtml = new FileOutputStream(folder + “fields.out.html”)) {
Document wordDoc = new Document(fis);

wordDoc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setUpdateFields(updateFields);
wordDoc.save(fosPdf, pdfSaveOptions);

HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions(SaveFormat.HTML);
htmlSaveOptions.setUpdateFields(updateFields);
wordDoc.save(fosHtml, htmlSaveOptions);

OoxmlSaveOptions ooxmlSaveOptions = new OoxmlSaveOptions(SaveFormat.DOCX);
ooxmlSaveOptions.setUpdateFields(updateFields);
if (updateFields) {
wordDoc.updateFields();
}
wordDoc.save(fosDocx, ooxmlSaveOptions);
}


Issue 1
When running the above code, the output of the dates will always be according to the locale returned by Locale.getDefault(). Word takes into consideration the languages, thus resulting in the appropriate date formatting for each language (tested with Word field update via F9 and storing to PDF / HTML formats).

Issue 2
HtmlSaveOptions#setUpdateFields(false) and OoxmlSaveOptions#setUpdateFields(false) seems to be ignored; the created html and word document always have the fields updated.
When calling the above code with updateField = false, the PDF Output shows the initial, non-updated field values. I’ve checked the Word output by inspecting document.xml, because Word will always update the fields when the document is opened.

Issue 3
I’ve already had a look at <a href="https://forum.aspose.com/t/47512. When having only one locale per document, setting the corresponding locale via Locale.setDefault would be an option in a single-threaded environment. As sherter stated in the mentioned forum thread, this will not work in a multi-threaded environment due to race condition (at least not without appropriate locking, which would reduce performance). Thus a way to set the locale on a per-document base would be highly appreciated, e.g. for example via com.aspose.words.Document#setLocale(Locale) or a similar method, which would then be used for standalone field update or in conjunction with conversion to e.g. PDF, HTML or Plain Text. This would solve issue 1 at least for documents with only a single locale (which is probably the most likely case anyway).

Tested with Aspose Words 16.2, 16.4 and 16.5.

Kind regards
Stephan

Hi Stephan,

Thanks for your inquiry.
bsiagch:
Issue 1
When running the above code, the output of the dates will always be according to the locale returned by Locale.getDefault(). Word takes into consideration the languages, thus resulting in the appropriate date formatting for each language (tested with Word field update via F9 and storing to PDF / HTML formats).
We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSJAVA-1394. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.
bsiagch:
Issue 2
HtmlSaveOptions#setUpdateFields(false) and OoxmlSaveOptions#setUpdateFields(false) seems to be ignored....
When SaveOptions.UpdateFields is false, fields are not updated before saving the document to a fixed page format. The field's result is rendered in output document without updating the field.
bsiagch:
Issue 3
Thus a way to set the locale on a per-document base would be highly appreciated, e.g. for example via com.aspose.words.Document#setLocale(Locale)....
This is Java specific issue. Please use CurrentThreadSettings class to set ThreadLocal or/and ThreadLocal before open/save a document with non-default locale settings. Hope, this helps.

Hi Tahir

Issue 1
Great, thank you.

Issue 2
When SaveOptions.UpdateFields is false, the fields are not updated when saving to PDF Format (which is correct). But the fields are updated when saving the HTML (may be correct, see ) or DOCX (incorrect).
When running the above code with updateFields = false, the PDF output is correct and shows the date according to the word document (i.e. Friday, 1. July 2016 16:06:24) but the HTML Output and the DOCX Output show the current date and time.
You mentioned that this option is only relevant when exporting to a fixed page format (thus PDF and DOCX). This would mean that when exporting to HTML this will be ignored and fields are always updated in this case (because HTML is not a fixed page format). Is that correct?

Issue 3
I didn’t know CurrentThreadSettings, that’s exactly what I need, thanks.

Kind regards
Stephan

Hi Stephan,

Thanks for your inquiry. Your understanding is correct regarding "Issue 2".

Please note that Aspose.Words mimics the same behavior as MS Word does. While exporting the Word document to fixed page format, MS Word updates some fields. We introduced a feature in Aspose.Words v15.11.0 to control this behavior by using SaveOptions.UpdateFields property. This property does not work for Docx and Html file format either the value of this property is false or true.

Please let us know if you have any more queries.

Hi Tahir

Thanks for the clarification.

Kind regards
Stephan

The issues you have found earlier (filed as WORDSJAVA-1394) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Tested successfully with 16.8.0, thank you.