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