SoftMask1.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.Pdf.Graphics;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos
  16. {
  17. // This sample demonstrates how to use GcPdfGraphics.SoftMask
  18. // to draw semi-transparently and specify clipping.
  19. public class SoftMask1
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. var doc = new GcPdfDocument();
  24. var page = doc.NewPage();
  25. var g = page.Graphics;
  26.  
  27. var rc = Common.Util.AddNote(
  28. "GcPdfGraphics has a SoftMask property, which allows creating a mask with a FormXObject, " +
  29. "draw on that object's Graphics using any supported drawing methods " +
  30. "(including semi-transparent drawing), and then use the result as a mask when drawing " +
  31. "on the document's pages. Only the alpha channel from the mask is used. " +
  32. "Solid areas do not mask, transparent areas mask completely, " +
  33. "semi-transparent areas mask in inverse proportion to the alpha value.",
  34. page);
  35.  
  36. var rMask = new RectangleF(0, 0, 72 * 5, 72 * 2);
  37. var rDoc = new RectangleF(rc.Left, rc.Bottom + 36, rMask.Width, rMask.Height);
  38.  
  39. var softMask = SoftMask.Create(doc, rDoc);
  40. var smGraphics = softMask.FormXObject.Graphics;
  41. smGraphics.FillEllipse(rMask, Color.FromArgb(128, Color.Black));
  42. smGraphics.DrawString("SOLID TEXT",
  43. new TextFormat() { Font = StandardFonts.HelveticaBold, FontSize = 52, ForeColor = Color.Black },
  44. new RectangleF(rMask.X, rMask.Y, rMask.Width, rMask.Height),
  45. TextAlignment.Center, ParagraphAlignment.Center, false);
  46. var rt = rMask;
  47. rt.Inflate(-8, -8);
  48. // Color on the mask does not matter, only alpha channel is important:
  49. smGraphics.DrawEllipse(rt, Color.Red);
  50.  
  51. g.SoftMask = softMask;
  52. g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), rDoc, null, ImageAlign.StretchImage);
  53. // NOTE: it looks like some PDF viewers (such as built-in browser viewers)
  54. // do not handle changing soft masks correctly unless the mask is reset prior
  55. // to assigning a new one, hence this:
  56. g.SoftMask = SoftMaskBase.None;
  57.  
  58. rDoc.Offset(0, rDoc.Height + 12);
  59. rDoc.Width = rc.Width;
  60. rDoc.Height = 36;
  61. rMask.Height = rDoc.Height;
  62.  
  63. for (int alpha = 16; alpha <= 255; alpha += 32)
  64. {
  65. softMask = SoftMask.Create(doc, rDoc);
  66. smGraphics = softMask.FormXObject.Graphics;
  67. smGraphics.DrawString($"Text drawn on mask with alpha {alpha}.",
  68. new TextFormat() { Font = StandardFonts.HelveticaBold, FontSize = 24, ForeColor = Color.FromArgb(alpha, Color.Black) },
  69. new RectangleF(rMask.X, rMask.Y, rMask.Width, rMask.Height),
  70. TextAlignment.Leading, ParagraphAlignment.Center, false);
  71. g.SoftMask = softMask;
  72. g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), rDoc, null, ImageAlign.StretchImage);
  73. g.SoftMask = SoftMaskBase.None;
  74. rDoc.Offset(0, rDoc.Height);
  75. }
  76.  
  77. // Done:
  78. doc.Save(stream);
  79. return doc.Pages.Count;
  80. }
  81. }
  82. }
  83.