Click here to Skip to main content
15,889,909 members
Articles / Productivity Apps and Services / Microsoft Office

Adding footnotes to a Word document programatically (MS Office 2007 Word)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
2 Oct 2008CPOL3 min read 40.9K   633   13   3
This is an article about adding footnotes to a Word document (MS Office 2007) programmatically.

Introduction

This is an article about adding footnotes to a Word document programmatically. I had this as one of my requirements while generating Word documents dynamically, and I really struggled to implement it, as well as to find some help online. So I thought I would share my experience with the community about this so that someone might find this helpful.

I assume the readers of this article should have knowledge of how to view a Word document (MS Office 2007) archive.

Implementation

Let's assume some text in a Word document, say template.docx:

This is a sample text <FN1>

I assume that the Word document contains this text (already), and the tag <FN1>, should be replaced with superscript "1", and there should be a corresponding footnote in the footer section. The XML representation of this text in document.xml looks like this (should be in this form):

XML
<w:p w:rsidR="00F54CB0" w:rsidRDefault="0016203E">
<w:r>
<w:t>Sample Text with footnote </w:t>
</w:r>
<w:r>
<w:t><FN1></w:t>        
</w:r>
</w:p>

Note that the tag <FN1> should be in a separate <w:r>><w:t> node. I assume this as a prerequisite for this logic to work. (This can be done manually in document.xml, or it should be done programmatically.)

Other prerequisites

  1. footernotes.xml should be a part of the Word archive.
  2. endnotes.xml should be a part of the word archive.
  3. [Content_Types].xml should reflect the above two XML files.
  4. word\_rels\document.xml.rels should reflect the above two XML files.
  5. Styles.xml should contain the nodes corresponding to the foot note font styles.

These things can be done either manually, or by opening the Word document and adding the footer section to the document. Once this is done, all the above changes will be automatically done.

I have attached the sample code, along with a sample document (template.docx) in which there is only an <FN1> raw tag, and all the above prerequisites are met.

Adding footnotes

Adding footnotes programmatically to the document consists of two steps:

  1. Replacing the <FN1> tag with the footnote node in document.xml (which consists of a superscript 1, and its references to footernotes.xml).
  2. Adding the footnote text node in footernotes.xml.

Step 1 consists of creating a <w:r> <w:t> node which corresponds to the Word footnotes syntax.

This is done in the CreateFootNoteNode(..) method in the code:

XML
<w:r>
 <w:rpr>
  <w:rStyle w:val="FootnoteReference" />
 <w:rpr>
 <w:footnoteReference w:customMarkFollows="1" w:id="3" />
 <w:t>1</w:t>
</w:r>

Step 2 consists of creating the footnotes related references in Footernotes.xml. This step creates a <footnote> node which consists of the superscript number and the actual footnote. The generated node should look something like this:

XML
<w:footnote w:id="3">
    <w:p>
      <w:pPr>
        <w:pStyle w:val="FootnoteText" />
      </w:pPr>
      <w:r>
        <w:rpr>
          <w:rStyle w:val="FootnoteReference" />
        </w:rpr>
        <w:t>1</w:t>
      </w:r>
      <w:r>
        <w:t>This is a footnote</w:t>
      </w:r>
    </w:p>
</w:footnote>

Once these two nodes are created, the footnote is ready. document.xml and footernotes.xml should be saved.

I have put a sample document with raw tags <fn1> in the bin/debug folder of the solution. Once the program is executed once, the document will reflect the footnotes correctly.

Notes:

This section briefly explains the prerequisites in the following files:

  1. [Content_Types].xml: The following section is added:
  2. XML
    <Override PartName="/word/endnotes.xml" 
      contentType="application/vnd.openxmlformats-officedocument.
                   wordprocessingml.endnotes+xml"/>
    <Override PartName="/word/footnotes.xml" 
      ContentType="application/vnd.openxmlformats-officedocument.
                   wordprocessingml.footnotes+xml"/>
  3. word\_rels\document.xml.rels: The following section is added:
  4. XML
    <Relationship Id="rId6" 
      Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" 
      Target="endnotes.xml"/>
    <Relationship Id="rId7" 
      Type="http://schemas.openxmlformats.org/
            officeDocument/2006/relationships/footnotes" 
      Target="footnotes.xml"/>

I have uploaded a sample application here, where the whole operation of dynamically creating footnotes in a Word document is done. It contains a file "sample template\template.docx". This file has to be put into the bin/debug folder of the application. Before executing, have a look at the document. It will contain the raw footnote placeholders. Once the program is executed, the placeholder is replaced by the footnote number, and the corresponding footnote can be seen in the footer section of the document.

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

 
Questionbenefit? Pin
alireza124631-Jul-13 7:23
professionalalireza124631-Jul-13 7:23 
AnswerRe: benefit? Pin
Abey Thomas31-Jul-13 18:29
Abey Thomas31-Jul-13 18:29 
Thank you for your question alireza1246.

Yes, your question is very valid, especially if you are new to OpenXML technology. I will try to answer your question without going into too much technical details, and with a bit of history.

Before Microsoft released MS Office 2007, all their Office products, say WORD, PPT, EXCEL, etc all were in binary format. This was a major problem back then, because there is a section of people in the software industry where they tried to automate the generation of MS Office documents (WORD, EXCEL or PPT). Since the APIs provided by Microsoft to create/manipulate Office documents had several shortcomings - very less documentation on the APIs, too little features, huge performance and scalability issues, and it was never designed to work on the "server" side in client-server system, to name a few.

When Microsoft released MS Office 2007, they totally redesigned the whole stuff and came up with a new architecture which the Office Products were internally based on - OpenXML technology. I hope you remember this when they came up with new file extensions - .docx, pptx or xlsx. In short - the Office documents, be it WORD, EXCEL or PPT, they were all XML files internally. Infact, they were all a bunch of xml files which were zipped. You can infact verify that by simply renaming a .docx file to .docx.zip and then extracting it using a zip utility.

So this new architecture opened up a new world of opportunities to certain section of people who were doing the stuff I mentioned earlier. ( I was fortunately one of them) - What it meant for us is we could modify a bunch of XML files to create/manipulate office documents, and there were sufficient Open XML APIs available for that.

So coming to specifics of your question - this section of the industry required to create WORD, EXCEL documents on the "fly" (programmatically) based on certain inputs available at runtime. This meant creating pages, table of contents, headers and footers, inserting images, inserting tables, footnotes and so on. Whatever a user can do manually with a WORD document, had to be done programmatically to generate beautiful WORD documents, which finally went to the customers. This article was one my experiences on creating footnotes in a WORD document programmatically.

Whew! that was almost 5 years ago, I left this in 2009-2010 - the technology must have matured by now - and there might be various improvements in the OpenXML API to do the stuff what I do here in a more easier way - but this was it then Smile | :)

Hope I have answered your question sufficiently. Let me know if you have any more questions.
- Abey


modified 2-Aug-13 1:23am.

AnswerRe: benefit? Pin
alireza12464-Aug-13 7:14
professionalalireza12464-Aug-13 7:14 

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.