//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Pdf.Graphics;usingGrapeCity.Documents.Text;usingGrapeCity.Documents.Drawing;usingGCTEXT = GrapeCity.Documents.Text;usingGCDRAW = GrapeCity.Documents.Drawing; namespaceDsPdfWeb.Demos{// This sample demonstrates how to use GcPdfGraphics.SoftMask// to draw semi-transparently and specify clipping.publicclassSoftMask1 {publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument();var page = doc.NewPage();var g = page.Graphics; var rc = Common.Util.AddNote("GcPdfGraphics has a SoftMask property, which allows creating a mask with a FormXObject, " +"draw on that object's Graphics using any supported drawing methods " +"(including semi-transparent drawing), and then use the result as a mask when drawing " +"on the document's pages. Only the alpha channel from the mask is used. " +"Solid areas do not mask, transparent areas mask completely, " +"semi-transparent areas mask in inverse proportion to the alpha value.", page); var rMask = newRectangleF(0, 0, 72 * 5, 72 * 2);var rDoc = newRectangleF(rc.Left, rc.Bottom + 36, rMask.Width, rMask.Height); var softMask = SoftMask.Create(doc, rDoc);var smGraphics = softMask.FormXObject.Graphics; smGraphics.FillEllipse(rMask, Color.FromArgb(128, Color.Black)); smGraphics.DrawString("SOLID TEXT",newTextFormat() { Font = StandardFonts.HelveticaBold, FontSize = 52, ForeColor = Color.Black },newRectangleF(rMask.X, rMask.Y, rMask.Width, rMask.Height),TextAlignment.Center, ParagraphAlignment.Center, false);var rt = rMask; rt.Inflate(-8, -8);// Color on the mask does not matter, only alpha channel is important: smGraphics.DrawEllipse(rt, Color.Red); g.SoftMask = softMask; g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), rDoc, null, ImageAlign.StretchImage);// NOTE: it looks like some PDF viewers (such as built-in browser viewers)// do not handle changing soft masks correctly unless the mask is reset prior// to assigning a new one, hence this: g.SoftMask = SoftMaskBase.None; rDoc.Offset(0, rDoc.Height + 12); rDoc.Width = rc.Width; rDoc.Height = 36; rMask.Height = rDoc.Height;// Draw a string on mask with increasing alpha values:for (int alpha = 16; alpha <= 255; alpha += 32) { softMask = SoftMask.Create(doc, rDoc); smGraphics = softMask.FormXObject.Graphics; smGraphics.DrawString($"Text drawn on mask with alpha {alpha}.",newTextFormat() { Font = StandardFonts.HelveticaBold, FontSize = 24, ForeColor = Color.FromArgb(alpha, Color.Black) },newRectangleF(rMask.X, rMask.Y, rMask.Width, rMask.Height),TextAlignment.Leading, ParagraphAlignment.Center, false); g.SoftMask = softMask; g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), rDoc, null, ImageAlign.StretchImage); g.SoftMask = SoftMaskBase.None; rDoc.Offset(0, rDoc.Height); } // Done: doc.Save(stream);return doc.Pages.Count; } }}