C # operate on the Word Document-Add the Word header, footer, and page number,

Source: Internet
Author: User
Tags add time

C # operate on the Word Document-Add the Word header, footer, and page number,

In Word documents, we can add headers and footers to enrich the document content. When adding headers and footers, you can add time, date, document title, document reference information, page number, content explanation, images/logos, and other graphic information. You can also adjust the position of text or image in the header and footer as needed. Therefore, this article describes how to use the Free Spire. Doc for. NET Component in C # To add headers and footers.

Prompt: After downloading and installing this component, pay attention to reference the dll file in your VS Project Program (this dll file can be obtained in the Bin folder under the installation file)

1. Add text and image 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 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 and add HeaderFooter header = sec. headersFooters. header; Paragraph headerPara = header. addParagraph (); HeaderFooter footer = sec. headersFooters. footer; Paragraph footerPara = footer. addParagraph (); // Add the image and text to the header, and set the text format 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 set the 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 minutes l 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 it. saveToFile (" 文 ..docx", FileFormat. docx); System. diagnostics. process. start (" 文 .docx ");}}}

Running result:

PS:You can useTextWrappingStyleOrTextWrappingTpye.

Eg:

HeaderImage. TextWrappingStyle = TextWrappingStyle. Through;
Or headerImage. TextWrappingType = TextWrappingType. Right;
2. Add page number

Add page number. You can add it to the header or footer.

Using Spire. doc; using Spire. doc. parameters; 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 and set BreakType to para. appendText ("Page 1st"); para. appendBreak (BreakType. pageBreak); para. appendText ("Page 2nd"); // create a HeaderFooter class instance and add the footer HeaderFooter footer = sec. headersFooters. footer; Paragraph footerPara = footer. addParagraph (); // Add the field type as the page number, and add the current page, separator line, and total page number footerPara. appendField ("page number", FieldType. fieldPage); footerPara. appendText ("/"); footerPara. appendField ("Total number of pages", FieldType. fieldNumPages); footerPara. format. horizontalAlignment = HorizontalAlignment. right; // Save the document. saveToFile ("webpage code .docx", FileFormat. docx); System. diagnostics. process. start ("Development page code .docx ");}}}

Effect display:

The preceding section describes how to add the header, footer, and page number in Word. If you like it, you are welcome to reprint it ).

Thank you for browsing!

 

Related Article

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.