[Guide] How to Sign a PDF Electronically or Remove Signatures in Python

·

6 min read

[Guide] How to Sign a PDF Electronically or Remove Signatures in Python

Adding a signature to a PDF is essential for document authentication and verification, especially in legal and business contexts. For example, if a company's branch offices are spread across different locations or if customers are located far from the company's main address, relying on paper documents for signatures becomes impractical. In such cases, electronic documents become essential for transmitting and sharing project documents or contracts efficiently.

This guide will show you how to sign a PDF electronically or remove signatures in Python, allowing you to manage signatures programmingly without the need for Adobe Acrobat or any other Apps.


In today’s blog, to add a signature to a PDF, we’ll need Spire.PDF for Python. It is a powerful Python library that allows users to create, edit, and compress PDF documents, etc. And of course, that includes the feature, of inserting a signature into a PDF document, which will be illustrated later.

You can install Spire.PDF for Python from PyPI using the following pip command:

pip install Spire.Pdf

If you already have Spire.PDF for Python installed and would like to upgrade to the latest version, use the following pip command:

pip install --upgrade Spire.Pdf

When it comes to how do I sign a PDF, people often encounter confusion about the different types of signatures. What exactly is an electronic signature? How does it differ from a digital signature or an image signature? Understanding these distinctions can help clarify the options available for signing documents.

Digital Signatures use cryptographic techniques to ensure security and authenticity. They use a private key for encryption and a public key for decryption, providing a high level of security and are legally binding in many jurisdictions.

Image Signatures insert an image of a handwritten signature into a PDF. While this method looks like a traditional signature signed with pens, it lacks security features and is typically used for less formal purposes.

Electronic Signatures is a broad term encompassing any form of digital signing, including both digital and image signatures, as well as other methods like typing names or using styluses.

In summary, a digital signature offers the highest security and legal validity, image signatures provide a visual representation without strong security, and electronic signatures cover a range of methods with varying levels of security and legality.

Now that you're familiar with the different types of signatures, let's dive into the practical steps for creating one. An image signature is widely used in both formal and informal settings. In this article, we'll focus on how to add an image signature to a PDF using Python first. This section provides detailed steps and code examples to help you understand how to use Spire.PDF to accomplish this task effectively.

Steps to sign a PDF electronically with an image signature:

  • Create an instance of the PdfDocument class and load a PDF document to be signed from the file path.

  • Create a signature maker.

  • Configure properties of the signature using properties of the PdfOrdinarySignatureMaker class, including name, telephone number, location, and the reason to sign.

  • Create a PdfSignatureAppearance() object and customize the appearance of the signature by setting labels. Meanwhile, specify the signature image’s file path with the PdfImage.FromFile() method. Set the display mode with the GraphicMode.SignImageAndSignDetail property and configure the layout of it by setting the SignImageLayout property to none.

  • Get a PDF page with the PdfDocument.Pages[] property.

  • Add a signature to a PDF at the specified position by calling the PdfOrdinarySignatureMaker.MakeSignature() method.

  • Save the document as a new file and release resources.

Below is the code example of signing a PDF electronically on the first page:

from spire.pdf.common import *
from spire.pdf import *


# Create a PdfDocument instance
doc = PdfDocument()
# Load a PDF document from file
doc.LoadFromFile("Sample.pdf")

# Create a signature maker
signatureMaker = PdfOrdinarySignatureMaker(doc, "gary.pfx", "e-iceblue")

# Configure the signature properties including the signer's name, contact information, location, and signature reason
signature = signatureMaker.Signature
signature.Name = "Gary"
signature.ContactInfo = "+86 12345678"
signature.Location = "China"
signature.Reason = "I am the author"

# Create a custom signature appearance
appearance = PdfSignatureAppearance(signature)
# Set label for the signer's name
appearance.NameLabel = "Signer: "
# Set a label for the contact information
appearance.ContactInfoLabel = "Phone: "
# Set a label for the location
appearance.LocationLabel = "Location: "
# Set label for the signature reason
appearance.ReasonLabel = "Reason: "
# Set signature image
appearance.SignatureImage = PdfImage.FromFile("SigImg.png")
# Set the graphic render/display mode for the signature
appearance.GraphicMode = GraphicMode.SignImageAndSignDetail
# Set the layout for the signature image
appearance.SignImageLayout = SignImageLayout.none

