RotatedText.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.Drawing;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // Shows how to use GcPdfGraphics.Transform to rotate a text string.
  16. // See also RotatedText2.
  17. public class RotatedText
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. // Rotation angle, degrees clockwise:
  22. float angle = -45;
  23. //
  24. var doc = new GcPdfDocument();
  25. var g = doc.NewPage().Graphics;
  26. // Create a text layout, pick a font and font size:
  27. TextLayout tl = g.CreateTextLayout();
  28. tl.DefaultFormat.Font = StandardFonts.Times;
  29. tl.DefaultFormat.FontSize = 24;
  30. // Add a text, and perform layout:
  31. tl.Append("Rotated text.");
  32. tl.PerformLayout(true);
  33. // Text insertion point at (1",1"):
  34. var ip = new PointF(72, 72);
  35. // Now that we have text size, create text rectangle with top left at insertion point:
  36. var rect = new RectangleF(ip.X, ip.Y, tl.ContentWidth, tl.ContentHeight);
  37. // Rotate the text around its bounding rect's center:
  38. // we now have the text size, and can rotate it about its center:
  39. g.Transform = Matrix3x2.CreateRotation((float)(angle * Math.PI) / 180f, new Vector2(ip.X + tl.ContentWidth / 2, ip.Y + tl.ContentHeight / 2));
  40. // Draw rotated text and bounding rectangle:
  41. g.DrawTextLayout(tl, ip);
  42. g.DrawRectangle(rect, Color.Black, 1);
  43. // Remove rotation and draw the bounding rectangle where the non-rotated text would have been:
  44. g.Transform = Matrix3x2.Identity;
  45. g.DrawRectangle(rect, Color.ForestGreen, 1);
  46. //
  47. doc.Save(stream);
  48. return doc.Pages.Count;
  49. }
  50. }
  51. }
  52.