Transforms.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 System.Numerics
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Drawing
  11.  
  12. '' Shows how to use graphics transformations (GcPdfGraphics.Transform property).
  13. Public Class Transforms
  14. '' Helper method drawing a filled box with text:
  15. Private Sub DrawBox(ByVal text As String, ByVal g As GcGraphics, ByVal box As RectangleF)
  16. g.FillRectangle(box, Color.FromArgb(80, 0, 184, 204))
  17. g.DrawRectangle(box, Color.FromArgb(0, 193, 213), 1)
  18. box.Inflate(-6, -6)
  19. g.DrawString(text, New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}, box)
  20. End Sub
  21.  
  22. Function CreatePDF(ByVal stream As Stream) As Integer
  23. Const baseTxt = "Text drawn at (0,36) in a 4""x2"" box"
  24. Dim doc = New GcPdfDocument()
  25. Dim page = doc.NewPage()
  26. Dim g = page.Graphics
  27. Dim box = New RectangleF(0, 36, 72 * 4, 72 * 2)
  28. '' #1:
  29. DrawBox($"Box 1: {baseTxt}, no transformations.", g, box)
  30. ''
  31. Dim translate0 = Matrix3x2.CreateTranslation(72 * 1, 72 * 4)
  32. Dim scale0 = Matrix3x2.CreateScale(0.5F)
  33.  
  34. '' Transforms are applied in order from last to first.
  35.  
  36. '' #2:
  37. g.Transform =
  38. scale0 *
  39. translate0
  40. DrawBox($"Box 2: {baseTxt}, translated by (1"",4"") and scaled by 0.5.", g, box)
  41. '' #3:
  42. g.Transform =
  43. translate0 *
  44. scale0
  45. DrawBox($"Box 3: {baseTxt}, scaled by 0.5 and translated by (1"",4"").", g, box)
  46. ''
  47. Dim translate1 = Matrix3x2.CreateTranslation(72 * 3, 72 * 5)
  48. Dim scale1 = Matrix3x2.CreateScale(0.7F)
  49. Dim rotate0 = Matrix3x2.CreateRotation((-70 * Math.PI) / 180.0F) '' 70 degrees CCW
  50. '' #4:
  51. g.Transform =
  52. rotate0 *
  53. translate1 *
  54. scale1
  55. DrawBox($"Box 4: {baseTxt}, scaled by 0.7, translated by (3"",5""), and rotated 70 degrees counterclockwise.", g, box)
  56. '' #5:
  57. g.Transform =
  58. Matrix3x2.CreateTranslation(36, 72) *
  59. g.Transform
  60. DrawBox($"Box 5: {baseTxt}, applied current transform (Box 4), and translated by (1/2"",1"").", g, box)
  61. '' #6:
  62. g.Transform =
  63. Matrix3x2.CreateSkew((-45 * Math.PI) / 180.0F, (20 * Math.PI) / 180.0F) *
  64. Matrix3x2.CreateTranslation(72 * 3, 72 * 7)
  65. DrawBox($"Box 6: {baseTxt}, translated by (3"",7""), and skewed -45 degrees on axis X and 20 degrees on axis Y.", g, box)
  66. '' #7:
  67. g.Transform =
  68. Matrix3x2.CreateRotation(Math.PI) *
  69. Matrix3x2.CreateTranslation(page.Size.Width - 72, page.Size.Height - 72)
  70. DrawBox($"Box 7: {baseTxt}, translated by (7.5"",10""), and rotated by 180 degrees.", g, box)
  71. '' We can remove any transformations on a graphics like so:
  72. g.Transform = Matrix3x2.Identity
  73. ''
  74. '' Done:
  75. doc.Save(stream)
  76. Return doc.Pages.Count
  77. End Function
  78. End Class
  79.