# Get the first page
page = doc.Pages[0]

# Add the signature to a specified location on the page
signatureMaker.MakeSignature("Signature by Gary", page, 90.0, 600.0, 260.0, 100.0, appearance)

# Save the signed document
doc.SaveToFile("Signed.pdf")

# Release resources
doc.Close()

Showcase of Signing a PDF Electonically with Image Signature

Digital signatures are commonly used in formal contexts, such as legal documents or professional contracts, due to their high level of security and authenticity. Despite their authoritative nature, inserting a digital signature in Python is much simpler than adding an image signature to a PDF. In this part, we will explore, with Spire.PDF for Python, how to add a digital signature to a PDF.

Steps to sign a PDF electronically with digital signatures :

  • Import essential modules.

  • Instantiate an object of the PdfDocument class and read the PDF file to sign with the PdfDocument.LoadFromFile() method.

  • Create a PdfOrdinarySignatureMaker object.

  • Add a digital signature to a PDF with the PdfOrdinarySignatureMaker.MakeSignature() method.

  • Save the modified document with PdfDocument.LoadFromFile() method, and release the resource.

Here is the code example of inserting a digital signature into a PDF:

from spire.pdf.common import *
from spire.pdf import *


# Create a PdfDocument instance
doc = PdfDocument()
# Load a PDF document
doc.LoadFromFile("test2.pdf")

# Create a signature maker
signatureMaker = PdfOrdinarySignatureMaker(doc, "gary.pfx", "e-iceblue")

# Add an invisible digital signature to the document
signatureMaker.MakeSignature("Signature by Gary")

# Save the signed document
doc.SaveToFile("InvisibleSignature.pdf")

# Release resources
doc.Close()

Result of Adding a Digital Signature to PDF

When you need to make revisions to a PDF document or if an electronic signature was mistakenly added, removing the signature becomes crucial. In this section, we will guide you through how to remove a signature from a PDF with Spire.PDF, enabling you to access and clear signatures without hassle.

Steps to remove signatures from PDF documents:

  • Create an instance of the PdfDocument class and load the document from the disk.

  • Get the form field collection with the PdfDocument.Form property.

  • Loop through all form fields in reverse order.

  • Check if the field is a PdfSignatureFieldWidget class.

  • If it is, remove signatures using the PdfFormFieldWidgetCollection.FieldsWidget.RemoveAt() method.

  • Store the resulting document and release the resource.

Here’s an example of removing signatures from a PDF document:

from spire.pdf.common import *
from spire.pdf import *


# Create an instance of the PdfDocument class
doc = PdfDocument()

# Open a PDF document from file
doc.LoadFromFile("Signed.pdf")

# Get form field collection from the document
pdfForm = doc.Form
formWidget = PdfFormWidget(pdfForm)

# Check if there are any form fields in the collection
if formWidget.FieldsWidget.Count > 0:
    # Loop through all form fields from the last to the first
    for i in range(formWidget.FieldsWidget.Count - 1, -1, -1):
        field = formWidget.FieldsWidget[i]  
        # Check if the field is a PdfSignatureFieldWidget
        if isinstance(field, PdfSignatureFieldWidget):
            # Remove the field
            formWidget.FieldsWidget.RemoveAt(i)

# Save the document
doc.SaveToFile("RemoveSignature.pdf")

# Release the resources
doc.Close()

This page mainly talks about how to sign a PDF electronically in Python, including image signatures and digital signatures. To make a better understanding, the guide clarifies the differences and similarities among image signatures, digital signatures, and electronic signatures. Besides, the article provides a simple way to remove signatures when revisions are needed. We hope you find it useful!

Subscribe to our newsletter

Read articles from Casie's blog directly inside your inbox. Subscribe to the newsletter, and don't miss out.