Introduction
This article is about the details of adding an image to a word document programatically.
Background
Let us assume a sample document, which contains an image placeholder with some tag value. For those who know how to add an image placeholder to a word document, can skip the next portion, and directly look at the implementation details.
How to add an image placeholder to a word document
Open a word document.
Go to the 'Developer' tab.
Click on the icon which says "Picture Content Control"
Once the icon is clicked, an image placeholder is added to the word document. Select the placeholder, and click "properties" button next to the icon.
This will open up the window "Content Control Properties". Add a title and the tag name value. The tag name of the image place holder used in this article source code is "ImagePlaceHolder1".
Now save the document and the close it.
We now have a document with an image placeholder. I have uploaded a sample word document with all this done, and can be found in "DocxImages\DocxImages\SampleTemplate folder.
Now this is just a place holder. Our requirement is to embed an image (of our choice) at run time to this place holder. The remaining part of the article deals with this issue.
Embedding an image to a placeholder on the fly
Now we have the template word document. And assume that we have an image file already. For the sake of simplicity, i am assuming a static image file which is already available with me. In real time scenarios, it can be a dynamically created images like charts, etc.
I have placed both the template and the sample image in the programs bin/debug folder.
Before going into the implementation details, let me give u some fundas
In the word 2007 archive, there is a folder "media" where all the image files are stored.
Also, every image is described by a relationship id. This number along with the image path is described in the file "word\_rels\document.xml.rels"
The main document xml file , i.e document.xml will contain a node "<w:sdt> </w:sdt>" which describes the image placeholder. The relationship id will be referred in this node.
Implementation details
With what is said above, embedding an image on the fly is a very easy process. It can be summarised in 3 points
1. Find the Image tag node: This node is described in the document.xml by <w:sdt> node. The document.xml should be searched for this node with tag name using XPathNodeIterator.
2. Find the relationship id of the image node: Once the image placeholder node is identified, the relationship id of this node should be identified. The relationship id is found as a part of the <pic:blipfill><a:blip></a:blip></pic:blipfill> node attribute..
3. The image referred to by the relationship id should be replaced by our new image: Once the image relationship id is found, the corresponding image in the media folder should be relaced with our new image.
The source code of the whole process is uploaded with this article and can be found here. Put the sample template document, and the sample image in the bin/debug folder of the program, and execute it.