SoftMask1.vb
'' 完毕:
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

'' 此示例演示了如何使用 GcPdfGraphics.SoftMask
'' 半透明绘制并指定剪切。
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 有一个 SoftMask 属性,它允许使用 FormXObject 创建蒙版," +
            "",
            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)
        '' 遮罩上的颜色并不重要,只有 Alpha 通道很重要:
        smGraphics.DrawEllipse(rt, Color.Red)

        g.SoftMask = sMask
        g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")), rDoc, Nothing, ImageAlign.StretchImage)
        '' 注意:它看起来像一些PDF查看器(例如内置浏览器查看器)
        '' 除非事先重置掩码,否则不要正确处理更改软掩码
        '' 分配一个新的,因此:
        g.SoftMask = SoftMaskBase.None

        rDoc.Offset(0, rDoc.Height + 12)
        rDoc.Width = rc.Width
        rDoc.Height = 36
        rMask.Height = rDoc.Height

        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

        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class