Display abnormal after Update and Unlink Word fields

I have a Word document that contains fields and I want to remove the fields and keep the displayed text.I try the code below, but the fields is displayed in Chinese after the call Update() and Unlink(). How can I keep the English displayed after processing? I opened the original file with MS Office, and the fields auto-update is normal.

void Main()
{
	var doc = new Aspose.Words.Document(@"C:\Users\543\Downloads\unlinkFields.doc");

	foreach (var field in doc.Range.Fields)
	{
		field.Update();
		field.Unlink();
	}

	doc.Save(@"C:\Users\543\Downloads\result.doc");
}

Version info:

Aspose.Words for Net 23.6

The demo file

unlinkFields.7z (45.9 KB)

The screenshot

20230617131145.png (67.7 KB)

OS Version Info

Windows 11 Home Chinese Edition

.NET Version Info

Net Framework 4.7

@sullivan By default Aspose.Words uses culture of the current thread when update fields. So you can specify thread culture to get the expected output:

// Set English culture
CultureInfo culture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

Document doc = new Document(@"C:\Temp\in.doc");
            
// Update fields.
doc.UpdateFields();
// Unlink fields in the document.
doc.UnlinkFields();

doc.Save(@"C:\temp\out.doc");

// Resore cultore.
Thread.CurrentThread.CurrentCulture = culture;

Also, you can configure Aspose.Words to use field code culture when update fields using FieldOptions.FieldUpdateCultureSource property

Ok,Thank you!

1 Like