EmptyTable.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 GrapeCity.Documents.Layout.Composition;
  15. using GCTEXT = GrapeCity.Documents.Text;
  16. using GCDRAW = GrapeCity.Documents.Drawing;
  17.  
  18. namespace DsPdfWeb.Demos
  19. {
  20. // This example shows how to draw a simple empty table,
  21. // using the GrapeCity.Documents.Layout.Composition.Surface and
  22. // GrapeCity.Documents.Drawing.TableRenderer classes.
  23. // This example highlights the main elements of the table layout
  24. // that can be created using the TableRenderer class.
  25. public class EmptyTable
  26. {
  27. public int CreatePDF(Stream stream)
  28. {
  29. var doc = new GcPdfDocument();
  30. var g = doc.NewPage().Graphics;
  31.  
  32. DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
  33.  
  34. // Save the PDF:
  35. doc.Save(stream);
  36. return doc.Pages.Count;
  37. }
  38.  
  39. static readonly TextFormat FormatDesc = new TextFormat()
  40. {
  41. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  42. FontSize = 16,
  43. FontSizeInGraphicUnits = true,
  44. ForeColor = Color.White
  45. };
  46.  
  47. static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
  48. {
  49. var surf = new Surface();
  50. var view = surf.CreateView(pageWidth, pageHeight);
  51.  
  52. // Fill the page with Gray color.
  53. view.CreateVisual((g, v) => {
  54. g.FillRectangle(v.AsRectF(), Color.Gray);
  55. }).LayoutRect.AnchorExact(null);
  56.  
  57. var vTable = view.CreateVisual();
  58. var tableRect = vTable.LayoutRect;
  59. tableRect.AnchorTopLeftRight(null, 36, 36, 36);
  60.  
  61. // The paddingAll parameter adds padding to the table grid relative to the table rectangle.
  62. var ta = new TableRenderer(g,
  63. tableRect, FixedTableSides.TopLeftRight,
  64. rowCount: 3,
  65. columnCount: 3,
  66. gridLineColor: Color.Yellow,
  67. gridLineWidth: 5,
  68. rowMinHeight: 100,
  69. paddingAll: 40);
  70.  
  71. var columns = ta.ColumnRects;
  72. columns[0].SetStarWidth(1);
  73. columns[1].SetWidth(100);
  74. columns[2].SetStarWidth(3);
  75.  
  76. ta.TableFrameStyle = new FrameStyle
  77. {
  78. LinePaddingAll = 20,
  79. LineColor = Color.SlateBlue,
  80. FillColor = Color.LightGreen,
  81. LineWidth = 10f
  82. };
  83.  
  84. ta.ApplyCellConstraints();
  85.  
  86. vTable.Draw = (g, v) =>
  87. {
  88. var rc = v.AsRectF();
  89. g.FillRectangle(rc, Color.White);
  90.  
  91. var tl = g.CreateTextLayout();
  92. tl.AppendLine("Table rectangle (tableRect) is White,", FormatDesc);
  93. tl.AppendLine("Padding of the table frame is 20 (TableFrameStyle.LinePaddingAll),", FormatDesc);
  94. tl.AppendLine("Frame line width is 10 (TableFrameStyle.LineWidth),", FormatDesc);
  95. tl.AppendLine("Frame line color is SlateBlue (TableFrameStyle.LineColor),", FormatDesc);
  96. tl.AppendLine("Frame area color is LightGreen (TableFrameStyle.FillColor),", FormatDesc);
  97. tl.AppendLine("Padding of the table grid is 40 (the paddingAll parameter),", FormatDesc);
  98. tl.AppendLine("Table grid line width is 5 (the gridLineWidth parameter),", FormatDesc);
  99. tl.AppendLine("Grid line color is Yellow (the gridLineColor parameter).", FormatDesc);
  100. g.DrawTextLayout(tl, new PointF(0, rc.Bottom + 10));
  101.  
  102. g.Transform = v.Layer.Surface.BaseTransform;
  103. ta.Render();
  104. };
  105.  
  106. surf.Render(g);
  107. }
  108. }
  109. }
  110.