Header Picture Disappears When Replacing Bookmark Text

Hi,

I encountered an issue with a simple docx containing a header with two bookmarks (Bookmark1 and Bookmark2) and a picture.

When I replace the text of Bookmark1, the picture disappears.
However, this issue does not occur when replacing Bookmark2.

Here’s the code I’m using to replace the bookmark text:

document.Bookmarks["Bookmark1"].GetContent(false).LoadText("Bookmark1 replaced by this.");

To demonstrate the behavior, I’ve created a minimal example, which you can find here: GitHub - sengelps/gemboxHeaderBookmark


=>

Regards,
Simon

Hi Simon,

That is a floating Picture element anchored inside the “Bookmark1” bookmark.
In other words, see this:

DocumentModel document = DocumentModel.Load("headerExample.docx");

var header = document.Sections[0].HeadersFooters[HeaderFooterType.HeaderDefault];
var paragraph = header.Blocks[0] as Paragraph;

foreach (var inline in paragraph.Inlines)
    Console.WriteLine(inline.ElementType);

The result is:

BookmarkStart
Picture
Run
BookmarkEnd

Another way you may notice that this Picture is inside the “Bookmark1” bookmark is to change its layout from floating to inline:

image

I hope this helps.

Regards,
Mario

Hi Mario,

thanks for shedding light on this issue!
Your explanation was helpful in understanding the structure of the document.
When moving the picture’s object anchor, I can replace the bookmark text without losing the image.

Regards,
Simon