RoundRectangle.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 GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This sample demonstrates how to draw round rectangles
  15. // using dedicated DrawRoundRect/FillRoundRect methods.
  16. // It also shows how the same result may be achieved with
  17. // graphics paths.
  18. public class RoundRectangle
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. var doc = new GcPdfDocument();
  23. var page = doc.NewPage();
  24. var g = page.Graphics;
  25.  
  26. var rc = Common.Util.AddNote(
  27. "GcPdfGraphics has dedicated methods to easily draw and fill rectangles with rounded corners. " +
  28. "This sample also shows how the same result may be achieved using graphics paths. " +
  29. "While they are not really needed for drawing round rectangles, graphics paths allow " +
  30. "to draw and fill arbitrary figures with complex geometries.",
  31. page);
  32.  
  33. // Rounded rectangle's radii:
  34. float rx = 36, ry = 24;
  35.  
  36. // Using dedicated methods to draw and fill round rectangles:
  37. var rEasy = new RectangleF(rc.Left, rc.Bottom + 36, 144, 72);
  38. g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen);
  39. g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4);
  40. // Add a label:
  41. var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
  42. g.DrawString("The easy way.", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, false);
  43.  
  44. // Using graphics path to achieve the same result:
  45. var rHard = rEasy;
  46. rHard.Offset(0, rEasy.Height + 36);
  47. var path = MakeRoundRect(g, rHard, rx, ry);
  48. g.FillPath(path, Color.PaleVioletRed);
  49. g.DrawPath(path, Color.Purple, 4);
  50. // Add a label:
  51. g.DrawString("The hard way.", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, false);
  52.  
  53. // Done:
  54. doc.Save(stream);
  55. return doc.Pages.Count;
  56. }
  57.  
  58. // This method shows how to create a graphics path that may be used
  59. // to fill or draw arbitrary shapes on a GcGraphics.
  60. private static IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry)
  61. {
  62. var path = g.CreatePath();
  63. var sz = new SizeF(rx, ry);
  64. // Start from horizontal top left:
  65. path.BeginFigure(new PointF(rc.Left + rx, rc.Top));
  66. path.AddLine(new PointF(rc.Right - rx, rc.Top));
  67. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  68. path.AddLine(new PointF(rc.Right, rc.Bottom - ry));
  69. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz });
  70. path.AddLine(new PointF(rc.Left + rx, rc.Bottom));
  71. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  72. path.AddLine(new PointF(rc.Left, rc.Top + ry));
  73. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz });
  74. path.EndFigure(FigureEnd.Closed);
  75. return path;
  76. }
  77. }
  78. }
  79.