RotatedTableText.cs
  1. //
  2. // This code is part of Document Solutions for PDF 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.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Common;
  12. using GrapeCity.Documents.Drawing;
  13. using GrapeCity.Documents.Layout;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This example shows how to draw a table with cells containing rotated texts,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class RotatedTableText
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. var doc = new GcPdfDocument();
  26. var p = doc.Pages.Add(new SizeF(doc.PageSize.Height, doc.PageSize.Width));
  27. var g = p.Graphics;
  28.  
  29. DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
  30.  
  31. // Save the PDF:
  32. doc.Save(stream);
  33. return doc.Pages.Count;
  34. }
  35.  
  36. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  37. {
  38. var host = new LayoutHost();
  39. var view = host.CreateView(pageWidth, pageHeight);
  40.  
  41. var rt = view.CreateRect();
  42. rt.AnchorTopLeftRight(null, 30, 20, 20);
  43.  
  44. var ta = new TableRenderer(g,
  45. rt, FixedTableSides.TopLeftRight,
  46. rowCount: 9, columnCount: 7,
  47. gridLineColor: Color.Transparent,
  48. gridLineWidth: 1,
  49. rowMinHeight: 20,
  50. columnMinWidth: 20);
  51.  
  52. ta.ColumnRects[6].SetStarWidth(1f);
  53. ta.RowRects[8].AppendMinHeight(70);
  54.  
  55. var fmtNorm = new TextFormat
  56. {
  57. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  58. FontSize = 16f,
  59. FontSizeInGraphicUnits = true
  60. };
  61.  
  62. var cs = new CellStyle
  63. {
  64. LineWidth = 1,
  65. LineColor = Color.Coral,
  66. LinePaddingAll = 1,
  67. CornerRadius = 5,
  68. FillColor = Color.Snow,
  69. PaddingLeftRight = 10,
  70. PaddingTop = 2,
  71. PaddingBottom = 5,
  72. TextAlignment = TextAlignment.Center,
  73. TextFormat = fmtNorm
  74. };
  75. ta.DefaultCellStyle = cs;
  76.  
  77. ta.TableFrameStyle = new FrameStyle
  78. {
  79. LineWidth = 1,
  80. LineColor = Color.CornflowerBlue,
  81. LinePaddingAll = -3,
  82. CornerRadius = 5,
  83. FillColor = Color.MistyRose
  84. };
  85.  
  86. var csFlexW = new CellStyle(cs)
  87. {
  88. FixedWidth = false
  89. };
  90. var cs270 = new CellStyle(cs)
  91. {
  92. RotationAngle = 270
  93. };
  94. var cs270FlexH = new CellStyle(cs)
  95. {
  96. RotationAngle = 270,
  97. ParagraphAlignment = ParagraphAlignment.Center,
  98. FixedWidth = false,
  99. MaxWidth = 120
  100. };
  101.  
  102. ta.AddCell(0, 1, 1, 3, "Title 1 with a long text");
  103. ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center },
  104. 0, 4, 1, 3, "Title 2");
  105.  
  106. ta.AddCell(cs270, 1, 1, 2, 1, "Vertical Title 1");
  107. ta.AddCell(cs270, 1, 2, 2, 1, "Vertical Title 2 with additional text");
  108. ta.AddCell(cs270, 1, 3, 2, 1, "Vertical Title 3");
  109.  
  110. ta.AddCell(1, 4, 1, 2, "Subtitle 2.1");
  111.  
  112. ta.AddCell(cs270FlexH, 2, 4, "Vertical Subtitle 2.1.1");
  113. ta.AddCell(cs270FlexH, 2, 5, "Vertical Subtitle 2.1.2 with a long, long, long, and even longer text");
  114.  
  115. ta.AddCell(cs270, 3, 0, 3, 1, "Side Title 1");
  116. ta.AddCell(cs270, 6, 0, 2, 1, "Side Title 2");
  117.  
  118. for (int r = 3; r < 8; r++)
  119. for (int c = 1; c < 4; c++)
  120. ta.AddCell(csFlexW, r, c, (r * c).ToString());
  121.  
  122. for (int r = 3; r < 8; r++)
  123. for (int c = 4; c < 6; c++)
  124. ta.AddCell(csFlexW, r, c, $"row {r} column {c}");
  125.  
  126. ta.AddCell(new CellStyle(cs)
  127. {
  128. RotationAngle = 90,
  129. ParagraphAlignment = ParagraphAlignment.Far,
  130. TextAlignment = TextAlignment.Leading
  131. },
  132. 1, 6, 7, 1, "Other Side");
  133. ta.AddCell(new CellStyle(cs)
  134. {
  135. TextAlignment = TextAlignment.Trailing,
  136. ParagraphAlignment = ParagraphAlignment.Far
  137. },
  138. 8, 0, 1, 7, "Bottom Side");
  139.  
  140. ta.Render();
  141. }
  142. }
  143. }
  144.