TableTextAlign.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 different
  19. // alignments of texts and paragraphs in table cells,
  20. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  21. public class TableTextAlign
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  26. using var g = bmp.CreateGraphics(Color.White);
  27. DrawTable(g, pixelSize.Width, pixelSize.Height);
  28. return bmp;
  29. }
  30.  
  31. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  32. {
  33. var host = new LayoutHost();
  34. var view = host.CreateView(pageWidth, pageHeight);
  35.  
  36. var rt = view.CreateRect();
  37. rt.AnchorTopLeft(null, 30, 20);
  38.  
  39. var ta = new TableRenderer(g,
  40. rt, FixedTableSides.TopLeft,
  41. rowCount: 4,
  42. columnCount: 3,
  43. gridLineColor: Color.Black,
  44. gridLineWidth: 1,
  45. rowMinHeight: 28);
  46.  
  47. var columns = ta.ColumnRects;
  48. columns[0].SetWidth(150);
  49. columns[1].SetWidth(200);
  50. columns[2].SetWidth(200);
  51.  
  52. var fmtNorm = new TextFormat
  53. {
  54. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  55. FontSize = 25,
  56. FontSizeInGraphicUnits = true,
  57. FontFeatures = new FontFeature[] { new FontFeature(FeatureTag.dlig) }
  58. };
  59. var fmtOrange = new TextFormat(fmtNorm)
  60. {
  61. ForeColor = Color.Orange
  62. };
  63.  
  64. var cs = new CellStyle
  65. {
  66. PaddingLeftRight = 15,
  67. PaddingBottom = 3,
  68. TextAlignment = TextAlignment.Center,
  69. TextFormat = fmtNorm,
  70. CreateTextLayout = (g, cs, data) =>
  71. {
  72. var tl = g.CreateTextLayout();
  73. tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
  74. tl.Append((string)data, cs.TextFormat);
  75. return tl;
  76. }
  77. };
  78.  
  79. // Setting the default cell style allows us to call the AddCell
  80. // method without passing the explicit CellStyle object.
  81. ta.DefaultCellStyle = cs;
  82.  
  83. ta.AddCell(0, 0, "Column 1");
  84. ta.AddCell(0, 1, "Column 2");
  85. ta.AddCell(0, 2, "Column 3");
  86.  
  87. ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Far, FillColor = Color.LemonChiffon },
  88. 1, 0, "One-liner.");
  89. ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center },
  90. 1, 1, "Multi-line and centered text.");
  91. ta.AddCell(new CellStyle(cs) { TextAlignment = TextAlignment.Distributed },
  92. 1, 2, "A multi-line piece of text that is distributed within the table cell.");
  93.  
  94. ta.AddCell(2, 0, "Apple");
  95. ta.AddCell(2, 1, "Banana");
  96. ta.AddCell(new CellStyle(cs) { TextFormat = fmtOrange }, 2, 2, "Orange");
  97. ta.AddCell(3, 0, "Apple");
  98. ta.AddCell(3, 1, "Banana");
  99. ta.AddCell(3, 2, "Orange");
  100.  
  101. ta.Render();
  102. }
  103. }
  104. }
  105.