wordpress hit counter
Adding Image to Word document’s Header - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

Adding Image to Word document’s Header

Adding Image to Word document’s Header

Rate This
  • Comments 16

Article By - Sheela E.N, Sonata Software Limited

 

This article explains how to add image to header of a word document using Open XML SDK API. The steps to be followed are as below –

 

1.      Create a C# project; add a reference to openxml dll

 

·         In the Add Reference dialog box, click the .NET tab.

·         Scroll to the Microsoft.Office.DocumentFormat.OpenXml option, select it, and then click OK.

 

2.      Import the namespace to refer to the dll

 

using Microsoft.Office.DocumentFormat.OpenXml.Packaging;

 

3.      Declare necessary namespaces

 

const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

 

const string relationshipNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";

 

4.      Open the word document to get the main document part.

 

WordprocessingDocument wordDoc = WordprocessingDocument.Open(@"C:\test\test.docx", true);

MainDocumentPart mainPart = wordDoc.MainDocumentPart;

 

5.      Load the xml of main part to DOM object to change the IDs at a later stage

 

XmlDocument xDoc = new XmlDocument();

 xDoc.Load(mainPart.GetStream());

 

6.      Add a header part to the main document part

 

HeaderPart headPart = mainPart.AddNewPart<HeaderPart>();

 

7.      Either construct xml file for the header or load the existing template.  Here, I am loading existing xml -

 

XmlDocument header = new XmlDocument();

 header.Load(@"..\..\HeaderWithImage.xml");

 

8.      Save the xml file into header part

 

header.Save(headPart.GetStream());

 

9.      Add image part to the header part

 

ImagePart imgpart = headPart.AddImagePart(ImagePartType.Gif);

 

10.  Load image into the image part

 

using (Stream targetStream = imgpart.GetStream())

                {

                    using (FileStream sourceStream = new FileStream("../../officelogo.gif",

                            FileMode.Open, FileAccess.Read))

                    {

                        byte[] buffer = new byte[1024];

                        int nrBytesWritten = sourceStream.Read(buffer, 0, 1024);

                        while (nrBytesWritten > 0)

                        {

                            targetStream.Write(buffer, 0, nrBytesWritten);

                            nrBytesWritten = sourceStream.Read(buffer, 0, 1024);

                        }

                    }

                }

 

11.  Get the relationship ids of header part and image part just created

 

string imgId = headPart.GetIdOfPart(imgpart);

string relId = mainPart.GetIdOfPart(headPart);

 

12.   Change the relationship IDs in both header.xml and document.xml file

 

XmlNode blip = header.SelectSingleNode("//a:blip", nsManager1);

 XmlAttribute embed = blip.Attributes["embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"];

                embed.Value = imgId;

 

XmlNode targetNode = xDoc.SelectSingleNode("//w:sectPr", nsManager);

 

XmlElement node = xDoc.CreateElement(reference, wordmlNamespace);

 

XmlAttribute attr = node.Attributes.Append(xDoc.CreateAttribute("r:id", relationshipNamespace));

 

attr.Value = relId;

 

13.   Save document and header parts

 

header.Save(headPart.GetStream());

xDoc.Save(mainPart.GetStream());

 

14.  Close the word handler

 

wordDoc.close();

 

This is a simple demo which demonstrates ease of use of Open XML SDK for adding image to the header part in word document. The example demo is attached with the article as a zip file.
Attachment: ImagePartToHeaderPart.zip
Page 1 of 2 (16 items) 12