SimpleTable.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 System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This sample creates a simple table,
  15. // and demonstrates the following table related tasks:
  16. // - Applying table styles (borders and padding);
  17. // - Adding rows and cells;
  18. // - Removing rows;
  19. // - Fetching text from a specific cell;
  20. // - Applying character style to a specific cell.
  21. public class SimpleTable
  22. {
  23. public GcWordDocument CreateDocx()
  24. {
  25. GcWordDocument doc = new GcWordDocument();
  26.  
  27. // Random-ish table dimensions:
  28. var rand = Util.NewRandom();
  29. var rows = rand.Next(15, 100);
  30. var cols = rand.Next(4, 6);
  31. var getrow = rand.Next(0, 10);
  32. var getcol = rand.Next(0, 4);
  33. var badrow = 12; // index 12 === number 13 :)
  34.  
  35. var section = doc.Body.Sections.First;
  36. var header = section.GetRange().Paragraphs.Add(
  37. $"A {cols} columns by {rows} rows table (note that the there's no 'row {badrow + 1}'):");
  38.  
  39. // Add an empty table:
  40. var t = section.GetRange().Tables.Add(0, 0);
  41.  
  42. // Add some rows and cells to it:
  43. var cells = new List<string>(cols);
  44. for (int col = 0; col < cols; ++col)
  45. cells.Add(string.Empty);
  46. for (int row = 0; row < rows; ++row)
  47. {
  48. for (int col = 0; col < cols; ++col)
  49. cells[col] = $"Row {row + 1}, col {col + 1}";
  50. t.Rows.Add(cells.ToArray());
  51. }
  52. // Remove a row:
  53. t.Rows[badrow].Delete();
  54.  
  55. // Create a new table style:
  56. var ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
  57. // Assign the style to the table:
  58. t.Style = ts1;
  59.  
  60. // We can enumerate all table borders, including inside borders:
  61. foreach (var border in ts1.Table.Borders)
  62. {
  63. border.LineStyle = LineStyle.Triple;
  64. border.LineWidth = 0.5f;
  65. border.Color.RGB = Color.Purple;
  66. }
  67. // Overwrite inside border's line styles:
  68. ts1.Table.Borders.InsideHorizontal.LineStyle =
  69. ts1.Table.Borders.InsideVertical.LineStyle =
  70. LineStyle.Double;
  71. // Add some cell padding:
  72. ts1.Table.Padding.All = 2;
  73.  
  74. // Finally, fetch the text from a certain cell and insert it before the table:
  75.  
  76. // Fetching the text from a cell: because we know exactly the structure of our table,
  77. // we can do this without any checks:
  78. // - get the cell at index 'getcol' in row 'getrow',
  79. // - first child fetches the paragraph,
  80. // - next child fetches the run,
  81. // - finally, next child fetches the actual text element:
  82. var text = t.Rows[getrow].Cells[getcol].Children.First().Children.First().Children.First() as Text;
  83. // Get the parent run:
  84. var run = text.ParentContent as Run;
  85. // Mark the found cell with bold/italic font:
  86. var rs1 = doc.Styles.Add("cell hi-light", StyleType.Character);
  87. rs1.Font.Bold = true;
  88. rs1.Font.Italic = true;
  89. run.Style = rs1;
  90. // Add the fetched text from cell (2,0) before the document's opening paragraph,
  91. // using the same character style:
  92. var pp = header.GetRange().Paragraphs.Insert(
  93. $"Text from cell at row {getrow + 1}, col {getcol + 1} (drawn with bold/italic font): \"", InsertLocation.Before);
  94. var r = pp.GetRange().Runs.Add(text.Value);
  95. r.Style = rs1;
  96. pp.GetRange().Runs.Add("\".");
  97.  
  98. // Done:
  99. return doc;
  100. }
  101. }
  102. }
  103.