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

Header and Footer in Word and How to Set with VB.NET

 

There may be header or footer in a Word document. Header and footer are not necessary but they can show the additional information for the document. Usually, the contents of header and footer include time, date, page or some symbols.

It is common that header and footer are about pages. If the document has over ten pages, the owner will add header or footer to show the pages for that it is convenient for readers to read contents of one page. Sometimes, the footer may be are symbols, for example, company name or author signature. These symbols show the owner of the document.

Although it is not essential to add header and footer, they are very useful in some documents. Open a Word document, click View menu, and we can find header and footer. Select it and the header and footer box appears. Write text or using the default format to set the header and footer. If we just want to set Word footer, we can leave the header box blank and enter text or use default format in footer box.

Besides using Microsoft Word to set header and footer directly, programmers may use C# or VB.NET to set Word header and footer. Now, I want to show a method to set word header and footer with VB.NET.

 

Code:

 

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click

    'Create word document

    Dim document_Renamed As New Document()

    InsertHeaderFooter(document_Renamed)

    'Save doc file.

    document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)

    'Launching the MS Word file.

    WordDocViewer("Sample.doc")

End Sub

Private Sub InsertHeaderFooter(ByVal document_Renamed As Document)

    Dim section_Renamed As Section = document_Renamed.AddSection()

    Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()

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

    paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)

    section_Renamed.PageSetup.DifferentFirstPageHeaderFooter = True

    paragraph_Renamed = New Paragraph(document_Renamed)

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

    paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center

    section_Renamed.HeadersFooters.FirstPageHeader.Paragraphs.Add(paragraph_Renamed)

    paragraph_Renamed = New Paragraph(document_Renamed)

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

    paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center

    section_Renamed.HeadersFooters.FirstPageFooter.Paragraphs.Add(paragraph_Renamed)

End Sub

Private Sub WordDocViewer(ByVal fileName As String)

    Try

        Process.Start(fileName)

    Catch

    End Try

End Sub

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité