Bookmark Images

We are assessing your DocX API to see if we could use it to replace a lot if interop DocX code we currently have.

One of the requirements is to insert an image into predefined bookmark sections. I’ve been talking to your chatbot and its

given me a few different ways to do this… all of them wouldn’t compile. I’m trying to load a docx file find a bookmark and insert

an image in the bookmark. The highlighted code is what I received from the bot, I’ve added comments to detail the problems encountered.

     string pictPath = @"C:\\RSIGDocs\Signature.png";

     Bookmark bookmark = doc.Bookmarks["SignatureBlock"];

     if (bookmark != null)
        // Create a Picture object using the Image object
        Picture picture = new Picture(doc, pictPath);

        // Insert the Picture object into the bookmark

        // Code from Internet:
        //bookmark.BookmarkStart.ParentCollection.InsertAfter(paragraph, bookmark.BookmarkStart);

        // Code Modified by me. There is NO InsertAfter
        //bookmark.Start.ParentCollection.InsertAfter(bookmark.Start, picture.Content);

        // 2nd round of code from Internet
        // Error: Can't convert Content range to string

        // Create a Run object and add the Picture object to it
        Run run = new Run(doc, picture.Content);

        // Replace the bookmark content with the Run object

        // ReplaceChile does not exist!
        bookmark.Start.Parent.ReplaceChild(run, bookmark.Start);

        //// Remove the bookmark



What “chatbot” have you been talking to?

Nevertheless, here is how you can do this with GemBox.Document:

if (bookmark != null)
    Picture picture = new Picture(doc, pictPath);

If you want the bookmark to be removed, just change the bookmark.GetContent(false) to bookmark.GetContent(true).

I hope this helps.


I used ChatGPT for this task :slight_smile: