I am writing an application which should use DocumentFormat.OpenXML SDK for writing data to form fields in a word template. But I cannot find a property in the document-object of the SDK where the form fields are stored.
I tried this code:
using (WordprocessingDocument document = WordprocessingDocument.Open("Path/To/document.dotx", true))
{
document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
MainDocumentPart mainPart = document.MainDocumentPart;
var fields = mainPart.Document.Body.Descendants<FormFieldData>();
foreach (var field in fields)
{
if (field.GetType() == typeof(FormFieldData))
{
if (field.LocalName == "Name")
{
Console.WriteLine("Hi!");
}
}
}
}
But fields is always null.