TableTextAlign.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 different
  20. // alignments of texts and paragraphs in table cells,
  21. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  22. public class TableTextAlign
  23. {
  24. public int CreatePDF(Stream stream)
  25. {
  26. var doc = new GcPdfDocument();
  27. var g = doc.NewPage().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.AnchorTopLeft(null, 30, 20);
  43.  
  44. var ta = new TableRenderer(g,
  45. rt, FixedTableSides.TopLeft,
  46. rowCount: 4,
  47. columnCount: 3,
  48. gridLineColor: Color.Black,
  49. gridLineWidth: 1,
  50. rowMinHeight: 28);
  51.  
  52. var columns = ta.ColumnRects;
  53. columns[0].SetWidth(150);
  54. columns[1].SetWidth(200);
  55. columns[2].SetWidth(200);
  56.  
  57. var fmtNorm = new TextFormat
  58. {
  59. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  60. FontSize = 25,
  61. FontSizeInGraphicUnits = true,
  62. FontFeatures = new FontFeature[] { new FontFeature(FeatureTag.dlig) }
  63. };
  64. var fmtOrange = new TextFormat(fmtNorm)
  65. {
  66. ForeColor = Color.Orange
  67. };
  68.  
  69. var cs = new CellStyle
  70. {
  71. PaddingLeftRight = 15,
  72. PaddingBottom = 3,
  73. TextAlignment = TextAlignment.Center,
  74. TextFormat = fmtNorm,
  75. CreateTextLayout = (g, cs, data) =>
  76. {
  77. var tl = g.CreateTextLayout();
  78. tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
  79. tl.Append((string)data, cs.TextFormat);
  80. return tl;
  81. }
  82. };
  83. ta.DefaultCellStyle = cs;
  84.  
  85. ta.AddCell(0, 0, "Column 1");
  86. ta.AddCell(0, 1, "Column 2");
  87. ta.AddCell(0, 2, "Column 3");
  88.  
  89. ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Far, FillColor = Color.LemonChiffon },
  90. 1, 0, "One-liner.");
  91. ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center },
  92. 1, 1, "Multi-line and centered text.");
  93. ta.AddCell(new CellStyle(cs) { TextAlignment = TextAlignment.Distributed },
  94. 1, 2, "A multi-line piece of text that is distributed within the table cell.");
  95.  
  96. ta.AddCell(2, 0, "Apple");
  97. ta.AddCell(2, 1, "Banana");
  98. ta.AddCell(new CellStyle(cs) { TextFormat = fmtOrange }, 2, 2, "Orange");
  99. ta.AddCell(3, 0, "Apple");
  100. ta.AddCell(3, 1, "Banana");
  101. ta.AddCell(3, 2, "Orange");
  102.  
  103. ta.Render();
  104. }
  105. }
  106. }
  107.