RoundRectangle.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10.  
  11. '' This sample demonstrates how to draw round rectangles
  12. '' using dedicated DrawRoundRect/FillRoundRect methods.
  13. '' It also shows how the same result may be achieved with
  14. '' graphics paths.
  15. Public Class RoundRectangle
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. Dim doc = New GcPdfDocument()
  18. Dim page = doc.NewPage()
  19. Dim g = page.Graphics
  20.  
  21. Dim rc = Util.AddNote(
  22. "GcPdfGraphics has dedicated methods to easily draw and fill rectangles with rounded corners. " +
  23. "This sample also shows how the same result may be achieved using graphics paths. " +
  24. "While they are not really needed for drawing round rectangles, graphics paths allow " +
  25. "to draw and fill arbitrary figures with complex geometries.",
  26. page)
  27.  
  28. '' Rounded rectangle's radii:
  29. Dim rx = 36, ry = 24
  30.  
  31. '' Using dedicated methods to draw And fill round rectangles:
  32. Dim rEasy = New RectangleF(rc.Left, rc.Bottom + 36, 144, 72)
  33. g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen)
  34. g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4)
  35. '' Add a label:
  36. Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
  37. g.DrawString("The easy way.", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, False)
  38.  
  39. '' Using graphics path to achieve the same result:
  40. Dim rHard = rEasy
  41. rHard.Offset(0, rEasy.Height + 36)
  42. Dim path = MakeRoundRect(g, rHard, rx, ry)
  43. g.FillPath(path, Color.PaleVioletRed)
  44. g.DrawPath(path, Color.Purple, 4)
  45. '' Add a label:
  46. g.DrawString("The hard way.", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, False)
  47.  
  48. '' Done
  49. doc.Save(stream)
  50. Return doc.Pages.Count
  51. End Function
  52.  
  53. '' This method shows how to create a graphics path that may be used
  54. '' to fill Or draw arbitrary shapes on a GcGraphics.
  55. Private Function MakeRoundRect(ByVal g As GcGraphics, ByVal rc As RectangleF, ByVal rx As Single, ByVal ry As Single) As IPath
  56. Dim path = g.CreatePath()
  57. Dim sz = New SizeF(rx, ry)
  58. '' start from horizontal top left
  59. path.BeginFigure(New PointF(rc.Left + rx, rc.Top))
  60. path.AddLine(New PointF(rc.Right - rx, rc.Top))
  61. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Right, rc.Top + ry), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  62. path.AddLine(New PointF(rc.Right, rc.Bottom - ry))
  63. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Right - rx, rc.Bottom), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  64. path.AddLine(New PointF(rc.Left + rx, rc.Bottom))
  65. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Left, rc.Bottom - ry), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  66. path.AddLine(New PointF(rc.Left, rc.Top + ry))
  67. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Left + rx, rc.Top), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  68. path.EndFigure(FigureEnd.Closed)
  69. Return path
  70. End Function
  71. End Class
  72.