RotatedTableText.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Numerics;
  9. using GrapeCity.Documents.Drawing;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Imaging;
  12. using GrapeCity.Documents.Layout;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsImagingWeb.Demos
  17. {
  18. // This example shows how to draw a table with cells containing rotated texts,
  19. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  20. public class RotatedTableText
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  25. using var g = bmp.CreateGraphics(Color.White);
  26. DrawTable(g, pixelSize.Width, pixelSize.Height);
  27. return bmp;
  28. }
  29.  
  30. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  31. {
  32. var host = new LayoutHost();
  33. var view = host.CreateView(pageWidth, pageHeight);
  34.  
  35. var rt = view.CreateRect();
  36. rt.AnchorTopLeftRight(null, 30, 20, 20);
  37.  
  38. var ta = new TableRenderer(g,
  39. rt, FixedTableSides.TopLeftRight,
  40. rowCount: 9, columnCount: 7,
  41. gridLineColor: Color.Transparent,
  42. gridLineWidth: 1,
  43. rowMinHeight: 20,
  44. columnMinWidth: 20);
  45.  
  46. // We set a "star" (weighted) width for the seventh column so that the table
  47. // can expand since both the left and right sides of the table are fixed.
  48. ta.ColumnRects[6].SetStarWidth(1f);
  49.  
  50. ta.RowRects[8].AppendMinHeight(70);
  51.  
  52. var fmtNorm = new TextFormat
  53. {
  54. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  55. FontSize = 16f,
  56. FontSizeInGraphicUnits = true
  57. };
  58.  
  59. var cs = new CellStyle
  60. {
  61. LineWidth = 1,
  62. LineColor = Color.Coral,
  63. LinePaddingAll = 1,
  64. CornerRadius = 5,
  65. FillColor = Color.Snow,
  66. PaddingLeftRight = 10,
  67. PaddingTop = 2,
  68. PaddingBottom = 5,
  69. TextAlignment = TextAlignment.Center,
  70. TextFormat = fmtNorm
  71. };
  72. ta.DefaultCellStyle = cs;
  73.  
  74. ta.TableFrameStyle = new FrameStyle
  75. {
  76. LineWidth = 1,
  77. LineColor = Color.CornflowerBlue,
  78. LinePaddingAll = -3,
  79. CornerRadius = 5,
  80. FillColor = Color.MistyRose
  81. };
  82.  
  83. // To adjust a column width automatically we need to set
  84. // the FixedWidth property the CellStyle to false.
  85. var csFlexW = new CellStyle(cs)
  86. {
  87. FixedWidth = false
  88. };
  89. var cs270 = new CellStyle(cs)
  90. {
  91. RotationAngle = 270
  92. };
  93. // A rotated cell also has a fixed width (which is actually a height) by default.
  94. // If we set FixedWidth to false, the row height will be adjusted automatically.
  95. var cs270FlexH = new CellStyle(cs)
  96. {
  97. RotationAngle = 270,
  98. ParagraphAlignment = ParagraphAlignment.Center,
  99. FixedWidth = false,
  100. MaxWidth = 120
  101. };
  102.  
  103. ta.AddCell(0, 1, 1, 3, "Title 1 with a long text");
  104. ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center },
  105. 0, 4, 1, 3, "Title 2");
  106.  
  107. ta.AddCell(cs270, 1, 1, 2, 1, "Vertical Title 1");
  108. ta.AddCell(cs270, 1, 2, 2, 1, "Vertical Title 2 with additional text");
  109. ta.AddCell(cs270, 1, 3, 2, 1, "Vertical Title 3");
  110.  
  111. ta.AddCell(1, 4, 1, 2, "Subtitle 2.1");
  112.  
  113. ta.AddCell(cs270FlexH, 2, 4, "Vertical Subtitle 2.1.1");
  114. ta.AddCell(cs270FlexH, 2, 5, "Vertical Subtitle 2.1.2 with a long, long, long, and even longer text");
  115.  
  116. ta.AddCell(cs270, 3, 0, 3, 1, "Side Title 1");
  117. ta.AddCell(cs270, 6, 0, 2, 1, "Side Title 2");
  118.  
  119. for (int r = 3; r < 8; r++)
  120. for (int c = 1; c < 4; c++)
  121. ta.AddCell(csFlexW, r, c, (r * c).ToString());
  122.  
  123. for (int r = 3; r < 8; r++)
  124. for (int c = 4; c < 6; c++)
  125. ta.AddCell(csFlexW, r, c, $"row {r} column {c}");
  126.  
  127. ta.AddCell(new CellStyle(cs)
  128. {
  129. RotationAngle = 90,
  130. ParagraphAlignment = ParagraphAlignment.Far,
  131. TextAlignment = TextAlignment.Leading
  132. },
  133. 1, 6, 7, 1, "Other Side");
  134. ta.AddCell(new CellStyle(cs)
  135. {
  136. TextAlignment = TextAlignment.Trailing,
  137. ParagraphAlignment = ParagraphAlignment.Far
  138. },
  139. 8, 0, 1, 7, "Bottom Side");
  140.  
  141. ta.Render();
  142. }
  143. }
  144. }
  145.