C# Add Word header and footer

In a Word document, we can enrich the content of the document by adding headers and footers. When adding headers and footers, you can add time, date, document title, document citation information, page numbers, content explanations, pictures/LOGO and other graphic information. At the same time, you can adjust the position of the text or picture in the header and footer according to your needs. Therefore, this article will introduce how to use the free component Free Spire. Doc for .NET in C# to add headers and footers.

Tip : After downloading and installing this component, please refer to the dll file in your VS project program (the dll file can be obtained in the Bin folder under the installation file)

1. Add text and picture headers

 

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;

namespace AddHeaderAndFooter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of the Document class, add section and Paragraph
            Document document = new Document(@"C:\Users\Administrator\Desktop\Test.docx");
            Section sec = document.AddSection();
            Paragraph para = sec.AddParagraph();

            //Declare a HeaderFooter class object, add headers and footers
            HeaderFooter header = sec.HeadersFooters.Header;
            Paragraph headerPara = header.AddParagraph();
            HeaderFooter footer = sec.HeadersFooters.Footer;
            Paragraph footerPara = footer.AddParagraph();           

            //Add pictures and text to the header, and format the text
            DocPicture headerImage = headerPara.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\2.jpg"));
            TextRange TR = headerPara.AppendText("The Word Trade Organization, WTO");
            TR.CharacterFormat.FontName = "Andalus";
            TR.CharacterFormat.FontSize = 12;
            TR.CharacterFormat.TextColor = Color.Green;
            TR.CharacterFormat.Bold = false;
            headerImage.TextWrappingType = TextWrappingType.Right;

            //Add text to footer and format
            TR = footerPara.AppendText("The World Trade Organization is an intergovernmental organization that regulates international trade.The WTO officially commenced on 1 January 1995 under the Marrakesh Agreement, signed by 123 nations on 15 April 1994, replacing the General Agreement on Tariffs and Trade, which commenced in 1948. ");
            TR.CharacterFormat.Bold = false;
            TR.CharacterFormat.FontSize = 9;           

            // save the document and run the document
            document.SaveToFile("Text header.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Text header.docx");
        }
    }
}

 

 

Test Results:



 

2. Add page numbers

using Spire.Doc;
using Spire.Doc.Documents;

namespace AddPageNumber_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Document class, add section and Paragraph
            Document document = new Document();
            Section sec = document.AddSection();
            Paragraph para = sec.AddParagraph();

            //Add text to paragraph, set BreakType to pagination
            para.AppendText("Page 1");
            para.AppendBreak(BreakType.PageBreak);
            para.AppendText("Page 2");

            //Create an instance of the HeaderFooter class and add a footer
            HeaderFooter footer = sec.HeadersFooters.Footer;
            Paragraph footerPara = footer.AddParagraph();

            //Add field type as page number, add current page, separator line and total number of pages
            footerPara.AppendField("页码", FieldType.FieldPage);
            footerPara.AppendText(" / ");
            footerPara.AppendField("总页数", FieldType.FieldNumPages);
            footerPara.Format.HorizontalAlignment = HorizontalAlignment.Right;

            // save the document
            document.SaveToFile("Add page number.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Add page number.docx");
        }
    }
}

 

Test Results:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119510&siteId=291194637