Show all table styles built into GcWordDocument (DOCX)

DOCX PDF TIFF SVG JPG PNG C# VB
BuiltInTableStyles.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. // Demo of all built-in table styles
  15. public class BuiltInTableStyles
  16. {
  17. public GcWordDocument CreateDocx()
  18. {
  19. var rows = 3;
  20. var cols = 4;
  21.  
  22. var doc = new GcWordDocument();
  23. var pars = doc.Body.Paragraphs;
  24.  
  25. pars.Add("Demo of All Built-in Table Styles", doc.Styles[BuiltInStyleId.Title]);
  26.  
  27. foreach (BuiltInStyleId id in Enum.GetValues(typeof(BuiltInStyleId)))
  28. {
  29. if (id == BuiltInStyleId.User)
  30. continue;
  31. var style = doc.Styles[id];
  32. if (style.Type != StyleType.Table)
  33. continue;
  34.  
  35. pars.Add($"The following table is formatted using style '{style.Name}':");
  36. var table = doc.Body.Tables.Add(cols, rows, style);
  37. for (int row = 0; row < rows; ++row)
  38. for (int col = 0; col < cols; ++col)
  39. table.Rows[row].Cells[col].GetRange().Paragraphs.First.GetRange().Runs.Add($"Cell ({row},{col})");
  40. }
  41.  
  42. // Done:
  43. return doc;
  44. }
  45. }
  46. }
  47.