About Me

My photo
Ireland
Hello, my name is Cathal Coffey. I am best described as a hybrid between a developer and an adventurer. When I am not behind a keyboard coding, I am hiking and climbing the beautiful mountains of my home country Ireland. I am a full time student studying Computer Science & Software Engineering at the National University of Ireland Maynooth. I am finishing the final year of a 4 year degree in September 2009. I am the creator of an open source project on codeplex.com called DocX. At the moment I spend a lot of my free time advancing DocX and I enjoy this very much. My aim is to build a community around DocX and add features based on requests from this community. I really enjoy hearing about how people are using DocX in their work\personal projects. So if you are one of these people, please send me an email. Cathal coffey.cathal@gmail.com

Monday, June 28, 2010

DocX version 1.0.0.10

This version of DocX contains support for Headers and Footers.
There are a few different options to choose from.

1) You can have a default Header and Footer for the entire document.
2) You can have a different Header and Footer for the first page.
3) You can have a different Header and Footer for odd and even pages.

The first code example generates a document with a default Header and Footer that is applied to all pages.

Default Header and Footer
  1. // Create a new document.
  2. using (DocX document = DocX.Create(@"Test.docx"))
  3. {
  4.     // Add Header and Footer support to this document.
  5.     document.AddHeaders();
  6.     document.AddFooters();
  7.  
  8.     // Get the default Header for this document.
  9.     Header header_default = document.Headers.odd;
  10.  
  11.     // Get the default Footer for this document.
  12.     Footer footer_default = document.Footers.odd;
  13.  
  14.     // Insert a Paragraph into the default Header.
  15.     Paragraph p1 = header_default.InsertParagraph();
  16.     p1.Append("Hello Header.").Bold();
  17.  
  18.     // Insert a Paragraph into the document.
  19.     Paragraph p2 = document.InsertParagraph();
  20.     p2.AppendLine("Hello Document.").Bold();
  21.  
  22.     // Insert a Paragraph into the default Footer.
  23.     Paragraph p3 = footer_default.InsertParagraph();
  24.     p3.Append("Hello Footer.").Bold();
  25.     
  26.     // Save all changes to this document.
  27.     document.Save();
  28. }// Release this document from memory.

The code directly above will generate a document that looks like the image below.

Untitled2 
The second code example generates a document which has a different Header and Footer for the first page. All other pages use the default Header and Footer.

First Header and Footer
  1. // Create a new document.
  2. using (DocX document = DocX.Create(@"Test.docx"))
  3. {
  4.     // Add Header and Footer support to this document.
  5.     document.AddHeaders();
  6.     document.AddFooters();
  7.  
  8.     // Get the first Header for this document.
  9.     Header header_first = document.Headers.first;
  10.  
  11.     // Get the first Footer for this document.
  12.     Footer footer_first = document.Footers.first;
  13.  
  14.     // Insert a Paragraph into the first Header.
  15.     Paragraph p1 = header_first.InsertParagraph();
  16.     p1.Append("Hello First Header.").Bold();
  17.  
  18.     // Insert a Paragraph into the document.
  19.     Paragraph p2 = document.InsertParagraph();
  20.     p2.AppendLine("Hello First page.");
  21.     
  22.     // Create a second page to show that only the first has a header and footer.
  23.     p2.InsertPageBreakAfterSelf();
  24.  
  25.     // Insert a Paragraph after the page break.
  26.     Paragraph p3 = document.InsertParagraph();
  27.     p3.AppendLine("Hello Second page.");
  28.  
  29.     // Insert a Paragraph into the first Footer.
  30.     Paragraph p4 = footer_first.InsertParagraph();
  31.     p4.Append("Hello First Footer.").Bold();
  32.  
  33.     // Force the first page to have a different Header and Footer.
  34.     document.DifferentFirstPage = true;
  35.  
  36.     // Save all changes to this document.
  37.     document.Save();
  38. }// Release this document from memory.

The code directly above will generate a document that looks like the image below. Notice that the second page has an empty Header and Footer, this will also be the same for all proceeding pages.

Untitled3

The third code example generates a document which has a different Header and Footer for odd and even pages.

