Hi,
I am running into an ArgumentOutOfRangeException
when attempting to use ContentRange.LoadText
on a Bookmark
located within an IF field.
This can be easily replicated by creating a new .docx with the following content:
{ IF “[SampleBookmark]” > “0” “test” “test1” }
Where [SampleBookmark] is a bookmark and not simply content with square brackets
Sample Code:
DocumentModel document = DocumentModel.Load(file);
foreach (var bookmark in document.Bookmarks)
{
var content = bookmark.GetContent(false);
var bookmarkText = "sampleText";
content.LoadText(bookmarkText);
Debug.WriteLine($"Bookmark Name: {bookmark.Name}, New Value: {bookmarkText}");
}
Exception Message:
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter ‘index’)
Exception Call Stack:
Call Stack
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
at System.Collections.Generic.List1.get_Item(Int32 index) at GemBox.Document.ElementCollection
1.get_Item(Int32 index)
at GemBox.Document.ElementCollection`1.GetCore(Int32 index)
at GemBox.Document.ElementCollection.get_Item(Int32 index)
at .()
at .()
at GemBox.Document.ContentRange.()
at GemBox.Document.ContentRange.LoadText(String text, CharacterFormat format)
at GemBox.Document.ContentRange.LoadText(String text)
For some of our more complex documents (e.g. multiple bookmarks contained within a single IF field definition), the problem doesn’t appear to impact all the bookmarks contained in a single IF field definition.
Is there an alternative mechanism through which I could pull back the content range of the bookmark and subsequently call LoadText that would not cause the same exception to be thrown?
Any advice would be much appreciated.
Cheers,