How to insert an image into a Docx File at a bookmark, yet WITHOUT LOSING this bookmark?

Hallo, I am using Aspose TOTAL Licensed edition 24.4.1 for Java.

Now I get such a serious Problem:

We have a Word Docx as Template,
dc_973233292486874670100080000050569ceb85.docx.docx.docx (55,3 KB)

We use German language Office, and in MS-Word, you can see, there are three Bookmarks:
FIRMA_LOGO
SIGN_Linksunterzeichner
SIGN_Rechtunterzeichner

We use following code to insert an Image at the Image Bookmark

    public static void docxBookmarkImagefilling(final String template, final String bookmarkName, final String imageLocation, final String bookmarkText, final String outputDocx) throws Exception
    {
        final Document doc = new Document(template);
        final DocumentBuilder builder = new DocumentBuilder(doc);

        final Bookmark bm = doc.getRange().getBookmarks().get(bookmarkName);
        if (bm != null)
        {
            builder.moveTo(bm.getBookmarkStart());
            builder.insertImage(imageLocation);
            if (bookmarkText != null)
            {
                bm.setText(bookmarkText);
            }
        }

        doc.save(outputDocx, SaveFormat.DOCX);
    }

For normal inserting Image at a bookmark, it works fine. Actually we use it for signature.

However, now we want to achieve such an effect:

  1. insert a signature pic at the Bookmark “SIGN_Linksunterzeichner”, the code above works fine for this step
  2. using a blank image to hide the bookmark “SIGN_Rechtunterzeichner”, however this bookmark must be kept, and used in the next step
  3. insert another signature pic at the Bookmark “SIGN_Rechtunterzeichner”

The problem of the current java code is: once we use it for step 2, then the bookmark “SIGN_Rechtunterzeichner” will get lost, and we can not start the step 3.

Is there anyway to do that?

@zwei You can use the following code to insert text and image inside bookmark:

String bookmarkName = "bkName";
String bookmarkText = "Some Text";
final Document doc = new Document("C:\\Temp\\in.docx");
final DocumentBuilder builder = new DocumentBuilder(doc);

final Bookmark bm = doc.getRange().getBookmarks().get(bookmarkName);
if (bm != null)
{
    // Remove bookmarkText
    bm.setText("");
    // Move builder inside bookmark.
    builder.moveToBookmark(bookmarkName, true, true);
    // Insert text 
    if (bookmarkText != null)
        builder.write(bookmarkText);
    // Insert an image
    builder.insertImage("C:\\Temp\\img.png");
}

doc.save("C:\\Temp\\out.docx");
1 Like

thank you very much, I will try it, and I will share the result here.

1 Like

What a pity, the new code doesn’t work at all, it can even not insert the image into bookmarks, and the processed bookmark get lost also.

Soon I will post several examples Docx Files here, they will help to understand this problem.

@zwei The code work fine with the document attached in the initial post on my side. I have tested with SIGN_Linksunterzeichner bookmark.
Before processing:


After processing:

We need to insert an image into this bookmark, my old code cause the lost of the bookmark, however the image is there. Unforunately, the new code could even not insert the image.

@zwei I cannot reproduce the problem on my side. Both image and text are inserted inside the bookmark and the bookmark is still there.

try to open it in MS-Word, or export it into PDF, we can not see the inserted image at the bookmark

Here it is result of our code
TestResultWithOldCode.docx (52,9 KB)

Here it is result of your advice code
TestResultWithAlexCode.docx (48,5 KB)

You may open them in MS-Word, and you will see, the signature image get added successfully in old code, but failed in new code.

Soon I will post the desired result.

Here it is the desired result, the desired result was generated by non-Aspose technology.

However, my old code and your new code could not achieve this desired effect.
desired.docx (56,8 KB)

the main difference is that the third bookmark still get kept, if you open the file in MS-Word, and activate the third bookmark, you can still show the bookmark in Word.

this is the result of my old code, as you can see, although the first signature image get inserted correctly, however the third bookmark disappeared.

and here it is the result of your code, it even failed to insert the first signature image, and the third bookmark disappeared also

@zwei Thank you for additional information. Unfortunately, I still cannot reproduce the problem on my side. The images are properly insert into the bookmarks and the bookmarks remains in the output document. Here is a simplified code and the result produced by it on my side:

String[] bookmarkNames = new String[] { "SIGN_Linksunterzeichner", "SIGN_Rechtsunterzeichner" };
Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

for (String bookmarkName : bookmarkNames)
{
    Bookmark bm = doc.getRange().getBookmarks().get(bookmarkName);
    if (bm != null)
    {
        // Remove bookmarkText
        bm.setText("");
        // Move builder inside bookmark.
        builder.moveToBookmark(bookmarkName, true, true);
        // Insert an image
        builder.insertImage("C:\\Temp\\img.png", 181, 56);
    }
}

