Transforms.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 graphics transformations (GcPdfGraphics.Transform property).
  16. public class Transforms
  17. {
  18. // Helper method drawing a filled box with text:
  19. private static void DrawBox(string text, GcGraphics g, RectangleF box)
  20. {
  21. g.FillRectangle(box, Color.FromArgb(80, 0, 184, 204));
  22. g.DrawRectangle(box, Color.FromArgb(0, 193, 213), 1);
  23. box.Inflate(-6, -6);
  24. g.DrawString(text, new TextFormat() { Font = StandardFonts.Times, FontSize = 14, }, box);
  25. }
  26.  
  27. public int CreatePDF(Stream stream)
  28. {
  29. const string baseTxt = "Text drawn at (0,36) in a 4\"x2\" box";
  30. var doc = new GcPdfDocument();
  31. var page = doc.NewPage();
  32. var g = page.Graphics;
  33. var box = new RectangleF(0, 36, 72 * 4, 72 * 2);
  34. // #1:
  35. DrawBox($"Box 1: {baseTxt}, no transformations.", g, box);
  36. //
  37. var translate0 = Matrix3x2.CreateTranslation(72 * 1, 72 * 4);
  38. var scale0 = Matrix3x2.CreateScale(0.5f);
  39.  
  40. // Transforms are applied in order from last to first.
  41.  
  42. // #2:
  43. g.Transform =
  44. scale0 *
  45. translate0;
  46. DrawBox($"Box 2: {baseTxt}, translated by (1\",4\") and scaled by 0.5.", g, box);
  47. // #3:
  48. g.Transform =
  49. translate0 *
  50. scale0;
  51. DrawBox($"Box 3: {baseTxt}, scaled by 0.5 and translated by (1\",4\").", g, box);
  52. //
  53. var translate1 = Matrix3x2.CreateTranslation(72 * 3, 72 * 5);
  54. var scale1 = Matrix3x2.CreateScale(0.7f);
  55. var rotate0 = Matrix3x2.CreateRotation((float)(-70 * Math.PI) / 180f); // 70 degrees CCW
  56. // #4:
  57. g.Transform =
  58. rotate0 *
  59. translate1 *
  60. scale1;
  61. DrawBox($"Box 4: {baseTxt}, scaled by 0.7, translated by (3\",5\"), and rotated 70 degrees counterclockwise.", g, box);
  62. // #5:
  63. g.Transform =
  64. Matrix3x2.CreateTranslation(36, 72) *
  65. g.Transform;
  66. DrawBox($"Box 5: {baseTxt}, applied current transform (Box 4), and translated by (1/2\",1\").", g, box);
  67. // #6:
  68. g.Transform =
  69. // rotate0 *
  70. Matrix3x2.CreateSkew((float)(-45 * Math.PI) / 180f, (float)(20 * Math.PI) / 180f) *
  71. Matrix3x2.CreateTranslation(72 * 3, 72 * 7);
  72. DrawBox($"Box 6: {baseTxt}, translated by (3\",7\"), and skewed -45 degrees on axis X and 20 degrees on axis Y.", g, box);
  73. // #7:
  74. g.Transform =
  75. Matrix3x2.CreateRotation((float)Math.PI) *
  76. Matrix3x2.CreateTranslation(page.Size.Width - 72, page.Size.Height - 72);
  77. DrawBox($"Box 7: {baseTxt}, translated by (7.5\",10\"), and rotated by 180 degrees.", g, box);
  78. // We can remove any transformations on a graphics like so:
  79. g.Transform = Matrix3x2.Identity;
  80.  
  81. // Done:
  82. doc.Save(stream);
  83. return doc.Pages.Count;
  84. }
  85. }
  86. }
  87.