Generate signature preview

Overview

GroupDocs.Signature PreviewSignatureOptions class to specify different options to manage signatures preview generation process.

Here are the steps how to generate signature preview with GroupDocs.Signature:

  • Instantiate the required SignOptions object with the settings you want to generate the preview
  • Instantiate the PreviewSignatureOptions object with:
  • delegate for signature stream creation (see event handler CreateSignatureStream);
  • image preview format - PNG / JPG / BMP,
  • unique signature identifier.
Note
Stream that were created by CreateSignatureStream delegate will be disposed automatically once after generation of preview image. If you need to implement custom image preview stream disposing you have to pass additional argument ReleaseSignatureStream to clean up resources.

Generate signature preview

This example demonstrates how to generate a preview image for a QR Code signature using GroupDocs.Signature with default sizes.

public static void GetPreview()
{
    // Create QR code sign options
    QrCodeSignOptions signOptions = new QrCodeSignOptions
    {
        EncodeType = QrCodeTypes.QR,
        Data = new GroupDocs.Signature.Domain.Extensions.Address
        {
            Street = "221B Baker Street",
            City = "London",
            State = "NW",
            ZIP = "NW16XE",
            Country = "England"
        },
        HorizontalAlignment = HorizontalAlignment.Left,
        VerticalAlignment = VerticalAlignment.Center,
        Margin = new Padding(10)
    };

    // Create signature preview options
    PreviewSignatureOptions previewOption = new PreviewSignatureOptions(
        signOptions, 
        CreateSignatureStream, 
        ReleaseSignatureStream
    )
    {
        SignatureId = Guid.NewGuid().ToString(),
        PreviewFormat = PreviewSignatureOptions.PreviewFormats.JPEG
    };

    // Generate signature preview
    Signature.GenerateSignaturePreview(previewOption);
}

private static Stream CreateSignatureStream(PreviewSignatureOptions previewOptions)
{
    string imageFilePath = 
        $"signature-{previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name}.jpg";

    return new FileStream(imageFilePath, FileMode.Create);
}

private static void ReleaseSignatureStream(PreviewSignatureOptions previewOptions, 
    Stream signatureStream)
{
    signatureStream.Dispose();
    Console.WriteLine(
        $"Signature {previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name} is ready for preview"
    );
}

Adaptive rendering for QR Code signature previews

Note
If the specified width and height are not appropriate for generating a readable QR Code preview, the system will automatically use default size values. These defaults are chosen as the nearest optimal dimensions to ensure the QR Code remains clear and scannable.

This example demonstrates how to generate a preview image for a QR Code signature using GroupDocs.Signature. You’ll see how to configure the QR Code’s appearance, including size, color, and text alignment, and how to generate and save the preview image stream.

public static void GetPreview()
{
    // Initialize QR Code signing options
    QrCodeSignOptions qrSignOptions = new QrCodeSignOptions("GROUP DOCS", QrCodeTypes.QR)
    {
        Width = 250,                          // Width of the QR code
        Height = 270,                         // Height of the QR code
        ForeColor = Color.Red,                // Color of the QR code
        CodeTextAlignment = CodeTextAlignment.Below, // Text alignment relative to the QR code
        Text = "GROUP DOCS"                   // Text displayed below the QR code
    };

    // Configure preview generation options for the QR code signature
    PreviewSignatureOptions previewOptions = new PreviewSignatureOptions(
        qrSignOptions,
        CreateSignatureStream,   // Delegate to create the stream
        ReleaseSignatureStream   // Delegate to release the stream
    )
    {
        SignatureId = Guid.NewGuid().ToString(),  // Unique identifier for the preview
        PreviewFormat = PreviewSignatureOptions.PreviewFormats.PNG  // Output format of preview
    };

    // Generate the signature preview image
    Signature.GenerateSignaturePreview(previewOptions);
}

private static Stream CreateSignatureStream(PreviewSignatureOptions previewOptions)
{
    string imageFilePath = 
        $"signature-{previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name}.jpg";

    return new FileStream(imageFilePath, FileMode.Create);
}

private static void ReleaseSignatureStream(PreviewSignatureOptions previewOptions, 
    Stream signatureStream)
{
    signatureStream.Dispose();
    Console.WriteLine(
        $"Signature {previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name} is ready for preview"
    );
}

Adaptive rendering for Barcode signature previews

Note
When the width and height provided for the Barcode preview are not suitable, the preview generation will fall back to default size values. These defaults are selected to produce a barcode image that is readable and properly rendered.

This section shows how to create a preview for a Barcode signature. The example covers setting barcode properties such as type, size, and color, as well as generating the preview image with a custom stream handler.

