BuiltInTableStylesHelpers.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 example demonstrates all built-in table styles, using the new in v6.2 content creation
  15. // helper methods. The document created by this example is the same
  16. // as the one created by the BuiltInTableStyles example, but uses the
  17. // helper methods for more robust and compact code.
  18. // The lines of code replaced by the new helper method calls are preserved
  19. // in the code of this example for reference, commented out with '//old:' comment.
  20. public class BuiltInTableStylesHelpers
  21. {
  22. public GcWordDocument CreateDocx()
  23. {
  24. var rows = 3;
  25. var cols = 4;
  26.  
  27. var doc = new GcWordDocument();
  28. var pars = doc.Body.Paragraphs;
  29.  
  30. pars.Add("Demo of All Built-in Table Styles", doc.Styles[BuiltInStyleId.Title]);
  31.  
  32. foreach (BuiltInStyleId id in Enum.GetValues(typeof(BuiltInStyleId)))
  33. {
  34. if (id == BuiltInStyleId.User)
  35. continue;
  36. var style = doc.Styles[id];
  37. if (style.Type != StyleType.Table)
  38. continue;
  39.  
  40. pars.Add($"The following table is formatted using style '{style.Name}':");
  41. var table = doc.Body.Tables.Add(cols, rows, style);
  42. for (int row = 0; row < rows; ++row)
  43. for (int col = 0; col < cols; ++col)
  44. {
  45. //old: table.Rows[row].Cells[col].GetRange().Paragraphs.First.GetRange().Runs.Add($"Cell ({row},{col})");
  46. table.Rows[row].Cells[col].GetRange().Paragraphs.First.AddRun($"Cell ({row},{col})");
  47. }
  48. }
  49.  
  50. // Done:
  51. return doc;
  52. }
  53. }
  54. }
  55.