doc.save("C:\\Temp\\out.docx");

out.docx (55.1 KB)

Thank you very much, can you try to replace the third Bookmark of your result file into another image?

Please just use the output of your last test (to ensure that two signatures get filled already) and REPLACE the third bookmark into this one.
KhalendUnterschrift2.png (5,4 KB)

@zwei Here is the modified code and the output:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

int index = 0;
for (Bookmark bm: doc.getRange().getBookmarks())
{
    if (bm != null)
    {
        // Remove bookmarkText
        bm.setText("");
        // Move builder inside bookmark.
        builder.moveToBookmark(bm.getName(), true, true);
        // Insert an image
        builder.insertImage("C:\\Temp\\img" + index + ".png", 181, 56);
        index++;
    }
}

doc.save("C:\\Temp\\out.docx");

out.docx (86.0 KB)

It seems that you get some misunderstanding.

Now we should try to replace rather than insert

In the input Docx File, two Bookmarks should get filled already, and we should try to replace one of them into another image, but NOT inserting two images at one empty template.

@zwei I have used the output document as an input and used the same code to insert new images and still everything works as expected:

Document doc = new Document("C:\\Temp\\out.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

for (Bookmark bm: doc.getRange().getBookmarks())
{
    if (bm != null)
    {
        // Remove bookmarkText
        bm.setText("");
        // Move builder inside bookmark.
        builder.moveToBookmark(bm.getName(), true, true);
        // Insert an image
        builder.insertImage("C:\\Temp\\img.png", 181, 56);
    }
}

doc.save("C:\\Temp\\out_2.docx");

out_2.docx (55.1 KB)

Here it is my test result with your code, the Image doesn’t appear at bookmark at all.

ronaldo-temp.docx (44,7 KB)

@zwei There is no content between bookmark start and end in your document. Please make sure you do not set bookmark’s text to empty string somewhere in your code.

Let’s try again:

  1. use this template file, and insert the “u1.png” at “SIGN_Linksunterzeichner” and “u2.png” at “SIGN_Rechtsunterzeichner”, then save the result as “intermediate.docx”

Prämienzahlung_einmalig_ohne_bild(PS#22.02.) (1).docx (43,7 KB)

u1.png (1,9 KB)

u2.png (1,8 KB)

  1. use “intermediate.docx” as input, replace “SIGN_Rechtsunterzeichner” into “KhalendUnterschrift2.png”, then save the result as “final.docx”

KhalendUnterschrift2.png (5,4 KB)

  1. post “intermediate.docx” and “final.docx” here along with all source codes.

Thank you very much! Problem is, in my sandbox, the new code never works to insert image at bookmark.

I am using Aspose Total 24.4.1 licensed edition.

@zwei Sure, here is the code that implements what you have described:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// 1. Replace bookmark SIGN_Linksunterzeichner
Bookmark bm1 = doc.getRange().getBookmarks().get("SIGN_Linksunterzeichner");
// Remove bookmarkText
bm1.setText("");
// Move builder inside bookmark.
builder.moveToBookmark(bm1.getName(), true, true);
// Insert an image
builder.insertImage("C:\\Temp\\u1.png", 181, 56);

// 2. Replace bookmark SIGN_Rechtsunterzeichner
Bookmark bm2 = doc.getRange().getBookmarks().get("SIGN_Rechtsunterzeichner");
bm2.setText("");
builder.moveToBookmark(bm2.getName(), true, true);
builder.insertImage("C:\\Temp\\u2.png", 181, 56);

// Save intermediate document.
doc.save("C:\\temp\\intermediate.docx");

// Load intermediate document.
Document doc1 = new Document("C:\\temp\\intermediate.docx");
DocumentBuilder builder1 = new DocumentBuilder(doc1);
// 3. Replace bookmark SIGN_Rechtsunterzeichner
Bookmark bm3 = doc1.getRange().getBookmarks().get("SIGN_Rechtsunterzeichner");
bm3.setText("");
builder1.moveToBookmark(bm3.getName(), true, true);
builder1.insertImage("C:\\Temp\\KhalendUnterschrift2.png", 181, 56);

// Save the final document.
doc1.save("C:\\Temp\\final.docx");

final.docx (42.2 KB)
intermediate.docx (38.7 KB)

I used the latest 24.7 version of Aspose.Words for Java.

1 Like

After update to 24.7, your example code works fine by me now!

It seems that my old 24.4.1 lib was the cause.

Thank you very much Alexey! ХпасийО!

1 Like