Current date is always formatted as English after exporting to PDF

I am using the following code to clean-up language settings in our docx files:

    public Document apply(byte[] bytes) throws Exception {


        try (InputStream stream = new ByteArrayInputStream(bytes)) {
            Document doc = new Document(stream);

            int newLocaleId = EditingLanguage.fromName(languageString);

            StyleCollection styles = doc.getStyles();
            styles.getDefaultFont().setLocaleId(newLocaleId);

            NodeCollection<?> os = doc.getChildNodes(NodeType.ANY, true);
            for(Object o : os){
                if(o instanceof Inline inline){
                    inline.getFont().setLocaleId(newLocaleId);
                }
            }

            doc.updateFields();

            System.err.println(EditingLanguage.getName(doc.getStyles().getDefaultFont().getLocaleId()));

            return doc;
        }
    }

Unfortunately, this does not help to format the field TIME @ “d. MMMM yyyy” into the correct locale. It is always displayed in English.

If I export to PDF directly from Word, it works.
In the print preview it is also set correctly.

Aspose export to PDF always shows the field in English.

The original file and the “fixed” files are attached.

Many thanks+
test-sprache.zip (55.5 KB)

@kerner By default Aspose.Words uses the current thread’s locale to update fields. So you should either specify the thread’s locale using the following code:

// Set locale for the current thread.
Locale current = CurrentThreadSettings.getLocale();
CurrentThreadSettings.setLocale(Locale.forLanguageTag("de-DE"));

Document doc = new Document("C:\\Temp\\in.docx");
doc.save("C:\\Temp\\out.pdf");

// Restore locale.
CurrentThreadSettings.setLocale(current);

Or instruct Aspose.Words to use field code as field update culture source:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);
doc.save("C:\\Temp\\out.pdf");
1 Like

That does not work unfortunately.

Only overriding thread local settings works.

Thank you!

@kerner The following code work fine on my side:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);
doc.save("C:\\Temp\\out.pdf");

Here is the produced output: out.pdf (46.7 KB)

I tested with both document you have attached in the initial post.

1 Like

Maybe there is a difference:

I call this code when “cleaning up”, and then saving it back to disk.

When generating, I am reading the doc again into Aspose and exporting it to PDF.

Is FieldUpdateCultureSource.FIELD_CODE serialized to the doc on disk?

I will try to call this right before generating the PDF…

Confirmed!

Calling

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

before saving the doc works!

@kerner Yes, this should be done be done before updating field in the document. Aspose.Words updates field upon saving document to PDF.

1 Like