Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert pictures in bookmarks (OpenXML)

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!

like image 637
Displaying Avatar asked Sep 06 '25 03:09

Displaying


1 Answers

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);
like image 108
amurra Avatar answered Sep 07 '25 22:09

amurra