Spire.Doc

Page number is one of the formats of header and footer. It gives the total pages of a document and it is convenient for readers to index. Spire.Doc supports to add text and image header and footers. This tutorial will guide you how to add the page number on the header/footer of a document in C#.

Spire.Doc offers HeaderFooter class to set header or footer for word document. Then, invoke header/footer.AddParagraph() method to add header/footer paragraph and add contents by invoking paragraph.AppendText method. Here comes to the code snippet of how to add the page number for word documents.

Step 1: Create a new document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Create footer for the first section and add a paragraph for the footer.

HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();

Step 3: Add the page number to the footer and set the alignment for the paragraph.

footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText(" of ");
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

Step 4: Save the document to file.

doc.SaveToFile("Add.docx", FileFormat.Docx);

Effective screenshot:

How to add the page number on the header/footer of a document

Full codes:

namespace AddPagenumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx");
            HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
            Paragraph footerParagraph = footer.AddParagraph();
            footerParagraph.AppendField("page number", FieldType.FieldPage);
            footerParagraph.AppendText(" of ");
            footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
            footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
            doc.SaveToFile("Add.docx", FileFormat.Docx);

        }
    }
}
Monday, 24 November 2014 07:56

How to Create Table of Contents (TOC) in C#

Written by support iceblue

A table of contents, usually shorten as 'Contents' and abbreviated as TOC, is one of the most common function used in the professional documents. It gives readers clear and brief information of the document. This How To Guide for developers will explain the steps of create table of contents in C# with the help of a .NET word API Spire.Doc for .NET.

Firstly, view the screenshot of the table of contents created by the Spire.Doc in C#.

How to create Table of Contents (TOC) in C#

In this example, we call the method of AppendTOC to add the table of contents directly and use ApplyStyle to set the styles. Here comes to the steps of how to create TOC in C#.

Name Space we will use:

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

Step 1: Create a new document and add section and paragraph to the document.

Document doc = new Document();
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();

Step 2: Add the Table of Contents and add the text that you want to appear in the table of contents.

para.AppendTOC(1, 3);
//Add a new paragraph to the section
Paragraph para1 = section.AddParagraph();
//Add text to the paragraph
para1.AppendText("Head1");

Step 3: Set the style for the paragraph.

para1.ApplyStyle(BuiltinStyle.Heading1);

Step 4: Add the second paragraph and set the style.

Paragraph para2 = section.AddParagraph();
para2.AppendText("Head2");
para2.ApplyStyle(BuiltinStyle.Heading2);

Step 5: Update the table of contents and save the document to file.

doc.UpdateTableOfContents();
doc.SaveToFile("CreateTableOfContent.docx", FileFormat.Docx);

Full codes:

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

namespace TableofContents
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph para = section.AddParagraph();

            para.AppendTOC(1, 3);
            Paragraph para1 = section.AddParagraph();
            para1.AppendText("Head1");

            para1.ApplyStyle(BuiltinStyle.Heading1);

            Paragraph para2 = section.AddParagraph();
            para2.AppendText("Head2");
            para2.ApplyStyle(BuiltinStyle.Heading2);

            doc.UpdateTableOfContents();
            doc.SaveToFile("CreateTableOfContent.docx", FileFormat.Docx);

        }
    }
}

In our daily work, we may have the requirement to add custom properties with fields to a Word document. As is shown in the following Word document, I have created three custom property fields for easily inserting or updating information.

How to Remove Custom Property Fields in C#, VB.NET

However, a custom property field may lose its value if we don’t want to use it any more, or a custom field might be created with wrong information, in such cases, we can choose to delete these fields manually and programmatically. In this article, I’ll introduce a C# and VB.NET solution to remove custom property fields using Spire.Doc.

Detailed Steps

Step 1: Create a new instance of Spire.Doc.Document class and load the sample file with specified path.

Document doc = new Document();
doc.LoadFromFile("FieldSample.docx", FileFormat.Docx);

Step 2: Get custom document properties object.

CustomDocumentProperties cdp = doc.CustomDocumentProperties;

Step 3: Use a for sentence and CustomDocumentProperties.Remove(string name) method to remove all custom property fields in the document.

for (int i = 0; i < cdp.Count; )
{
  cdp.Remove(cdp[i].Name);
}
doc.IsUpdateFields = true;

Step 4: Save the file.

doc.SaveToFile("Result.docx", FileFormat.Docx);

Output:

How to Remove Custom Property Fields in C#, VB.NET

Full Code:

[C#]
Document doc = new Document();
doc.LoadFromFile("FieldSample.docx", FileFormat.Docx);
CustomDocumentProperties cdp = doc.CustomDocumentProperties;
for (int i = 0; i < cdp.Count; )
{
    cdp.Remove(cdp[i].Name);
}
doc.IsUpdateFields = true;
doc.SaveToFile("Result.docx", FileFormat.Docx);
[VB.NET]
Dim doc As New Document()
doc.LoadFromFile("FieldSample.docx", FileFormat.Docx)
Dim cdp As CustomDocumentProperties = doc.CustomDocumentProperties
Dim i As Integer = 0
While i < cdp.Count
	cdp.Remove(cdp(i).Name)
End While
doc.IsUpdateFields = True
doc.SaveToFile("Result.docx", FileFormat.Docx)
Page 1 of 27