GroupDocs.Signature provides PreviewSignatureOptions class to generate an image of a signature before it is placed on a document - for example, to show a user what a text, barcode or QR code signature will look like.
Here are the steps to generate signature preview with GroupDocs.Signature:
a function that creates the stream for the signature image (see CreateSignatureStream);
image preview format - PNG / JPEG / BMP / GIF / SVG (PNG by default),
an optional signature_id that tells the functions which signature they are handling.
The stream returned by the create function is closed automatically as soon as the signature image is written: a file is closed, and an io.BytesIO already holds the complete image. If you need to process the finished image or clean up resources yourself, pass a release function as an additional argument (see ReleaseSignatureStream).
GroupDocs.Signature calls the create function with the PreviewSignatureOptions object and writes the signature image into the stream it returns: a writable file object, an io.BytesIO, or a .NET stream.
defcreate_signature_stream(preview_options):# Name the image after the signature_id set on the preview optionsreturnopen(f"{preview_options.signature_id}.png","wb")
ReleaseSignatureStream delegate implementation
The release function receives the same preview options and the same object the create function returned. By the time it is called, the image is complete.
defrelease_signature_stream(preview_options,signature_stream):# signature_stream is the object create_signature_stream returnedprint(f"Image file {signature_stream.name} is ready for preview")
Generate signature preview to an image file
fromgroupdocs.signatureimportSignaturefromgroupdocs.pydrawingimportColorfromgroupdocs.signature.domainimportSignatureFontfromgroupdocs.signature.optionsimportPreviewSignatureOptions,TextSignOptionsdefcreate_signature_stream(preview_options):# Name the image after the signature_id set belowreturnopen(f"{preview_options.signature_id}.png","wb")defrelease_signature_stream(preview_options,signature_stream):print(f"Image file {signature_stream.name} is ready for preview")defgenerate_signature_preview_to_file():# Describe the signature exactly as you would for signingsign_options=TextSignOptions("John Smith")sign_options.width=200sign_options.height=50sign_options.fore_color=Color.dark_bluefont=SignatureFont()font.family_name="Arial"font.size=24sign_options.font=font# Create preview options objectpreview_options=PreviewSignatureOptions(sign_options,create_signature_stream,release_signature_stream)preview_options.signature_id="text_signature"preview_options.preview_format=PreviewSignatureOptions.PreviewFormats.PNG# Generate preview; no document is involvedSignature.generate_signature_preview(preview_options)if__name__=="__main__":generate_signature_preview_to_file()
The create function can return an io.BytesIO. When generate_signature_preview returns, the buffer holds the whole image, ready to be stored or sent elsewhere.
importiofromgroupdocs.signatureimportSignaturefromgroupdocs.signature.domainimportQrCodeTypesfromgroupdocs.signature.optionsimportPreviewSignatureOptions,QrCodeSignOptionsdefgenerate_signature_preview_to_memory():image=io.BytesIO()defcreate_signature_stream(preview_options):# Write the image into the buffer instead of a filereturnimagesign_options=QrCodeSignOptions("https://www.groupdocs.com/",QrCodeTypes.QR)sign_options.width=120sign_options.height=120preview_options=PreviewSignatureOptions(sign_options,create_signature_stream)Signature.generate_signature_preview(preview_options)data=image.getvalue()is_png=data.startswith(b"\x89PNG")print(f"QR code preview: {len(data)} bytes, PNG image: {is_png}")if__name__=="__main__":generate_signature_preview_to_memory()
Generate previews of several signatures in different formats
One pair of functions can serve any number of previews: signature_id tells them which signature they are handling, and preview_format chooses the image type. Without a license, the SVG format is available only for Code39 barcodes.