TableRoundBorders.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 rounded
  20. // table and cell borders,
  21. // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
  22. public class TableRoundBorders
  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.AnchorTopLeftRight(null, 36, 36, 36);
  43.  
  44. var ta = new TableRenderer(g,
  45. rt, FixedTableSides.TopLeftRight,
  46. rowCount: 5,
  47. columnCount: 4,
  48. gridLineColor: Color.Transparent,
  49. gridLineWidth: 1,
  50. rowMinHeight: 30,
  51. paddingAll: 3)
  52. {
  53. TableFrameStyle = new FrameStyle
  54. {
  55. FillColor = Color.AliceBlue,
  56. LineColor = Color.CornflowerBlue,
  57. LineWidth = 1,
  58. CornerRadius = 5
  59. }
  60. };
  61.  
  62. var columns = ta.ColumnRects;
  63. columns[0].SetStarWidth(1);
  64. columns[1].SetStarWidth(5);
  65. columns[2].SetStarWidth(2);
  66. columns[3].SetStarWidth(3);
  67.  
  68. var fmt = new TextFormat
  69. {
  70. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
  71. ForeColor = Color.CornflowerBlue,
  72. FontSize = 16,
  73. FontSizeInGraphicUnits = true
  74. };
  75. var csNormal = new CellStyle
  76. {
  77. TextFormat = fmt,
  78. ParagraphAlignment = ParagraphAlignment.Center,
  79. PaddingLeftRight = 10,
  80. FillColor = Color.MistyRose,
  81. LineColor = Color.CornflowerBlue,
  82. LinePaddingAll = 2,
  83. LineWidth = 1,
  84. CornerRadius = 5
  85. };
  86. var csCenter = new CellStyle(csNormal)
  87. {
  88. TextAlignment = TextAlignment.Center,
  89. PaddingLeftRight = 0,
  90. };
  91. var csHeader = new CellStyle(csCenter)
  92. {
  93. TextFormat = new TextFormat(fmt)
  94. {
  95. ForeColor = Color.White,
  96. FontBold = true
  97. },
  98. FillColor = Color.LightBlue
  99. };
  100.  
  101. ta.AddCell(csHeader, 0, 0, "#");
  102. ta.AddCell(csHeader, 0, 1, "Name");
  103. ta.AddCell(csHeader, 0, 2, "Age");
  104. ta.AddCell(csHeader, 0, 3, "Country");
  105.  
  106. ta.AddCell(csCenter, 1, 0, "1.");
  107. ta.AddCell(csNormal, 1, 1, "Alice");
  108. ta.AddCell(csCenter, 1, 2, "25");
  109. ta.AddCell(csNormal, 1, 3, "Spain");
  110.  
  111. ta.AddCell(csCenter, 2, 0, "2.");
  112. ta.AddCell(csNormal, 2, 1, "Bob");
  113. ta.AddCell(csCenter, 2, 2, "36");
  114. ta.AddCell(csNormal, 2, 3, "Germany");
  115.  
  116. ta.AddCell(csCenter, 3, 0, "3.");
  117. ta.AddCell(csNormal, 3, 1, "Ken");
  118. ta.AddCell(csCenter, 3, 2, "5");
  119. ta.AddCell(csNormal, 3, 3, "Brazil");
  120.  
  121. ta.AddCell(csCenter, 4, 0, "4.");
  122. ta.AddCell(csNormal, 4, 1, "Teddy");
  123. ta.AddCell(csCenter, 4, 2, "12");
  124. ta.AddCell(csNormal, 4, 3, "Mexico");
  125.  
  126. ta.ApplyCellConstraints();
  127.  
  128. ta.Render();
  129. }
  130. }
  131. }
  132.