I'm going crazy trying to find how to insert pictures in my bookmarks...
For the moment I have no problems with insert text or tables: I find bookmarks and insert in that position like John's way: Replace bookmark text in Word file using Open XML SDK
Now I want to send images to this bookmarks. I'm reading articles like:
http://msdn.microsoft.com/en-us/library/bb497430(office.14).aspx
http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/6d9066db-a154-475d-9731-944c8ce13e67/
http://msdn.microsoft.com/en-us/library/ee342530.aspx
...but I can't do it work with my template dotx and my bookmarks. Some ideas?
Here is the code I am using to insert the paragraph in my bookmark:
Run runImg = new Run();
runImg.Append(element);
Paragraph parImg = new Paragraph();
parImg.Append(runImg);
foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
{
if (bookmarkStart.Name.Value == _nomBM)
{
bookmarkStart.FirstChild.PrependChild(parImg);
}
}
Thanks!
Inserting a picture in a bookmark should work as if you are inserting a picture into the word document itself. Any of those above links should show you how to insert the picture correctly. The key is to find the bookmark you want to insert it in and making sure you insert the paragraph that contains the picture in between the <w:bookmarkStart>
and <w:bookmarkEnd>
elements. If this is what you are doing and you are still having issues, post your code so we can take a look.
EDIT
After seeing your code the problem is the <w:bookmarkStart>
element is a child of the <w:p>
element. You want to find the parent of the <w:bookmarkStart>
which will be the <w:p>
element and then insert your image paragraph as the next element using something like this:
bookmarkStart.Parent.InsertAfterSelf<Paragraph>(parImg);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With