I'm trying to replace a text variable with an CheckBoxFormField. When the Spire.Doc object is disposed, i get a null exception:
- Code: Select all
Object reference not set to an instance of an object.
at Spire.Doc.Fields.FormField.Detach()
at Spire.Doc.Collections.ParagraphItemCollection.OnClear()
at Spire.Doc.Collections.DocumentObjectCollection.Clear()
at Spire.Doc.Documents.Paragraph.Close()
at Spire.Doc.Body.ᜂ()
at Spire.Doc.Section.ᜁ()
at Spire.Doc.Document.ᜐ()
at Spire.Doc.Document.ᜑ()
at Spire.Doc.Document.ᜁ(Boolean A_0)
at Spire.Doc.Document.Dispose()
I'm using the code below. Find all variables {{ some-key }}, and Replace it with a checkbox. Each checkbox will have a unique Name.
- Code: Select all
var checkbox = new Spire.Doc.Fields.CheckBoxFormField(doc);
checkbox.Name = field.Key;
checkbox.Checked = field.checked;
var textSections = doc.FindAllString("{{ " + field.Key + " }}", true, true);
foreach (var section in textSections)
{
var range = section.GetAsOneRange();
var paragraph = range.OwnerParagraph;
int index = paragraph.ChildObjects.IndexOf(range);
paragraph.ChildObjects.Insert(index, checkbox);
paragraph.ChildObjects.RemoveAt(index + 1);
}
Shouldn't this be possible? The checked state is set by the code, and the checkbox should only be a visual element (instead of yes/no) in the Word document.
Thanks in advance!