C # Word Document Actions--add word headers, footers, and page numbers

Source: Internet
Author: User

In Word documents, we can enrich the contents of the document by adding headers and footers. When you add a header or footer, you can add time, date, document title, document reference information, page number, content interpretation, picture/logo, and more. You can also adjust the position of the text or picture in the header and footer as needed. Therefore, this article describes how to use the Community Edition control free Spire in C #. Doc for. NET to add headers, footers, and page numbering methods.

tip: After downloading and installing the component, note that the DLL file is referenced in your VS Project program (the DLL file can be obtained in the Bin folder under the installation file)

First, add text, picture header
Using spire.doc;using spire.doc.documents;using system.drawing;using spire.doc.fields;namespace AddHeaderAndFooter{            Class Program {static void Main (string[] args) {//Create a document class instance, 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 header, footer 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 the footer and format TR = Footerpara.appendtext ("The World trade Organization are an intergovernmental Organization t Hat regulates international trade. The WTO officially commenced on 1 January 1995 under the Marrakesh Agreement, signed by 123 nations on April 1994, REPL Acing the agreement on Tariffs and trade, which commenced in 1948.            "); Tr.            Characterformat.bold = false; Tr.                       Characterformat.fontsize = 9; Save the document and run the document.            SaveToFile ("Text header. docx", fileformat.docx);        System.Diagnostics.Process.Start ("Text header. docx"); }    }}

Operation Result:

PS: for situations where you need to set the position of the picture in the text, we can do it by Textwrappingstyle or Textwrappingtpye.
Eg:

headerImage.TextWrappingStyle = TextWrappingStyle.Through;或headerImage.TextWrappingType = TextWrappingType.Right;
Second, add page number

To add a page number, we can choose to add it at the header or footer.

Using spire.doc;using Spire.doc.documents;namespace addpagenumber_doc{class Program {static void Main (Strin            G[] (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 paging para.            AppendText ("1th page"); Para.            Appendbreak (Breaktype.pagebreak); Para.            AppendText ("2nd page"); Create an instance of the HeaderFooter class, add the footer HeaderFooter footer = sec.            Headersfooters.footer; Paragraph Footerpara = footer.            Addparagraph ();            Add field type to page number, add current page, divider line, and total page Footerpara.appendfield ("page number", fieldtype.fieldpage);            Footerpara.appendtext ("/");            Footerpara.appendfield ("Total pages", fieldtype.fieldnumpages);            FooterPara.Format.HorizontalAlignment = HorizontalAlignment.Right; Save document documents. SaveToFile ("Add page number. docx", FiLeformat.docx);        System.Diagnostics.Process.Start ("Add page number. docx"); }    }}

Operation Result:

These are the code actions for how Word adds headers, footers, and page numbers. If you like, welcome reprint (Reproduced please specify the source)
Thanks for browsing!

C # Word Document Actions--add word headers, footers, and page numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.