NestedTable.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Word;
  9.  
  10. namespace DsWordWeb.Demos
  11. {
  12. // This sample creates a simple table and adds another (nested)
  13. // table into a cell of the first table.
  14. public class NestedTable
  15. {
  16. public GcWordDocument CreateDocx()
  17. {
  18. GcWordDocument doc = new GcWordDocument();
  19.  
  20. doc.Body.Sections.First.GetRange().Paragraphs.Add("Here is a 5 columns by 3 rows table, with a nested table in cell (2, 2):");
  21. var t = doc.Body.Sections.First.GetRange().Tables.Add(new string[][]
  22. {
  23. new string[] { "row 1, col 1", "row 1, col 2", "row 1, col 3", "row 1, col 4", "row 1, col 5", },
  24. new string[] { "row 2, col 1", "row 2, col 2", "row 2, col 3", "row 2, col 4", "row 2, col 5", },
  25. new string[] { "row 3, col 1", "row 3, col 2", "row 3, col 3", "row 3, col 4", "row 3, col 5", },
  26. }
  27. );
  28. // Create a new table style:
  29. var ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
  30. // We can enumerate all table borders, including inside borders:
  31. foreach (var border in ts1.Table.Borders)
  32. {
  33. border.LineStyle = LineStyle.Triple;
  34. border.LineWidth = 0.5f;
  35. border.Color.RGB = Color.Purple;
  36. }
  37. // Assign the style to the table:
  38. t.Style = ts1;
  39.  
  40. // Overwrite inside border's line styles:
  41. ts1.Table.Borders.InsideHorizontal.LineStyle =
  42. ts1.Table.Borders.InsideVertical.LineStyle =
  43. LineStyle.Double;
  44. // Add some padding:
  45. ts1.Table.Padding.All = 3;
  46.  
  47. // Set up cell (1,1) to visually span two following cells:
  48. var cell = t.Rows[1].Cells[1];
  49. var tn = cell.GetRange().Tables.Add();
  50. cell.GetRange().Paragraphs.First.GetRange().Texts.First.Value = "Cell with nested table.";
  51.  
  52. tn.Rows.Add("Nested (1,1)", "Nested (1,2)", "Nested (1,3)");
  53. tn.Rows.Add("Nested (2,1)", "Nested (2,2)", "Nested (2,3)");
  54. tn.Rows.Add("Nested (3,1)", "Nested (3,2)", "Nested (3,3)");
  55.  
  56. // Done:
  57. return doc;
  58. }
  59. }
  60. }
  61.