'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsGrapeCity.Documents.PdfImportsGrapeCity.Documents.Pdf.GraphicsImportsGrapeCity.Documents.TextImportsGrapeCity.Documents.DrawingImports GCDRAW = GrapeCity.Documents.Drawing '' This sample demonstrates how to use GcPdfGraphics.SoftMask'' to draw semi-transparently and specify clipping.PublicClassSoftMask1FunctionCreatePDF(ByVal stream AsStream) AsIntegerDim doc = NewGcPdfDocument()Dim page = doc.NewPage()Dim g = page.Graphics Dim rc = 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) Dim rMask = NewRectangleF(0, 0, 72 * 5, 72 * 2)Dim rDoc = NewRectangleF(rc.Left, rc.Bottom + 36, rMask.Width, rMask.Height) Dim sMask = SoftMask.Create(doc, rDoc)Dim smGraphics = sMask.FormXObject.Graphics smGraphics.FillEllipse(rMask, Color.FromArgb(128, Color.Black)) smGraphics.DrawString("SOLID TEXT",NewTextFormat() With {.Font = StandardFonts.HelveticaBold, .FontSize = 52, .ForeColor = Color.Black},NewRectangleF(rMask.X, rMask.Y, rMask.Width, rMask.Height),TextAlignment.Center, ParagraphAlignment.Center, False)Dim 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 = sMask g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), rDoc, Nothing, 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 alpha = 16To255Step32 sMask = SoftMask.Create(doc, rDoc) smGraphics = sMask.FormXObject.Graphics smGraphics.DrawString($"Text drawn on mask with alpha {alpha}.",NewTextFormat() With {.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 = sMask g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), rDoc, Nothing, ImageAlign.StretchImage) g.SoftMask = SoftMaskBase.None rDoc.Offset(0, rDoc.Height)Next '' Done: doc.Save(stream)Return doc.Pages.CountEndFunctionEndClass