SoftMask1.vb
 '' '' This code is part of Document Solutions for PDF demos. '' Copyright (c) MESCIUS inc. All rights reserved. '' Imports System.IO Imports System.Drawing Imports GrapeCity.Documents.Pdf Imports GrapeCity.Documents.Pdf.Graphics Imports GrapeCity.Documents.Text Imports GrapeCity.Documents.Drawing Imports GCTEXT = GrapeCity.Documents.Text Imports GCDRAW = GrapeCity.Documents.Drawing '' This sample demonstrates how to use GcPdfGraphics.SoftMask '' to draw semi-transparently and specify clipping. Public Class SoftMask1 Function CreatePDF(ByVal stream As Stream) As Integer Dim doc = New GcPdfDocument() 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 = New RectangleF(0, 0, 72 * 5, 72 * 2) Dim rDoc = New RectangleF(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", New TextFormat() With {.Font = StandardFonts.HelveticaBold, .FontSize = 52, .ForeColor = Color.Black}, New RectangleF(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 = 16 To 255 Step 32 sMask = SoftMask.Create(doc, rDoc) smGraphics = sMask.FormXObject.Graphics smGraphics.DrawString($"Text drawn on mask with alpha {alpha}.", New TextFormat() With {.Font = StandardFonts.HelveticaBold, .FontSize = 24, .ForeColor = Color.FromArgb(alpha, Color.Black)}, New RectangleF(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.Count End Function End Class