Gradients.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 GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsPdfWeb.Demos.Basics
  15. {
  16. // This sample shows how to create gradient fills
  17. // using LinearGradientBrush and RadialGradientBrush.
  18. public class Gradients
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. var doc = new GcPdfDocument();
  23. var g = doc.NewPage().Graphics;
  24. var testRectSize = new SizeF(72 * 4, 72);
  25. var dy = 12;
  26. // TextLayout to draw labels:
  27. var tl = g.CreateTextLayout();
  28. tl.DefaultFormat.Font = StandardFonts.Times;
  29. tl.DefaultFormat.FontSize = 18;
  30. tl.DefaultFormat.ForeColor = Color.Chartreuse;
  31. tl.MaxWidth = testRectSize.Width;
  32. tl.MaxHeight = testRectSize.Height;
  33. tl.TextAlignment = TextAlignment.Center;
  34. tl.ParagraphAlignment = ParagraphAlignment.Center;
  35. // Note 1:
  36. var rc = Common.Util.AddNote("Linear gradients using LinearGradientBrush:", doc.Pages.Last, new RectangleF(72, 36, 500, 100));
  37. // Text insertion point:
  38. var ip = new PointF(rc.Left, rc.Bottom + dy);
  39. // Local action to draw a gradient-filled rectangle:
  40. Action<GCDRAW.Brush, string> drawSwatch = (b_, txt_) =>
  41. {
  42. var rect = new RectangleF(ip, testRectSize);
  43. // Fill the rectangle with a gradient brush:
  44. g.FillRectangle(rect, b_);
  45. // Draw a border, text info etc:
  46. g.DrawRectangle(rect, Color.Magenta);
  47. tl.Clear();
  48. tl.Append(txt_);
  49. tl.MaxHeight = testRectSize.Height;
  50. tl.MaxWidth = testRectSize.Width;
  51. tl.PerformLayout(true);
  52. g.DrawTextLayout(tl, ip);
  53. ip.Y += rect.Height + dy;
  54. };
  55. // LinearGradientBrush:
  56. // Horizontal gradient:
  57. var linearGradBrush = new LinearGradientBrush(Color.Red, Color.Blue);
  58. drawSwatch(linearGradBrush, $"Linear gradient\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
  59. // Vertical gradient:
  60. linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Green, new PointF(0, 1));
  61. drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
  62. // Diagonal gradient (increase swatch height to better show diagonal):
  63. testRectSize.Height *= 2;
  64. linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Teal, new PointF(1, 1));
  65. drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
  66. // RadialGradientBrush
  67. rc = Common.Util.AddNote("Radial gradients using RadialGradientBrush:", doc.Pages.Last, new RectangleF(ip, new SizeF(500, 100)));
  68. ip.Y = rc.Bottom + dy;
  69. // Centered:
  70. var radialGradBrush = new RadialGradientBrush(Color.Orange, Color.Purple);
  71. drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
  72. // Center in bottom right corner:
  73. radialGradBrush = new RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, new PointF(1, 1));
  74. drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
  75. // Done:
  76. doc.Save(stream);
  77. return doc.Pages.Count;
  78. }
  79. }
  80. }
  81.