Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Jan 29, 2018 9:56 am

Hi,

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!

carstenp
 
Posts: 5
Joined: Wed Sep 28, 2016 5:49 am

Mon Jan 29, 2018 10:29 am

Hello,

Thanks for your inquiry.
To help us with a better investigation, could you please provide the following information.
1. What are the variables {{ some-key }}, are they fields or just a series of string?
2. Which line throws the exception? Does the "textSections" return null?
3. If you could share your sample document, it couldn't be better.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Mon Jan 29, 2018 10:30 am

The problem was the following line in the textSections loop:
Code: Select all
paragraph.ChildObjects.RemoveAt(index + 1);

I just removed this line and removed the variable after the loop (replaced with string.Empty).

carstenp
 
Posts: 5
Joined: Wed Sep 28, 2016 5:49 am

Tue Jan 30, 2018 1:32 am

Hello,

Thanks for your valuable feedback.
If you want to use the "RemoveAt" method, please use
Code: Select all
paragraph.ChildObjects.RemoveAt(index + 4)
instead. Besides, if there're more than 1 checkbox you are going to insert, you need to make sure the "checkbox.Name" has a different value each time. Here's the code snippet on my end.
Code: Select all
var textSections = doc.FindAllString("{{ " + field.Key + " }}", true, true);
int i = 0;
foreach (var section in textSections)
{
    var checkbox = new Spire.Doc.Fields.CheckBoxFormField(doc);
    checkbox.Name = field.Key+i++.ToString();
    checkbox.Checked = field.checked;

    var range = section.GetAsOneRange();
    var paragraph = range.OwnerParagraph;
    int index = paragraph.ChildObjects.IndexOf(range);
    paragraph.ChildObjects.Insert(index,checkbox);
    paragraph.ChildObjects.RemoveAt(index + 4);
}


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Jan 30, 2018 8:12 am

Thanks! This was exactly the last challenge I had. Use of the same variable name multiple times, would only add one checkbox - but this fixed it!

carstenp
 
Posts: 5
Joined: Wed Sep 28, 2016 5:49 am

Tue Jan 30, 2018 8:17 am

Hi carstenp,

Glad to hear that.
Just feel free to contact us if you need any help.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc