Click here to Skip to main content
15,867,488 members
Articles / Productivity Apps and Services / Microsoft Office
Article

Adding An Image to a Word Document Programmatically (Microsoft Office 2007)

Rate me:
Please Sign up or sign in to vote.
3.62/5 (6 votes)
6 Oct 2008CPOL3 min read 81.1K   3K   14   11
This article is about the details of adding an image to a Word document programmatically.

Introduction

This article is about the details of adding an image to a Word document programmatically.

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, they 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 then close it.
    We now have a document with an image placeholder.

I have uploaded a sample Word document with all this done. It can be found in the 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. 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 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 you some fundamentals.

  • 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 "" 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 summarized in three points:

  1. Find the Image tag node: This node is described in the document.xml by 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 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 replaced 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.

History

  • 6th October, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Philips
India India
I am Abey Thomas Raju from Bangalore, India.

More than 7 years of solid experience in designing, developing and maintaining web applications using .NET technologies.
Good knowledge of ASP.NET aspects and related web technologies like Javascript, AJAX, CSS and jquery.

Proven experience in Object oriented analysis and design by developing and maintaining top class back-end framework for successful financial web applications.

Comments and Discussions

 
QuestionWhen two images are there in word doc.....not working as expected Pin
Member 112762471-Dec-14 19:39
Member 112762471-Dec-14 19:39 
QuestionHow to remove the annoying icon - click here to insert a picture Pin
Member 998945316-Apr-13 4:12
Member 998945316-Apr-13 4:12 
AnswerRe: How to remove the annoying icon - click here to insert a picture Pin
travich14-Dec-15 10:03
travich14-Dec-15 10:03 
QuestionMore flexible solution Pin
Johnny Glenn13-Mar-12 23:57
Johnny Glenn13-Mar-12 23:57 
GeneralIterator bug in GetImagePlaceHolderRelationShipId Pin
koffeinfrei8-Sep-09 3:53
koffeinfrei8-Sep-09 3:53 
GeneralRe: Iterator bug in GetImagePlaceHolderRelationShipId Pin
his_dudeness9-Sep-09 1:47
his_dudeness9-Sep-09 1:47 
GeneralRe: Iterator bug in GetImagePlaceHolderRelationShipId Pin
dxl_2k17-Jan-11 10:51
dxl_2k17-Jan-11 10:51 
GeneralRe: Iterator bug in GetImagePlaceHolderRelationShipId Pin
Member 1102558921-Aug-14 5:53
Member 1102558921-Aug-14 5:53 
GeneralThank you for your contribution Pin
ElsaWood15-Jun-09 3:49
ElsaWood15-Jun-09 3:49 
QuestionHow to open the developer tab? Pin
Ricardo Amores Hernández6-Oct-08 22:41
Ricardo Amores Hernández6-Oct-08 22:41 
AnswerRe: How to open the developer tab? Pin
Abey Thomas6-Oct-08 23:28
Abey Thomas6-Oct-08 23:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.