Odd and Even Header and Footer
  1. // Create a new document.
  2. using (DocX document = DocX.Create(@"Test.docx"))
  3. {
  4.     // Add Header and Footer support to this document.
  5.     document.AddHeaders();
  6.     document.AddFooters();
  7.  
  8.     // Get the odd and even Headers for this document.
  9.     Header header_odd = document.Headers.odd;
  10.     Header header_even = document.Headers.even;
  11.  
  12.     // Get the odd and even Footer for this document.
  13.     Footer footer_odd = document.Footers.odd;
  14.     Footer footer_even = document.Footers.even;
  15.  
  16.     // Insert a Paragraph into the odd Header.
  17.     Paragraph p1 = header_odd.InsertParagraph();
  18.     p1.Append("Hello Odd Header.").Bold();
  19.  
  20.     // Insert a Paragraph into the even Header.
  21.     Paragraph p2 = header_even.InsertParagraph();
  22.     p2.Append("Hello Even Header.").Bold();
  23.  
  24.     // Insert a Paragraph into the document.
  25.     Paragraph p3 = document.InsertParagraph();
  26.     p3.AppendLine("Hello First page.");
  27.     
  28.     // Create a second page to show that even and odd pages have different headers and footers.
  29.     p3.InsertPageBreakAfterSelf();
  30.  
  31.     // Insert a Paragraph after the page break.
  32.     Paragraph p4 = document.InsertParagraph();
  33.     p4.AppendLine("Hello Second page.");
  34.  
  35.     // Insert a Paragraph into the odd Footer.
  36.     Paragraph p5 = footer_odd.InsertParagraph();
  37.     p5.Append("Hello Odd Footer.").Bold();
  38.  
  39.     // Insert a Paragraph into the even Footer.
  40.     Paragraph p6 = footer_even.InsertParagraph();
  41.     p6.Append("Hello Even Footer.").Bold();
  42.  
  43.     // Force odd & even pages to have different Headers and Footers.
  44.     document.DifferentOddAndEvenPages = true;
  45.  
  46.     // Save all changes to this document.
  47.     document.Save();
  48. }// Release this document from memory.

The code directly above will generate a document that looks like the image below. Note that the first page has a different header and footer than the second.

Untitled4

It is of course possible to create a document with a different header and footer for odd, even and the first page. You just need to set the following two properties to true.

// Force the first, odd & even pages to have different Headers and Footers.
document.DifferentOddAndEvenPages = true;
document.DifferentFirstPage = true;

This version of DocX does not support page numbering inside Headers and Footers. This feature will require much more development time due to the shear number of options available for numbering. I hope to add this feature in the near future.

Happy coding,
Cathal

15 comments:

  1. Are page numbers supported in DocX?

    ReplyDelete
  2. Hi Cathal Coffey,

    Thanks for creating very useful and innovative project.

    But I am facing issue while modifying footer.It adds footer multiple times on same page. For instance,if I execute append footer code on same file 4 times, then it will add 4 footer on each page.

    Is there any way to remove existing footer or replace it with new one??

    Thanks and Regards,
    Pankaj

    ReplyDelete
  3. Hi Cathal,

    I did some research on docX code, and found answer. once again thanks for
    creating .It reduced my lots of effort.

    Thanks and Regards,
    Pankaj

    ReplyDelete
  4. Is it possible to add header text to an existing header? For instance, add additional lines of text below a company letthead logo.

    Can an image be inserted?

    Any luck with adding page #'s to the footer?

    Thank you for a great contribution!

    ReplyDelete
  5. Hi Cathal,
    I want to append new page to current word document and remove headers and footers from that last page only. Is that possible?

    ReplyDelete
  6. hi cathal i want change the position of header can you please help me

    ReplyDelete
  7. Hi Cathal,
    I want to set the position odf footer as footer sometimes getting duplicate itself when editing a document.

    ReplyDelete
  8. Hi, how insert a image in header?

    Thanks

    ReplyDelete
  9. Hi,

    Thank you for the beautiful library. Questions...
    - Is there a way to replace an image in a document?
    - How can I specify exact location on images/paragraphs withing the document?

    Thanks
    - Daniel

    ReplyDelete
  10. How do i put a different margin for header/footers and for the body of the document? IF if i define for all of the document margin equal to 0, i still get a top margin for the header and a bottom margin for the footer, how can i change that?

    ReplyDelete
    Replies
    1. If you can, or someone that knows this library well, can tell me if the following is possible? http://stackoverflow.com/questions/44046928/custom-headers-and-footers-for-novacode-docx
      please.

      Delete
  11. Hello Cathal.
    Excellent library, thank you.
    I have having trouble, though, accessing Headers and Footers where my document contains Sections. Is there a special trick to this? Thanks

    ReplyDelete
  12. Hi Cathal,

    How to remove the existing header and footer before adding header and footer . Can you please provide me the code

    Thanks and Regards,
    Pankaj

    ReplyDelete
  13. Hi Cathal,

    How to remove the existing header and footer before adding header and footer . Can you please provide me the code

    Thanks and Regards,
    Pankaj

    ReplyDelete