SignatureAppearance.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.AcroForms
Imports GrapeCity.Documents.Pdf.Graphics
Imports GrapeCity.Documents.Text
Imports System.Security.Cryptography.X509Certificates
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 此示例演示了如何使用 .pfx 文件创建 PDF 并对其进行签名,
'' 指定自定义 AppearanceStream 来表示签名。
'' 这个示例与SignDoc类似,只不过它添加了AppearanceStream。
Public Class SignatureAppearance
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
        page.Graphics.DrawString(
            "你好世界!" +
            "",
            tf, New PointF(72, 72))

        '' 初始化测试证书:
        Dim pfxPath = Path.Combine("Resources", "Misc", "DsPdfTest.pfx")
        Dim cert = New X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
            X509KeyStorageFlags.MachineKeySet Or X509KeyStorageFlags.PersistKeySet Or X509KeyStorageFlags.Exportable)
        Dim sp = New SignatureProperties() With {
            .SignatureBuilder = New Pkcs7SignatureBuilder() With {
                .CertificateChain = New X509Certificate2() {cert}
            },
            .Location = "DsPdfWeb Demo Browser",
            .SignerName = "DsPdfWeb",
            .SigningDateTime = Util.TimeNow()
        }

        '' 创建一个签名字段来保存签名:
        Dim sf = New SignatureField()
        '' 将签名字段添加到文档中:
        doc.AcroForm.Fields.Add(sf)
        '' 连接签名字段和签名属性:
        sp.SignatureField = sf

        '' 设置签名字段:
        sf.Widget.Rect = New RectangleF(page.Size.Width - 72 * 4, 72 * 2, 72 * 3, 72)
        sf.Widget.Page = page
        '' 小部件视觉道具将被下面设置的 sf.Widget.AppearanceStreams.Normal.Default 覆盖:
        sf.Widget.BackColor = Color.PeachPuff
        sf.Widget.Border = New GrapeCity.Documents.Pdf.Annotations.Border() With
            {
                .Color = Color.SaddleBrown,
                .Width = 3
            }
        sf.Widget.ButtonAppearance.Caption = $"Signer: {sp.SignerName}{vbLf}Location: {sp.Location}"
        '' 创建自定义签名外观流:
        Dim rc = New RectangleF(PointF.Empty, sf.Widget.Rect.Size)
        Dim fxo = New FormXObject(doc, rc)
        rc.Inflate(-4, -4)
        fxo.Graphics.FillEllipse(rc, Color.CornflowerBlue)
        fxo.Graphics.DrawEllipse(rc, New GCDRAW.Pen(Color.RoyalBlue, 3))
        rc.Inflate(-5, -5)
        fxo.Graphics.DrawEllipse(rc, New GCDRAW.Pen(Color.LightSteelBlue, 1))
        fxo.Graphics.DrawString($"Signed by {sp.SignerName}{vbLf}on {Util.TimeNow():yyyy-MM-dd}.",
            New TextFormat() With
            {
                .FontName = "Times New Roman",
                .FontSize = 14,
                .FontItalic = True,
                .ForeColor = Color.Navy
            },
            fxo.Bounds,
            TextAlignment.Center, ParagraphAlignment.Center, False)
        sf.Widget.AppearanceStreams.Normal.Default = fxo

        '' 重置签名外观以便使用小部件外观流:
        sp.SignatureAppearance = Nothing

        '' 签署并保存文档:
        '' 笔记:
        '' - 签名和保存是一个原子操作,两者不能分开。
        '' - 传递给 Sign() 方法的流必须可读。
        doc.Sign(sp, stream)

        '' 完成(生成并签名的文档已保存到“流”)。
        Return doc.Pages.Count
    End Function
End Class