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:
insert a signature pic at the Bookmark âSIGN_Linksunterzeichnerâ, the code above works fine for this step
using a blank image to hide the bookmark âSIGN_Rechtunterzeichnerâ, however this bookmark must be kept, and used in the next step
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.
@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:
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.
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.
@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");
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)
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 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.
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â
use âintermediate.docxâ as input, replace âSIGN_Rechtsunterzeichnerâ into âKhalendUnterschrift2.pngâ, then save the result as âfinal.docxâ