Insert Table in OneNote using C# | OneNote Table C#

OneNote is a powerful note-taking application that allows users to organize their thoughts and ideas in a structured format. One of the essential features of OneNote is the ability to insert tables, which can be useful for creating lists, comparing data, and more. In this guide, we will learn how to insert a table in OneNote using C#.

This article covers the following topics:

  1. C# API to Insert Tables in OneNote Document
  2. Insert a Table in OneNote using C#
  3. Create a Table with Locked Columns in C#
  4. Free Resources

C# API to Insert Tables in OneNote Documents

We will use the Aspose.Note for .NET API to insert tables in OneNote documents. It is a powerful API that allows developers to work with Microsoft OneNote files (.one) in their .NET applications. It provides a comprehensive set of features to create, edit, and manipulate OneNote documents.

Please download the DLL of the API or install it using NuGet with the following command:

PM> Install-Package Aspose.Note

Insert a Table in OneNote using C#

We can insert a table in a OneNote document by following the steps below:

  1. Create a new OneNote document using the Document class.
  2. Add a new page using the Page class.
  3. Create table rows and cells using the TableRow and TableCell classes, respectively.
  4. Initialize the Table class object and set column widths.
  5. After that, append all objects using the AppendChildLast() method.
  6. Finally, save the document using the Save method.

The following code sample shows how to create a table in a OneNote document using C#.

// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Page page = new Page();
// Initialize TableRow class object
TableRow row1 = new TableRow();
// Initialize TableCell class objects
TableCell cell11 = new TableCell();
TableCell cell12 = new TableCell();
TableCell cell13 = new TableCell();
// Append outline elements in the table cell
cell11.AppendChildLast(GetOutlineElementWithText("cell_1.1"));
cell12.AppendChildLast(GetOutlineElementWithText("cell_1.2"));
cell13.AppendChildLast(GetOutlineElementWithText("cell_1.3"));
// Table cells to rows
row1.AppendChildLast(cell11);
row1.AppendChildLast(cell12);
row1.AppendChildLast(cell13);
// Initialize TableRow class object
TableRow row2 = new TableRow();
// initialize TableCell class objects
TableCell cell21 = new TableCell();
TableCell cell22 = new TableCell();
TableCell cell23 = new TableCell();
// Append outline elements in the table cell
cell21.AppendChildLast(GetOutlineElementWithText("cell_2.1"));
cell22.AppendChildLast(GetOutlineElementWithText("cell_2.2"));
cell23.AppendChildLast(GetOutlineElementWithText("cell_2.3"));
// Append table cells to rows
row2.AppendChildLast(cell21);
row2.AppendChildLast(cell22);
row2.AppendChildLast(cell23);
// Initialize Table class object and set column widths
Table table = new Table()
{
IsBordersVisible = true,
Columns = { new TableColumn { Width = 200 }, new TableColumn { Width = 200 }, new TableColumn { Width = 200 } }
};
// Append table rows to table
table.AppendChildLast(row1);
table.AppendChildLast(row2);
// Initialize Outline object
Outline outline = new Outline();
// Initialize OutlineElement object
OutlineElement outlineElem = new OutlineElement();
// Add table to outline element node
outlineElem.AppendChildLast(table);
// Add outline element to outline
outline.AppendChildLast(outlineElem);
// Add outline to page node
page.AppendChildLast(outline);
// Add page to document node
doc.AppendChildLast(page);
// Save the document
doc.Save("InsertTable_out.one");
static OutlineElement GetOutlineElementWithText(string text)
{
OutlineElement outlineElem = new OutlineElement();
ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
outlineElem.AppendChildLast(new RichText() { Text = text, ParagraphStyle = textStyle });
return outlineElem;
}
Insert a Table in OneNote using C#

Insert a Table in OneNote using C#

Create a Table with Locked Columns in C#

Similarly, we can create a table with a locked column width in a OneNote document by following the above mentioned steps. However, we just need to specify the LockedWidth property to true while creating the TableColumns class object.

The following code sample shows how to create a table with a locked column width in a OneNote document using C#.

// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Page page = new Page();
// Initialize TableRow class object
TableRow row1 = new TableRow();
// Initialize TableCell class object and set text content
TableCell cell11 = new TableCell();
cell11.AppendChildLast(GetOutlineElementWithText("Small text"));
row1.AppendChildLast(cell11);
// Initialize TableRow class object
TableRow row2 = new TableRow();
// Initialize TableCell class object and set text content
TableCell cell21 = new TableCell();
cell21.AppendChildLast(GetOutlineElementWithText("Long text with several words and spaces."));
row2.AppendChildLast(cell21);
// Initialize Table class object
Table table = new Table()
{
IsBordersVisible = true,
Columns = { new TableColumn { Width = 70, LockedWidth = true } }
};
// Add rows
table.AppendChildLast(row1);
table.AppendChildLast(row2);
Outline outline = new Outline();
OutlineElement outlineElem = new OutlineElement();
// Add table node
outlineElem.AppendChildLast(table);
// Add outline element node
outline.AppendChildLast(outlineElem);
// Add outline node
page.AppendChildLast(outline);
// Add page node
doc.AppendChildLast(page);
// Save
doc.Save("CreateTableWithLockedColumns_out.one");

Get a Free License

Please get a free temporary license to try Aspose.Note for .NET without evaluation limitations.

OneNote Table – Free Resources

Besides inserting tables in OneNote documents, you may learn more about the API and explore various other features using the resources below:

Conclusion

In this guide, we have seen how to insert a table in OneNote using C#. By following the steps outlined in this article, you can easily create a table in OneNote and add data to it using C#. This can be useful for automating tasks and creating custom solutions for your note-taking needs. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also