facebooktwittermenuarrow-up

GemBox Support Forum

Modifying Bookmark content when embedded in IF fields

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 :slight_smile:

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.ElementCollection1.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,

Hi,

I’m afraid that ContentRange (and ContentPosition) is not intended for such usage, for processing field’s instruction text.

You see, this API flattens the document content, it enables you to view the document as if it’s a contiguous sequence of characters by hiding the complexity of the underlying hierarchical content model.

As such, it’s can be used for seamless processing of the field’s result because they’re the part of the content, but not the field’s instruction or code. To process the instruction, you’ll need to manipulate directly with Field.InstructionInlines collection.

I hope this helps, please let me know if you need anything else.

Regards,
Mario

Hi Mario,

That all makes sense. I’ll explore the InstructionInLines property to see how we can manipulate the bookmark content.

Many Thanks,