Work, Extract, Insert Tables in OneNote Document using C#

Extracting Plain Text from the Table of OneNote Document

Aspose.Note for .NET allows developers to extract text from the entire table, a row or a particular cell element.

Aspose.Note for .NET offers the Document class that represents a OneNote file. The Document class exposes the GetChildNodes method that can be called to extract table nodes from a OneNote document.

Get Table Text from OneNote Document

This example works as follows:

  1. Create an object of the Document class.
  2. Call the Document class’ GetChildNodes method.
  3. Retrieve a list of table nodes.
  4. Call the LINQ-based Code to extract text
  5. Display text on the output screen.

The following code example demonstrates how to get table text from a OneNote document.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Tables();
 3
 4// Load the document into Aspose.Note.
 5Document document = new Document(dataDir + "Sample1.one");
 6
 7// Get a list of table nodes
 8IList<Table> nodes = document.GetChildNodes<Table>();
 9
10// Set table count
11int tblCount = 0;
12
13foreach (Table table in nodes)
14{
15    tblCount++;
16    Console.WriteLine("table # " + tblCount);
17
18    // Retrieve text
19    string text = string.Join(Environment.NewLine, table.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
20    // Print text on the output screen
21    Console.WriteLine(text);
22}

Get Row Text from a Table in a OneNote Document

This example works as follows:

  1. Create an object of the Document class.
  2. Filter out a list of table nodes.
  3. Iterate through table rows.
  4. Call the LINQ-based Code to extract text.
  5. Display the text on the output screen.

The following code example demonstrates how to extract row text from a table in a OneNote document.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Tables();
 3
 4// Load the document into Aspose.Note.
 5Document document = new Document(dataDir + "Sample1.one");
 6
 7// Get a list of table nodes
 8IList<Table> nodes = document.GetChildNodes<Table>();
 9
10// Set row count
11int rowCount = 0;
12
13foreach (Table table in nodes)
14{
15    // Iterate through table rows
16    foreach (TableRow row in table)
17    {
18        rowCount++;
19        // Retrieve text
20        string text = string.Join(Environment.NewLine, row.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
21        // Print text on the output screen
22        Console.WriteLine(text);
23    }
24}

Get Cell Text from a Row in a Table

This example works as follows:

  1. Create an object of the Document class.
  2. Filter out a list of table nodes.
  3. Iterate through the table rows.
  4. Filter out a list of cell nodes from each row.
  5. Iterate through the row cells.
  6. Call the LINQ-based Code to extract text.
  7. Display the text on the output screen.

The following code example demonstrates how to get cell text from a row of the table.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Tables();
 3
 4// Load the document into Aspose.Note.
 5Document document = new Document(dataDir + "Sample1.one");
 6
 7// Get a list of table nodes
 8IList<Table> nodes = document.GetChildNodes<Table>();        
 9
10foreach (Table table in nodes)
11{
12    // Iterate through table rows
13    foreach (TableRow row in table)
14    {
15        // Get list of TableCell nodes
16        IList<TableCell> cellNodes = row.GetChildNodes<TableCell>();
17        // Iterate through table cells
18        foreach (TableCell cell in cellNodes)
19        {
20            // Retrieve text
21            string text = string.Join(Environment.NewLine, cell.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
22            // Print text on the output screen
23            Console.WriteLine(text);
24        }
25    }
26}

Insert a Table in OneNote Document

Aspose.Note for .NET APIs allows developers to insert a table at the particular node position. This article is meant to show you how to create a table in OneNote document programmatically.

Aspose.Note for .NET offers the Document class that represents a OneNote file. Developers can append content under TableCell node, table cells to the TableRow node, table row to the Table node. Later they could append table under OutlineElement node, outline element to Outline node, outline to Page node and then a page to the Document node. It’s all based on the Aspose.Note DOM structure.

The following code example demonstrates how to insert a table in a OneNote document.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Tables();
 3
 4// Create an object of the Document class
 5Document doc = new Document();
 6// Initialize Page class object
 7Aspose.Note.Page page = new Aspose.Note.Page(doc);
 8
 9// Initialize TableRow class object
10TableRow row1 = new TableRow(doc);
11// Initialize TableCell class objects
12TableCell cell11 = new TableCell(doc);
13TableCell cell12 = new TableCell(doc);
14TableCell cell13 = new TableCell(doc);
15
16// Append outline elements in the table cell
17cell11.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.1"));
18cell12.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.2"));
19cell13.AppendChildLast(GetOutlineElementWithText(doc, "cell_1.3"));
20// Table cells to rows
21row1.AppendChildLast(cell11);
22row1.AppendChildLast(cell12);
23row1.AppendChildLast(cell13);
24
25// Initialize TableRow class object
26TableRow row2 = new TableRow(doc);
27// initialize TableCell class objects
28TableCell cell21 = new TableCell(doc);
29TableCell cell22 = new TableCell(doc);
30TableCell cell23 = new TableCell(doc);
31
32// Append outline elements in the table cell
33cell21.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.1"));
34cell22.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.2"));
35cell23.AppendChildLast(GetOutlineElementWithText(doc, "cell_2.3"));
36
37// Append table cells to rows
38row2.AppendChildLast(cell21);
39row2.AppendChildLast(cell22);
40row2.AppendChildLast(cell23);
41
42// Initialize Table class object and set column widths
43Table table = new Table(doc)
44{
45    IsBordersVisible = true,
46    Columns = { new TableColumn { Width = 200 }, new TableColumn { Width = 200 }, new TableColumn { Width = 200 } }
47};
48// Append table rows to table
49table.AppendChildLast(row1);
50table.AppendChildLast(row2);
51
52// Initialize Outline object
53Outline outline = new Outline(doc);
54// Initialize OutlineElement object
55OutlineElement outlineElem = new OutlineElement(doc);
56// Add table to outline element node
57outlineElem.AppendChildLast(table);
58// Add outline element to outline
59outline.AppendChildLast(outlineElem);
60// Add outline to page node
61page.AppendChildLast(outline);
62// Add page to document node
63doc.AppendChildLast(page);
64dataDir = dataDir + "InsertTable_out.one";
65doc.Save(dataDir);

Create a Table with Locked Columns in the OneNote Document

Aspose.Note for .NET APIs allows developers to insert a table at the particular node position. This article is meant to show you how to create a table with a locked column in OneNote document programmatically.

Aspose.Note for .NET offers the Document class that represents a OneNote file. Developers can append content under TableCell node, table cells to the TableRow node, table row to the Table node. LockedWidth property of the Table class allows to bolt its width. Later they could append table under OutlineElement node, outline element to Outline node, outline to Page node and then a page to the Document node. It’s all based on the Aspose.Note DOM structure.

The following code example demonstrates how to insert a table with locked columns in a OneNote document.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Tables();
 3
 4// Create an object of the Document class
 5Document doc = new Document();
 6// Initialize Page class object
 7Aspose.Note.Page page = new Aspose.Note.Page(doc);
 8
 9// Initialize TableRow class object
10TableRow row1 = new TableRow(doc);
11// Initialize TableCell class object and set text content
12TableCell cell11 = new TableCell(doc);
13cell11.AppendChildLast(InsertTable.GetOutlineElementWithText(doc, "Small text"));
14row1.AppendChildLast(cell11);
15
16// Initialize TableRow class object
17TableRow row2 = new TableRow(doc);
18// Initialize TableCell class object and set text content
19TableCell cell21 = new TableCell(doc);
20cell21.AppendChildLast(InsertTable.GetOutlineElementWithText(doc, "Long   text    with    several   words and    spaces."));
21row2.AppendChildLast(cell21);
22
23// Initialize Table class object
24Table table = new Table(doc)
25{
26    IsBordersVisible = true,
27    Columns = { new TableColumn { Width = 70, LockedWidth = true } }
28};
29// Add rows
30table.AppendChildLast(row1);
31table.AppendChildLast(row2);
32
33Outline outline = new Outline(doc);
34OutlineElement outlineElem = new OutlineElement(doc);
35// Add table node
36outlineElem.AppendChildLast(table);
37// Add outline element node
38outline.AppendChildLast(outlineElem);
39// Add outline node
40page.AppendChildLast(outline);
41// Add page node
42doc.AppendChildLast(page);
43dataDir = dataDir + "CreateTableWithLockedColumns_out.one";
44doc.Save(dataDir);

GetOutlineElementWithText Method

1public static OutlineElement GetOutlineElementWithText(Document doc, string text)
2{
3    OutlineElement outlineElem = new OutlineElement(doc);
4    ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
5    outlineElem.AppendChildLast(new RichText(doc) { Text = text, ParagraphStyle = textStyle });
6    return outlineElem;
7}

Setting Cell Background Color

 1// Create an object of the Document class
 2Document doc = new Document();
 3// Initialize Page class object
 4Aspose.Note.Page page = new Aspose.Note.Page(doc);
 5
 6// Initialize TableRow class object
 7TableRow row1 = new TableRow(doc);
 8// Initialize TableCell class object and set text content
 9TableCell cell11 = new TableCell(doc);
10cell11.AppendChildLast(InsertTable.GetOutlineElementWithText(doc, "Small text"));
11cell11.BackgroundColor = Color.Coral;
12row1.AppendChildLast(cell11);  

Changing style of a table

 1        private static void SetRowStyle(TableRow row, Color highlightColor, bool bold, bool italic)
 2        {
 3            foreach (var cell in row)
 4            {
 5                cell.BackgroundColor = highlightColor;
 6
 7                foreach (var node in cell.GetChildNodes<RichText>())
 8                {
 9                    node.ParagraphStyle.IsBold = bold;
10                    node.ParagraphStyle.IsItalic = italic;
11
12                    foreach (var style in node.Styles)
13                    {
14                        style.IsBold = bold;
15                        style.IsItalic = italic;
16                    }
17                }
18            }
19        }
20        
21        [STAThread]
22        public static void Main()
23        {
24            string dataDir = RunExamples.GetDataDir_Tables();
25
26            // Load the document into Aspose.Note.
27            Document document = new Document(dataDir + "ChangeTableStyleIn.one");
28
29            // Get a list of table nodes
30            IList<Table> nodes = document.GetChildNodes<Table>();
31
32            foreach (Table table in nodes)
33            {
34                SetRowStyle(table.First(), Color.DarkGray, true, true);
35
36                // Highlight first row after head.
37                var flag = false;
38                foreach (var row in table.Skip(1))
39                {
40                    SetRowStyle(row, flag ? Color.LightGray : Color.Empty, false, false);
41
42                    flag = !flag;
43                }
44            }
45
46            document.Save(Path.Combine(dataDir, "ChangeTableStyleOut.one"));
47        }        
todo:image_alt_text

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.