VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
Vintasoft.Imaging.Pdf Namespace / PdfPageDigitalSignatureHelper Class / DrawSignatureAppearance(PdfGraphics,RectangleF) Method
Syntax Example Requirements SeeAlso
In This Topic
    DrawSignatureAppearance(PdfGraphics,RectangleF) Method (PdfPageDigitalSignatureHelper)
    In This Topic
    Draws the signature appearance.
    Syntax
    'Declaration
    
    Protected Overridable Sub DrawSignatureAppearance( _
    ByVal graphics
    The PDF graphics.
    As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics, _
    ByVal rect
    The signature rect on graphics.
    As System.Drawing.RectangleF _
    )

    Parameters

    graphics
    The PDF graphics.
    rect
    The signature rect on graphics.
    Example

    Here is an example that shows how to how to sign a PDF document using visible digital signature with custom appearance:

    
    Imports System.Drawing
    Imports System.Security.Cryptography.X509Certificates
    Imports Vintasoft.Imaging.Pdf
    Imports Vintasoft.Imaging.Pdf.Drawing
    Imports Vintasoft.Imaging.Pdf.Tree.Fonts
    
    ''' <summary>
    ''' Adds visible digital signature with custom appearance to a PDF page.
    ''' </summary>
    Public Class PdfPageDigitalSignatureHelper_CustomAppearance
        ''' <summary>
        ''' Signs a PDF document using specified PFX file.
        ''' </summary>
        ''' <param name="inputFilename">The name of input PDF file.</param>
        ''' <param name="outputFilename">The name of output PDF file.</param>
        ''' <param name="pfxFileName">The name of PFX file.</param>
        ''' <param name="pfxFilePassword">The password for PDF file.</param>
        Public Shared Sub SignPdfDocument(inputFilename As String, outputFilename As String, pfxFileName As String, pfxFilePassword As String)
            ' get the X509 certificate from .pfx-file
            Using certificate As New X509Certificate2(pfxFileName, pfxFileName)
                ' create a helper that alows to add digital signature to a PDF document
                Dim digitalSignatureHelper As New MyDigitalSignatureHelper(certificate, New RectangleF(20, 20, 250, 100))
    
                ' set the background color for signature
                digitalSignatureHelper.SignatureBackgoundColor = Color.Green
    
                ' set the reason for signing
                digitalSignatureHelper.SigningReason = "Approve document"
    
                ' set text for signature appearance
                digitalSignatureHelper.SignatureText = String.Format("Digitally signed by" & vbLf & "{0}", digitalSignatureHelper.SignerName)
    
                ' add digital signature to the first PDF page, sign PDF document and save PDF document to the output file
                digitalSignatureHelper.SignDocument(inputFilename, outputFilename, 0)
            End Using
        End Sub
    
        Public Class MyDigitalSignatureHelper
            Inherits PdfPageDigitalSignatureHelper
            Public Sub New(certificate As X509Certificate2, signatureRect As RectangleF)
                MyBase.New(certificate, signatureRect)
            End Sub
    
            ''' <summary>
            ''' Draws the text on signature appearance.
            ''' </summary>
            ''' <param name="graphics">The PDF graphics.</param>
            ''' <param name="rect">The rectangle.</param>
            ''' <param name="font">The text font.</param>
            ''' <param name="text">The text do draw.</param>
            Protected Overrides Sub DrawSignatureAppearanceText(graphics As PdfGraphics, rect As RectangleF, font As PdfFont, text As String)
                ' create border pen
                Dim borderPen As New PdfPen(Color.Orange, 10)
    
                ' draw border
                graphics.DrawRectangle(borderPen, rect)
    
                ' padding
                rect.Inflate(-borderPen.Width * 2, -borderPen.Width * 2)
    
                ' measure font size
                Dim fontSize As Single = graphics.MeasureFontSize(text, font, rect.Width, rect.Height)
    
                ' draw the signature text
                graphics.DrawString(text, font, fontSize, New PdfBrush(SignatureTextColor), rect, SignatureTextAlignment, _
                    False)
            End Sub
        End Class
    End Class
    
    
    
    using System.Drawing;
    using System.Security.Cryptography.X509Certificates;
    using Vintasoft.Imaging.Pdf;
    using Vintasoft.Imaging.Pdf.Drawing;
    using Vintasoft.Imaging.Pdf.Tree.Fonts;
    
    /// <summary>
    /// Adds visible digital signature with custom appearance to a PDF page.
    /// </summary>
    public class PdfPageDigitalSignatureHelper_CustomAppearance
    {
        /// <summary>
        /// Signs a PDF document using specified PFX file.
        /// </summary>
        /// <param name="inputFilename">The name of input PDF file.</param>
        /// <param name="outputFilename">The name of output PDF file.</param>
        /// <param name="pfxFileName">The name of PFX file.</param>
        /// <param name="pfxFilePassword">The password for PDF file.</param>
        public static void SignPdfDocument(
            string inputFilename,
            string outputFilename,
            string pfxFileName,
            string pfxFilePassword)
        {
            // get the X509 certificate from .pfx-file
            using (X509Certificate2 certificate = new X509Certificate2(pfxFileName, pfxFileName))
            {
                // create a helper that alows to add digital signature to a PDF document
                MyDigitalSignatureHelper digitalSignatureHelper = new MyDigitalSignatureHelper(certificate, new RectangleF(20, 20, 250, 100));
    
                // set the background color for signature
                digitalSignatureHelper.SignatureBackgoundColor = Color.Green;
    
                // set the reason for signing
                digitalSignatureHelper.SigningReason = "Approve document";
    
                // set text for signature appearance
                digitalSignatureHelper.SignatureText = string.Format("Digitally signed by\n{0}", digitalSignatureHelper.SignerName);
    
                // add digital signature to the first PDF page, sign PDF document and save PDF document to the output file
                digitalSignatureHelper.SignDocument(inputFilename, outputFilename, 0);
            }
        }
    
        public class MyDigitalSignatureHelper : PdfPageDigitalSignatureHelper
        {
            public MyDigitalSignatureHelper(X509Certificate2 certificate, RectangleF signatureRect)
                : base(certificate, signatureRect)
            {
            }
    
            /// <summary>
            /// Draws the text on signature appearance.
            /// </summary>
            /// <param name="graphics">The PDF graphics.</param>
            /// <param name="rect">The rectangle.</param>
            /// <param name="font">The text font.</param>
            /// <param name="text">The text do draw.</param>
            protected override void DrawSignatureAppearanceText(PdfGraphics graphics, RectangleF rect, PdfFont font, string text)
            {
                // create border pen
                PdfPen borderPen = new PdfPen(Color.Orange, 10);
    
                // draw border
                graphics.DrawRectangle(borderPen, rect);
    
                // padding
                rect.Inflate(-borderPen.Width * 2, -borderPen.Width * 2);
    
                // measure font size
                float fontSize = graphics.MeasureFontSize(text, font, rect.Width, rect.Height);
    
                // draw the signature text
                graphics.DrawString(text, font, fontSize, new PdfBrush(SignatureTextColor), rect, SignatureTextAlignment, false);
            }
        }
    }
    
    

    Requirements

    Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    See Also