Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
25 octobre 2011

(C#) Add Header and Footer in Word

Header and footer are often used in MS Word to present page number and other information. For example, there is a Word document with many pages, people may add page number as header. Also, if the document is company materials, the footer will be named as company name or material property. And the contents of header and footer can be exchanged.

MS Word provides us with the function to add header and footer. We can choose the most appropriate style to insert, including page number format, such as x, x/y and so on.

It is easy to insert header and footer. In Word 2007, click Insert menu, and then select Header or Footer. Choose one style to insert. You can write words to edit header or footer directly or click Page to insert page number.

Now, I want to introduce a method to insert header and footer in Word with C#. This method is based on a component named Spire.Doc which is used to operate Word with Silverlight and .NET. If you want to use the following code, please download and install this component. Then add its DLL file as reference.

private void button1_Click(object sender, EventArgs e)

{

    //Create word document

    Document document = new Document();

 

    InsertHeaderFooter(document);

 

    //Save doc file.

    document.SaveToFile("Sample.doc",FileFormat.Doc);

 

    //Launching the MS Word file.

    WordDocViewer("Sample.doc");

 

}

 

private void InsertHeaderFooter(Document document)

{

    Section section = document.AddSection();

    Paragraph paragraph = section.AddParagraph();

    paragraph.AppendText("The sample demonstrates how to insert a header and footer into a document.");

    paragraph.ApplyStyle(BuiltinStyle.Heading2);

   

    paragraph = section.AddParagraph();

    paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");

 

    section.PageSetup.DifferentFirstPageHeaderFooter = true;

 

    paragraph = new Paragraph(document);

    paragraph.AppendText("Spire.Doc for .NET").CharacterFormat.FontSize = 15;

    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;

    section.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph);

 

    paragraph = new Paragraph(document);

    paragraph.AppendText("e-iceblue company Ltd. 2002-2010 All rights reserverd").CharacterFormat.FontSize = 15;

    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;

    section.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph);

}

 

private void WordDocViewer(string fileName)

{

    try

    {

        System.Diagnostics.Process.Start(fileName);

    }

    catch { }

}

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité