Free Support Forum - aspose.com

Updating Form Field values

Hi,
I would like to update the textbox and checkbox field values in my word document (doc/docx). Is this possible using ASPOSE.Words? if so, could you please post a sample?
Thanks, Siva

Hi,

Thanks for your inquiry.

Please attach your input and target Word documents here for testing. I will investigate as to how you are expecting your final document to be generated like. You can use Microsoft Word to create your target Word document. I will then provide you code to achieve what you're looking for.

For example, one way to access and change the values of these form fields would be as follows:

foreach (FormField formField in doc.Range.FormFields)

{

if (formField.Type == FieldType.FieldFormCheckBox)

formField.Checked = true;

if (formField.Type == FieldType.FieldFormTextInput)

formField.SetTextInputValue("text is modified.");

}


Best regards,

Hi Awais,
Thank you very much for your quick reply.

Please find attached input and output documents. Basically, I wanted to update the same document that I supplied as input, I do not want to create a new document.

Document doc = new Document(@“c:\test\sampleInput.docx”);
foreach (FormField formField in doc.Range.FormFields)
{
if (formField.Type == FieldType.FieldFormCheckBox)
formField.Checked = true;

if (formField.Type == FieldType.FieldFormTextInput)
    formField.SetTextInputValue("text is modified.");

}

doc.UpdateFields(); // Does this work?

Please let me know the code snippet for saving the changes to the same document.

Thanks,
Siva

Hi ,


Thanks for the additional information.

Yes, the same code will work in this case. Here is the code that loads a document into memory, sets the checked status of the check box form field and then finally saves changes to the same document file.

Document
doc = new Document(@“C:\Temp\sample-Input.docx”);

FormField cbxField = doc.Range.FormFields[1];

cbxField.Checked = true;

doc.Save(@"C:\Temp\sample-Input.docx");


I hope, this helps.

Best regards,