3

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:

...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!

Community
  • 1
  • 1
Displaying
  • 117
  • 3
  • 8

2 Answers2

4

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);
amurra
  • 15,221
  • 4
  • 70
  • 87
  • OK, I know... look, if i do: document.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element))); where element is the image with all the propiets and formats... works great, and the image is inserted in the main document. But when i want to insert in my bookmark I write like this: – Displaying May 19 '11 at 08:21
  • 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); } } DOESEN'T WORK – Displaying May 19 '11 at 08:23
  • @Displaying - That's because the workbook part start element is a child of the paragraph element. Once you find the start bookmark element you want you get its parent and then add the image paragraph as the next element. Something like `bookmarkStart.Parent.InsertAfterSelf(parImg);` See my edit for the same info. – amurra May 19 '11 at 11:39
  • My last wish is to put this image into the bookmark (not in a paragraph "After" or "Before") ... maybe in this way your image position depends on where the bookmark is – Displaying May 20 '11 at 09:00
  • @displaying - for a paragraph to be in a bookmark it has to be in between the bookmarkstart and bookmarkend elements. Inserting after should put it in-between the two. If not find the bookmarkend element and insert your paragraph before this element. – amurra May 20 '11 at 15:15
  • Still having a problem: I have achieved the insertion between bookmarkstart/end, but when the insertion is in a bookmark with nothing before, give a format error (document doesn't open) – Displaying Jun 02 '11 at 09:31
  • @Displaying - What do you mean by "insertion is in a bookmark with nothing before", nothing before the bookmark? – amurra Jun 08 '11 at 12:11
  • I had solve this problem! Thanks for your interest @amurra I will update my question when I can – Displaying Jun 09 '11 at 15:29
0

I know it is too late but try the below you may get bit closer to the bookmark position

bookmarkStart.Parent.InsertBeforeSelf<Paragraph>(parImg);

Verybiztalker
  • 75
  • 1
  • 7