With Spire.Doc for .NET, developers can easily operate the word fields from code. We have already shown how to create an IF field and remove Custom Property Fields in C#. From Spire.Doc V5.8.33, our developers add a new event UpdateFields to handle the Ask Field. This article will focus on demonstrating how to update the ASK field on the word document in C#.
Firstly, please view the sample document with an Ask filed which will be updated later:
Step 1: Create a new instance of Spire.Doc.Document class and load the document from file.
Document doc = new Document(); doc.LoadFromFile("Sample.docx");
Step 2: Call UpdateFieldsHandler event to update the ASK field.
doc.UpdateFields += new UpdateFieldsHandler(doc_UpdateFields);
Step 3: Update the fields in the document.
doc.IsUpdateFields = true;
Step 4: Save the document to file.
doc.SaveToFile("output.docx", FileFormat.Docx);
The following doc_UpdateFields () method shows how to update the ask field:
private static void doc_UpdateFields(object sender, IFieldsEventArgs args) { if (args is AskFieldEventArgs) { AskFieldEventArgs askArgs = args as AskFieldEventArgs; askArgs.ResponseText = "Female"; } }
Effective screenshot after updating the Ask Field in C#:
Full codes:
using Spire.Doc; using Spire.Doc.Fields; namespace Askfield { class Program { public void Field() { Document doc = new Document(); doc.LoadFromFile("Sample.docx"); doc.UpdateFields += new UpdateFieldsHandler(doc_UpdateFields); doc.IsUpdateFields = true; doc.SaveToFile("output.docx", FileFormat.Docx); } private static void doc_UpdateFields(object sender, IFieldsEventArgs args) { if (args is AskFieldEventArgs) { AskFieldEventArgs askArgs = args as AskFieldEventArgs; askArgs.ResponseText = "Female"; } } } }