Dynamically update all document fields

Hello,

I need to update all fields in a document without knowing exactly which fields are contained. At a minimum, each document will contain saveDate fields, table of contents, various fields in the headers and footers, and cross-references/hyper-links, section names, etc.

What is the best way to get a list of all the fields in a document to update them?

Hi,

If you’re saving the document as PDF then you only need to update TOC elements, Field elements will be updated automatically.

If you’re saving the document as DOCX, then try this:

var document = DocumentModel.Load("input.docx");

foreach (var element in document.GetChildElements(true, ElementType.Field | ElementType.TableOfEntries))
{
    if (element is Field field)
        field.Update();
    else if (element is TableOfEntries tableOfEntries)
        tableOfEntries.Update();
}

document.GetPaginator(new PaginatorOptions() { UpdateFields = true });

document.Save("output.docx");

I hope this helps.

Regards,
Mario