RotatedText.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. Imports System.Numerics
  11.  
  12. '' Shows how to use GcPdfGraphics.Transform to rotate a text string.
  13. '' See also RotatedText2.
  14. Public Class RotatedText
  15. Function CreatePDF(ByVal stream As Stream) As Integer
  16. '' Rotation angle, degrees clockwise:
  17. Dim angle = -45.0F
  18. ''
  19. Dim doc = New GcPdfDocument()
  20. Dim g = doc.NewPage().Graphics
  21. '' Create a text layout, pick a font and font size:
  22. Dim tl = g.CreateTextLayout()
  23. tl.DefaultFormat.Font = StandardFonts.Times
  24. tl.DefaultFormat.FontSize = 24
  25. '' Add a text, and perform layout:
  26. tl.Append("Rotated text.")
  27. tl.PerformLayout(True)
  28. '' Text insertion point at (1",1"):
  29. Dim ip = New PointF(72, 72)
  30. '' Now that we have text size, create text rectangle with top left at insertion point:
  31. Dim rect = New RectangleF(ip.X, ip.Y, tl.ContentWidth, tl.ContentHeight)
  32. '' Rotate the text around its bounding rect's center:
  33. '' we now have the text size, and can rotate it about its center:
  34. g.Transform = Matrix3x2.CreateRotation((angle * Math.PI) / 180.0F, New Vector2(ip.X + tl.ContentWidth / 2, ip.Y + tl.ContentHeight / 2))
  35. '' Draw rotated text and bounding rectangle:
  36. g.DrawTextLayout(tl, ip)
  37. g.DrawRectangle(rect, Color.Black, 1)
  38. '' Remove rotation and draw the bounding rectangle where the non-rotated text would have been:
  39. g.Transform = Matrix3x2.Identity
  40. g.DrawRectangle(rect, Color.ForestGreen, 1)
  41. ''
  42. '' Done:
  43. doc.Save(stream)
  44. Return doc.Pages.Count
  45. End Function
  46. End Class
  47.