public static void GetPreview()
{
    // Initialize QR Code signing options
    BarcodeSignOptions signOptions = new BarcodeSignOptions("GROUP DOCS", BarcodeTypes.MaxiCode)
    {
        Width = 400,                          // Width of the Barcode code
        Height = 400,                         // Height of the Barcode code
        ForeColor = Color.Red,                // Color of the Barcode code
        CodeTextAlignment = CodeTextAlignment.Below, // Text alignment relative to the Barcode code
        Text = "GROUP DOCS"                   // Text displayed below the Barcode code
    };

    // Configure preview generation options for the QR code signature
    PreviewSignatureOptions previewOptions = new PreviewSignatureOptions(
        qrSignOptions,
        CreateSignatureStream,   // Delegate to create the stream
        ReleaseSignatureStream   // Delegate to release the stream
    )
    {
        SignatureId = Guid.NewGuid().ToString(),  // Unique identifier for the preview
        PreviewFormat = PreviewSignatureOptions.PreviewFormats.PNG  // Output format of preview
    };

    // Generate the signature preview image
    Signature.GenerateSignaturePreview(previewOptions);
}

private static Stream CreateSignatureStream(PreviewSignatureOptions previewOptions)
{
    string imageFilePath = 
        $"signature-{previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name}.jpg";

    return new FileStream(imageFilePath, FileMode.Create);
}

private static void ReleaseSignatureStream(PreviewSignatureOptions previewOptions, 
    Stream signatureStream)
{
    signatureStream.Dispose();
    Console.WriteLine(
        $"Signature {previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name} is ready for preview"
    );
}

Rotate Barcode and QR Code previews

This example demonstrates how to apply rotation to Barcode and QR Code signature previews. Rotation can help fit signatures into specific layout requirements or create visual effects.

public static void GetPreview()
{
    // Initialize Barcode signing options
    BarcodeSignOptions barcodeSignOptions = new BarcodeSignOptions("GROUP DOCS", BarcodeTypes.MaxiCode)
    {
        Width = 400,                               // Width of the barcode
        Height = 400,                              // Height of the barcode
        ForeColor = Color.Red,                     // Foreground color of the barcode
        CodeTextAlignment = CodeTextAlignment.Below,  // Position of the text relative to the barcode
        Text = "GROUP DOCS",                       // Text displayed with the barcode
        RotationAngle = 45                         // Rotate the barcode by 45 degrees
    };

    // Configure preview generation options for the QR code signature
    PreviewSignatureOptions previewOptions = new PreviewSignatureOptions(
        qrSignOptions,
        CreateSignatureStream,   // Delegate to create the stream
        ReleaseSignatureStream   // Delegate to release the stream
    )
    {
        SignatureId = Guid.NewGuid().ToString(),  // Unique identifier for the preview
        PreviewFormat = PreviewSignatureOptions.PreviewFormats.PNG  // Output format of preview
    };

    // Generate the signature preview image
    Signature.GenerateSignaturePreview(previewOptions);
}

private static Stream CreateSignatureStream(PreviewSignatureOptions previewOptions)
{
    string imageFilePath = 
        $"signature-{previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name}.jpg";

    return new FileStream(imageFilePath, FileMode.Create);
}

private static void ReleaseSignatureStream(PreviewSignatureOptions previewOptions, 
    Stream signatureStream)
{
    signatureStream.Dispose();
    Console.WriteLine(
        $"Signature {previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name} is ready for preview"
    );
}

Transparency for Barcode/QR previews

This snippet shows how to set the transparency level of Barcode or QR Code previews, allowing you to customize the signature’s visual blending with the underlying document.

public static void GetPreview()
{
    // Initialize barcode signing options with Codabar type
    BarcodeSignOptions barcodeSignOptions = new BarcodeSignOptions("GROUP DOCS", BarcodeTypes.Codabar)
    {
        Width = 400,                               // Width of the barcode preview
        Height = 400,                              // Height of the barcode preview
        ForeColor = Color.Red,                     // Foreground color of the barcode bars
        CodeTextAlignment = CodeTextAlignment.Below,  // Position text below the barcode
        Text = "GROUP DOCS",                       // Text displayed with the barcode
        Transparency = 0.5                         // Set barcode background transparency (50%)
    };

    // Configure preview generation options for the QR code signature
    PreviewSignatureOptions previewOptions = new PreviewSignatureOptions(
        qrSignOptions,
        CreateSignatureStream,   // Delegate to create the stream
        ReleaseSignatureStream   // Delegate to release the stream
    )
    {
        SignatureId = Guid.NewGuid().ToString(),  // Unique identifier for the preview
        PreviewFormat = PreviewSignatureOptions.PreviewFormats.PNG  // Output format of preview
    };

    // Generate the signature preview image
    Signature.GenerateSignaturePreview(previewOptions);
}

private static Stream CreateSignatureStream(PreviewSignatureOptions previewOptions)
{
    string imageFilePath = 
        $"signature-{previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name}.jpg";

    return new FileStream(imageFilePath, FileMode.Create);
}

private static void ReleaseSignatureStream(PreviewSignatureOptions previewOptions, 
    Stream signatureStream)
{
    signatureStream.Dispose();
    Console.WriteLine(
        $"Signature {previewOptions.SignatureId}-" +
        $"{previewOptions.SignOptions.GetType().Name} is ready for preview"
    );
}

Advanced Usage Topics

To learn more about document eSign features, please refer to the advanced usage section.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.