NestedTableHelpers.cs
//
// This code is part of Document Solutions for Word samples.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This example shows how to create a simple table with a nested
    // table in one of the cells, using the new in v6.2 content creation
    // helper methods. The document created by this example is the same
    // as the one created by the NestedTable example, but uses the
    // helper methods for more robust and compact code.
    // The lines of code replaced by the new helper method calls are preserved
    // in the code of this example for reference, commented out with '//old:' comment.
    public class NestedTableHelpers
    {
        public GcWordDocument CreateDocx()
        {
            GcWordDocument doc = new GcWordDocument();

            //old: doc.Body.Sections.First.GetRange().Paragraphs.Add("Here is a 5 columns by 3 rows table, with a nested table in cell (2, 2):");
            doc.Body.AddParagraph("Here is a 5 columns by 3 rows table, with a nested table in cell (2, 2):");
            //old: var t = doc.Body.Sections.First.GetRange().Tables.Add(new string[][]
            var t = doc.Body.AddTable(new string[][]
                {
                    new string[] { "row 1, col 1", "row 1, col 2", "row 1, col 3", "row 1, col 4", "row 1, col 5",  },
                    new string[] { "row 2, col 1", "row 2, col 2", "row 2, col 3", "row 2, col 4", "row 2, col 5",  },
                    new string[] { "row 3, col 1", "row 3, col 2", "row 3, col 3", "row 3, col 4", "row 3, col 5",  },
                }
            );
            // Create a new table style:
            var ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
            // We can enumerate all table borders, including inside borders:
            foreach (var border in ts1.Table.Borders)
            {
                border.LineStyle = LineStyle.Triple;
                border.LineWidth = 0.5f;
                border.Color.RGB = Color.Purple;
            }
            // Assign the style to the table:
            t.Style = ts1;

            // Overwrite inside border's line styles:
            ts1.Table.Borders.InsideHorizontal.LineStyle = ts1.Table.Borders.InsideVertical.LineStyle = LineStyle.Double;
            // Add some padding:
            ts1.Table.Padding.All = 3;

            // Set up cell (1,1) to visually span two following cells:
            var cell = t.Rows[1].Cells[1];
            //old: var tn = cell.GetRange().Tables.Add();
            var tn = cell.AddTable();
            cell.GetRange().Paragraphs.First.GetRange().Texts.First.Value = "Cell with nested table.";

            tn.Rows.Add("Nested (1,1)", "Nested (1,2)", "Nested (1,3)");
            tn.Rows.Add("Nested (2,1)", "Nested (2,2)", "Nested (2,3)");
            tn.Rows.Add("Nested (3,1)", "Nested (3,2)", "Nested (3,3)");

            // Done:
            return doc;
        }
    }
}