NestedTableHelpers.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 example shows how to create a simple table with a nested
  13. // table in one of the cells, using the new in v6.2 content creation
  14. // helper methods. The document created by this example is the same
  15. // as the one created by the NestedTable example, but uses the
  16. // helper methods for more robust and compact code.
  17. // The lines of code replaced by the new helper method calls are preserved
  18. // in the code of this example for reference, commented out with '//old:' comment.
  19. public class NestedTableHelpers
  20. {
  21. public GcWordDocument CreateDocx()
  22. {
  23. GcWordDocument doc = new GcWordDocument();
  24.  
  25. //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):");
  26. doc.Body.AddParagraph("Here is a 5 columns by 3 rows table, with a nested table in cell (2, 2):");
  27. //old: var t = doc.Body.Sections.First.GetRange().Tables.Add(new string[][]
  28. var t = doc.Body.AddTable(new string[][]
  29. {
  30. new string[] { "row 1, col 1", "row 1, col 2", "row 1, col 3", "row 1, col 4", "row 1, col 5", },
  31. new string[] { "row 2, col 1", "row 2, col 2", "row 2, col 3", "row 2, col 4", "row 2, col 5", },
  32. new string[] { "row 3, col 1", "row 3, col 2", "row 3, col 3", "row 3, col 4", "row 3, col 5", },
  33. }
  34. );
  35. // Create a new table style:
  36. var ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
  37. // We can enumerate all table borders, including inside borders:
  38. foreach (var border in ts1.Table.Borders)
  39. {
  40. border.LineStyle = LineStyle.Triple;
  41. border.LineWidth = 0.5f;
  42. border.Color.RGB = Color.Purple;
  43. }
  44. // Assign the style to the table:
  45. t.Style = ts1;
  46.  
  47. // Overwrite inside border's line styles:
  48. ts1.Table.Borders.InsideHorizontal.LineStyle = ts1.Table.Borders.InsideVertical.LineStyle = LineStyle.Double;
  49. // Add some padding:
  50. ts1.Table.Padding.All = 3;
  51.  
  52. // Set up cell (1,1) to visually span two following cells:
  53. var cell = t.Rows[1].Cells[1];
  54. //old: var tn = cell.GetRange().Tables.Add();
  55. var tn = cell.AddTable();
  56. cell.GetRange().Paragraphs.First.GetRange().Texts.First.Value = "Cell with nested table.";
  57.  
  58. tn.Rows.Add("Nested (1,1)", "Nested (1,2)", "Nested (1,3)");
  59. tn.Rows.Add("Nested (2,1)", "Nested (2,2)", "Nested (2,3)");
  60. tn.Rows.Add("Nested (3,1)", "Nested (3,2)", "Nested (3,3)");
  61.  
  62. // Done:
  63. return doc;
  64. }
  65. }
  66. }
  67.