简体   繁体   中英

How can I update all fields (cross-reference) with OpenXML in C#?

In Word there is an option "Update field(s)". I want to load a Word-Template and fill the FormFields with some data. There are also fields which are references to other fields.

With Interop I can simply write "UpdateAllFields", but what's the equivalent in OpenXML?

Thank you :)

You want to use the Open XML SDK to update fields,right? Here's some sample code:

using (WordprocessingDocument document = WordprocessingDocument.Open(path, true))
{
  DocumentSettingsPart settingsPart = document.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
  // Create object to update fields on open
  UpdateFieldsOnOpen updateFields = new UpdateFieldsOnOpen();
  updateFields.Val = new DocumentFormat.OpenXml.OnOffValue(true);
  // Insert object into settings part.
  settingsPart.Settings.PrependChild<UpdateFieldsOnOpen>(updateFields);
  settingsPart.Settings.Save();
}

For more information, please refer the link below:

OpenXML: How to refresh a field when the document is opened

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM