Add digital signature or digitally sign PDF in Python
Sign PDF with digital signatures
import aspose.pdf as ap
import aspose.pydrawing as drawing
ppath_infile = self.data_dir + infile
path_outfile = self.data_dir + outfile
path_pfxfile = self.data_dir + pfxfile
# Open PDF document
with ap.Document(path_infile) as document:
# Instantiate PdfFileSignature object
with ap.facades.PdfFileSignature(document) as signature:
# Create PKCS#7 object for sign
pkcs = ap.forms.PKCS7(path_pfxfile, "12345")
# Sign PDF file
signature.sign(1, True, drawing.Rectangle(300, 100, 400, 200), pkcs)
# Save PDF document
signature.save(path_outfile)
A PKCS#7 detached signature adds a digital signature to a document without embedding the content into the signature block.
The next example signs a PDF document using a PKCS#7 detached digital signature, applying the signature to the first page in a specified rectangular area.
import aspose.pdf as ap
import aspose.pydrawing as drawing
path_infile = self.data_dir + infile
path_outfile = self.data_dir + outfile
path_pfxfile = self.data_dir + pfxfile
# Open PDF document
with ap.Document(path_infile) as document:
# Instantiate PdfFileSignature object using the opened document
with ap.facades.PdfFileSignature(document) as signature:
# Create PKCS#7 detached object for sign
pkcs = ap.forms.PKCS7Detached(path_pfxfile, password, ap.DigestHashAlgorithm.SHA256)
# Sign PDF file
signature.sign(1, True, drawing.Rectangle(300, 100, 400, 200), pkcs)
# Save PDF document
signature.save(path_outfile)
This Python code snippet verifies a digital signature in a PDF file using ‘file_sign.verify_signature()’ method.
- Creates an instance of PdfFileSignature that allows you to interact with signatures in PDF.
- Get a list of signature names
get_signature_names(True)
. - Checks the first signature in the list
verify_signature
for compliance with the specified certificate.
import aspose.pdf as ap
path_infile = self.data_dir + infile
# Create an instance of PdfFileSignature for working with signatures in the document
with ap.facades.PdfFileSignature(path_infile) as file_sign:
# Get a list of signatures
signature_names = file_sign.get_signature_names(True)
# Verify the signature with the given name.
return file_sign.verify_signature(signature_names[0], certificate)
Add timestamp to digital signature
How to digitally sign a PDF with timestamp
Aspose.PDF for Python supports to digitally sign the PDF with a timestamp server or Web service.
In order to accomplish this requirement, the TimestampSettings class has been added to the Aspose.PDF namespace. Please take a look at the following code snippet which obtains timestamp and adds it to PDF document:
import aspose.pdf as ap
import aspose.pydrawing as drawing
path_infile = self.data_dir + infile
path_outfile = self.data_dir + outfile
path_pfxfile = self.data_dir + pfxfile
# Open PDF document
with ap.Document(path_infile) as document:
# Create an instance of PdfFileSignature for working with signatures in the document
with ap.facades.PdfFileSignature(document) as signature:
pkcs = ap.forms.PKCS7(path_pfxfile, password)
# Create TimestampSettings settings
timestamp_settings = ap.TimestampSettings("https://freetsa.org/tsr",
"", ap.DigestHashAlgorithm.SHA256) # User/Password can be omitted
pkcs.timestamp_settings = timestamp_settings
rect = drawing.Rectangle(100, 100, 200, 100) # Creating a rectangle for the signature
# Create any of the three signature types
signature.sign(1, "Signature Reason", "Contact", "Location", True, rect, pkcs)
# Save PDF document
signature.save(path_outfile)
Signing PDF documents using ECDSA
Signing PDF documents using ECDSA (Elliptic Curve Digital Signature Algorithm) involves utilizing elliptic curve cryptography to generate digital signatures.
The code snippet above illustrates how to apply a PKCS#7 detached digital signature to a PDF document using Aspose.PDF for Python. By loading the document, configuring the signature appearance and security settings, and saving the result, this example demonstrates a complete, reliable workflow for digitally signing PDF files.
This method ensures the document’s authenticity and integrity by embedding a secure, verifiable signature on the first page. The use of SHA-256 as the digest algorithm meets modern cryptographic standards, while the ability to control signature placement offers flexibility for visible approval marks.
import aspose.pdf as ap
import aspose.pydrawing as drawing
path_infile = self.data_dir + infile
path_outfile = self.data_dir + outfile
path_pfxfile = self.data_dir + pfxfile
# Open PDF document
with ap.Document(path_infile) as document:
# Create an instance of PdfFileSignature to sign the document
with ap.facades.PdfFileSignature(document) as signature:
# Create a PKCS7Detached object using the provided certificate and password
pkcs = ap.forms.PKCS7Detached(path_pfxfile, password, ap.DigestHashAlgorithm.SHA256)
# Sign the first page of the document, setting the signature's appearance at the specified location
signature.sign(1, True, drawing.Rectangle(300, 100, 400, 200), pkcs)
# Save PDF document
signature.save(path_outfile)