We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace the Form Field in word

need to replace formfield in word file with different other formfield types(like HTML, image, hyperlink).


how can i do this stuff, with Aspose.Word. i have found many solutions but still i didn’t get solution that i want. please help me in this things.

need,
1) Replace form Field.
2) Insert Form Field before another Form Field (need starting pint of another Form Field)

Please give me the solutions for this things ASAP.
Hi Saurabh,

Thanks for your inquiry. Please move the cursor to the form field and insert the content according to your requirement. Please refer to the following article:
Use DocumentBuilder to Insert Document Elements

If you face any issue, please share your input and expected output documents here for our reference. We will then provide you more information about your query along with code.

Ok. Thanks for your reply.

I will try this one. and let you know, if anything is required.

Hello,

I am using below code for adding new form fields and remove old one.

but for second time, it will give me the error like

“System.InvalidOperationException: Document structure was changed.”.

please see below code,

foreach (Aspose.Words.Fields.FormField field in doc.Range.FormFields)
{
    if (field.Type == Aspose.Words.Fields.FieldType.FieldFormTextInput)
    {
        if (field.Name == "xxDWPerPremLmtLab_1")
        {
            Node currentNode = field;
            while (currentNode.NodeType != NodeType.FieldStart)
            {
                currentNode = currentNode.PreviousSibling;
            }
            //move cursor to fieldStrar
            builder.MoveTo(currentNode);
            //Insert image
            builder.InsertImage(FilePath + "test.jpg");
            //Now we should remove field
            while (currentNode.NodeType != NodeType.FieldEnd)
            {
                currentNode = currentNode.NextSibling;
                currentNode.PreviousSibling.Remove();
            }
            //currentNode.Remove();
            doc.UpdateFields();
            doc.Save(FilePath + FileName);
        }
    }
}

please let me know, the solution ASAP.

Hi Saurabh,


Thanks for your feedback. As requested above, please share your sample input and expected output document here. It will help us to investigate your issue exactly and address it accordingly.

We are sorry for the inconvenience.

Best Regards,
got the attachment.

please find the file. in that tahre is one form field that name is "xxDWPerPremLmtLab_1".
i want to replace this form field with any other form field.
please give me the proper solutions ASAP.

Thanks,
Saurabh

Hi Saurabh,

Thanks for sharing the document. Please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "test.rtf");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (FormField formfield in doc.Range.Bookmarks["xxDWPerPremLmtLab_1"].BookmarkStart.ParentNode.Range.FormFields)
{
    if (formfield.Name == "xxDWPerPremLmtLab_1")
    {
        doc.Range.Bookmarks["xxDWPerPremLmtLab_1"].Text = "";
        builder.MoveToBookmark("xxDWPerPremLmtLab_1");
        builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Hello", 0);
        break;
    }
}

doc.Save(MyDir + "17.5.docx");