Click here to Skip to main content
6,306,412 members and growing! (23,940 online)
Email Password   helpLost your password?
Enterprise Systems » Office Development » Microsoft Word     Intermediate License: The Code Project Open License (CPOL)

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

By Abey Thomas

This is an article about adding footnotes to a word document (MS Office 2007) programatically.
C# (C# 2.0, C# 3.0), XML, Windows (WinXP), .NET (.NET 2.0, .NET 3.0, .NET 3.5), Office, ASP.NET, Visual Studio (VS2005, VS2008), COM+, Dev, QA, Design
Posted:2 Oct 2008
Views:5,650
Bookmarked:10 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
2 votes for this article.
Popularity: 1.05 Rating: 3.50 out of 5

1

2
1 vote, 50.0%
3
1 vote, 50.0%
4

5

Introduction

This is an article about adding footnotes to a word document programatically. I had this as one of my requirement while generating word documents dynamically, and i really struggled to acheive it, as well as 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 the word document (MS Office 2007) archive.

Implementation

Lets assume a text in the 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) as shown below

 
<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 seperate <w:r>><w:t> node. I assume this as a prerequisite for this logic to work. (This can be done manually in the document.xml or it should be done programatically.)

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 to xml files
4. word\_rels\document.xml.rels should reflect the above to xml files
5. Styles.xml should contain the nodes corresponding to the foot notes font styles

These things can be done either manually, or 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 met.

Adding footnotes

Adding footnotes programatically 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 the footernotes.xml)
2. Adding the footnote text node in the footernotes.xml

Step 1 consists of creating a <w:r> <w:t> node which corresponds to the word footnotes syntax. The generated node should look something like this

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

<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 refernces to the Footernotes.xml. This step created a node which consists of the supercript number, and the actual footnote. The generated node should look something like this

<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 2 nodes are created, the footnote is ready. The document.xml and footernotes.xml should be saved.

I have put a sample document with raw tags 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 what were the prerequistes done in the following files
1. [Content_Types].xml: The following section was added

<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"/>
2. word\_rels\document.xml.rels: The following section was added
<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 this 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)

About the Author

Abey Thomas


Member
I am Abey Thomas Raju from Bangalore, India.
Occupation: Software Developer (Senior)
Company: AdviceAmerica Software Pvt Ltd
Location: India India

Other popular Office Development articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Oct 2008
Editor:
Copyright 2008 by Abey Thomas
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project