C# .NET - Header and footer of Word DOC file using C#

Asked By Arth Srivastava on 08-May-09 12:45 AM
Hi All,
I have created a Word Doc file using C#...Now i also want to create header and footer inside the Word DOC file. The content of the header may be image/Text..

Plz share...
Thanks,
Arth
Ravenet Rasaiyah replied to Arth Srivastava on 08-May-09 12:49 AM
Hi

MSDN made sample for this,
Here sample for Footer and header to word document.

http://msdn.microsoft.com/en-us/library/ms178795.aspx

thank you

re

Web Star replied to Arth Srivastava on 08-May-09 12:50 AM

try this

Public Sub Page_Load(sender as Object, e as EventArgs)
    'build the content for the dynamic Word document
    'in HTML alongwith some Office specific style properties. 
    Dim strBody As New System.Text.StringBuilder("")
    strBody.Append("<html " & _ 
            "xmlns:o='urn:schemas-microsoft-com:office:office' " & _
            "xmlns:w='urn:schemas-microsoft-com:office:word'" & _ 
            "xmlns='http://www.w3.org/TR/REC-html40'>" & _
            "<head><title>Time</title>") 
    'The setting specifies document's view after it is downloaded as Print
    'instead of the default Web Layout
    strBody.Append("<!--[if gte mso 9]>" & _
                             "<xml>" & _ 
                             "<w:WordDocument>" & _
                             "<w:View>Print</w:View>" & _
                             "<w:Zoom>90</w:Zoom>" & _ 
                             "<w:DoNotOptimizeForBrowser/>" & _
                             "</w:WordDocument>" & _
                             "</xml>" & _ 
                             "<![endif]-->")
    strBody.Append("<style>" & _
                            "<!-- /* Style Definitions */" & _
                            "@page Section1" & _
                            "   {size:8.5in 11.0in; " & _
                            "   margin:1.0in 1.25in 1.0in 1.25in ; " & _
                            "   mso-header-margin:.5in; " & _
                            "   mso-footer-margin:.5in; mso-paper-source:0;}" & _
                            " div.Section1" & _
                            "   {page:Section1;}" & _
                            "-->" & _
                           "</style></head>") 
    strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" & _
                            "<div class=Section1>" & _
                            "<h1>Time and tide wait for none</h1>" & _ 
                            "<p style='color:red'><I>" & _
                            DateTime.Now & "</I></p>" & _
                            "</div></body></html>") 
    'Force this content to be downloaded 
    'as a Word document with the name of your choice
    Response.AppendHeader("Content-Type", "application/msword")
    Response.AppendHeader ("Content-disposition", _
                           "attachment; filename=myword.doc")
    Response.Write(strBody)
End Sub
http://www.codeproject.com/KB/office/Wordyna.aspx?fid=59794&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2277283

add text to the header & footer of a Word file

mv ark replied to Arth Srivastava on 08-May-09 12:56 AM
This explains how to add text to the header & footer of a Word file using the Office XML technique - http://mvark.blogspot.com/2007/02/how-to-add-header-or-footer-to.html
this is in vb
Rao B replied to Arth Srivastava on 08-May-09 01:01 AM
Well I found an example for VB:
ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).Range.InsertAfter
"My Text"
one more problem
Arth Srivastava replied to Ravenet Rasaiyah on 08-May-09 02:05 AM
Hi ,
Thanks for the Code..this one works fine...
below is the code..
foreach (Word.Section wordSection in oWordDoc.Sections)
{
wordSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Font.ColorIndex = Word.WdColorIndex.wdBlack;

wordSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Font.Size = 11;

wordSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "This is My Header";
}


but..if want in my header as...::


Arth Srivastava
Eggheadcafe


I mean to say ..adding space and breaks after the text...????

How can i achieve this...



Thanks,
Arth
welcome
Ravenet Rasaiyah replied to Arth Srivastava on 08-May-09 02:29 AM
Hi

Did you try with white space after finished your text? i think

string yourtest="dadandbaandas\t";

use like this
thank you
Spacilng is fine
Arth Srivastava replied to Ravenet Rasaiyah on 08-May-09 02:48 AM
hi ...spacing is working fine..but how to set the font size....

See...
I have three Fotter text as:

Arth Srivastava
Eggheadcafe


Now i want that all three text (Arth,Srivastava and Eggheadcafe) to be in different color and in different font size...ann all these 3 are part of a Footer....

I hope you got my question now... :)

Thanks a lot,
Arth
of course
Ravenet Rasaiyah replied to Arth Srivastava on 08-May-09 02:56 AM
OK good

try this way
foreach (Word.Section wordSection in this.Sections)
{
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;

wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Font.Size = 20;
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Text = "Eggheadcafe";

}

like this way.

thank you
No No
Arth Srivastava replied to Ravenet Rasaiyah on 08-May-09 03:18 AM
Hey dude...
you didnt get my question till now............


If i write ur code...it is ignoring the othere header text which i have already made....

I again repeat...
i have 3 text as
Arth Srivastava(please notice spacing and Font in all 3 text)
eggheadcafe


now how can i achieve this...??

Thanks,
Arth
let me think and try in my VS
Ravenet Rasaiyah replied to Arth Srivastava on 08-May-09 03:20 AM
end of post
jason rle replied to Arth Srivastava on 07-Aug-10 02:58 AM
I think you can use the Spire.Doc ,it is http://www.e-iceblue.com/Introduce/word-for-net-introduce.htmlcomponent that supports read and write doc, docx, html, image etc. embed image still support,also http://www.e-iceblue.com/Knowledgebase/Spire.Doc/Demos/Context/net-word-header-footer-demo.htmlinto a word doc file.
Shahzad Latif replied to Arth Srivastava on 11-Aug-11 07:22 AM
http://www.aspose.com/categories/.net-components/aspose.words-for-.net/default.aspx allows you to http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-create-headersfooters-using-documentbuilder.html. Please see if this might help in your scenario.
John Glenn replied to Arth Srivastava on 22-Feb-12 08:56 AM
Hi,

you can easily http://www.gemboxsoftware.com/document/articles/c-sharp-vb-net-create-write-word headers and footers for http://www.gemboxsoftware.com/document/articles/c-sharp-vb-net-docx format with this http://www.gemboxsoftware.com/document/overview library.

Here is a sample C# code:
// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// Create a new empty document.
var document = new DocumentModel();

// Add three pages to the document content and different headers / footers for first, even and all other pages.
document.Sections.Add(
  new Section(document,
    new Paragraph(document,
      new Run(document, "First page content"),
      new SpecialCharacter(document, SpecialCharacterType.PageBreak),
      new Run(document, "Second page content"),
      new SpecialCharacter(document, SpecialCharacterType.PageBreak),
      new Run(document, "Third page content")))
  {
    HeadersFooters =
    {
      new HeaderFooter(document, HeaderFooterType.HeaderFirst, new Paragraph(document, "First page header")),
      new HeaderFooter(document, HeaderFooterType.FooterFirst, new Paragraph(document, "First page footer")),
      new HeaderFooter(document, HeaderFooterType.HeaderEven, new Paragraph(document, "Even page header")),
      new HeaderFooter(document, HeaderFooterType.FooterEven, new Paragraph(document, "Event page footer")),
      new HeaderFooter(document, HeaderFooterType.HeaderDefault, new Paragraph(document, "Default page header")),
      new HeaderFooter(document, HeaderFooterType.FooterDefault, new Paragraph(document, "Default page footer")),
    }
  });

// Save the document to a file.
document.Save("Document.docx", SaveOptions.DocxDefault);