Spire.Doc
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 some text to it.

//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 table of contents.

para1.ApplyStyle(BuiltinStyle.Heading1);

Step 4: 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);

            para1 = section.AddParagraph();
            para1.AppendText("Head2");
            para1.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)
Tuesday, 04 November 2014 09:08

Insert an existing Table by cloning in C#

Written by support iceblue

In some case, we need make some modifications in an existing table but don't want destroy the original data, so we would like to copy the existing table then make some changes in the new table. How could we get the copied table? The easiest method is clone. There would introduce a solution to copy table and modify some data then insert the new table after original table via Spire.Doc.

Spire.Doc for .NET, a stand-alone .NET Word component, provides a method, Table.clone() to allow users to copy an existing table.

The main steps of the solution:

Firstly: load the word document with a table.

Document doc = new Document();
doc.LoadFromFile(@"CopyTable.doc");

The original document effect screenshot:

Insert an existing Table by cloning

Secondly: extract the existing table and call the table.clone () method to copy it.

Section se = doc.Sections[0];
Table original_Table =(Table) se.Tables[0];
Table copied_Table = original_Table.Clone();

Thirdly: extract the last row then traversal its cells to modify data.

string[] st = new string[] { "Guyana", "Georgetown", "South America", "214969", "800000" };
//get the last row of copied table
TableRow lastRow = copied_Table.Rows[copied_Table.Rows.Count - 1];
//change lastRow data.
lastRow.RowFormat.BackColor = Color.Gray;
for (int i = 0; i < lastRow.Cells.Count; i++)
    {
    lastRow.Cells[i].Paragraphs[0].Text = st[i];       
     }

Finally: call Section. tables.add() method to add the copied table in section and save this document.

se.Tables.Add(copied_Table);
doc.SaveToFile("result.doc", FileFormat.Doc);
The result document effect screenshot:

Insert an existing Table by cloning

Full code:

using Spire.Doc;
using System.Drawing;

namespace InsertingaAnExistingTable
{
    class Program
    {
        static void Main(string[] args)
        { 
//load a word document
            Document doc = new Document();
            doc.LoadFromFile(@"CopyTable.doc");

// extract the existing table
            Section se = doc.Sections[0];
            Table original_Table =(Table) se.Tables[0];

// copy the existing table to copied_Table via Table.clone()
            Table copied_Table = original_Table.Clone();
string[] st = new string[] { "Guyana", "Georgetown", "South America", "214969", "800000" };
            //get the last row of table
            TableRow lastRow = copied_Table.Rows[copied_Table.Rows.Count - 1];
            //change last row data.
            lastRow.RowFormat.BackColor = Color.Gray;
            for (int i = 0; i < lastRow.Cells.Count; i++)
            {
                lastRow.Cells[i].Paragraphs[0].Text = st[i];
            }
// add copied_Table in section
            se.Tables.Add(copied_Table);
            doc.SaveToFile("result.doc", FileFormat.Doc);     
        }
    }
}
Page 1 of 26