Sign PDF Documents
Aspose.PDF for Python via .NET provides the PdfFileSignature facade for applying digital signatures to existing PDF documents. Using a certificate file, you can sign a document programmatically, place the signature on a page, assign signature metadata, and customize how the signature is displayed.
This article demonstrates several common signing workflows:
- Create and bind a
PdfFileSignatureobject to the input PDF. - Configure the signing certificate.
- Apply a digital signature to the target page.
- Optionally assign a signature name and visible appearance.
- Save the signed PDF.
Prepare reusable signing helpers
Before applying a digital signature to a PDF, it is useful to set up a small group of reusable helper functions. These helpers initialize the signature handler, define the visible signature area, configure the certificate, and build reusable PKCS#7 signature objects so the signing examples below remain self-contained and easier to follow.
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
import aspose.pydrawing as apd
DEFAULT_CERTIFICATE_PASSWORD = "Aspose2021"
DEFAULT_SIGNATURE_NAME = "Signature1"
def create_pdf_file_signature(infile):
pdf_signature = pdf_facades.PdfFileSignature()
pdf_signature.bind_pdf(infile)
return pdf_signature
def create_signature_rectangle():
return apd.Rectangle(10, 10, 200, 60)
def configure_signature_certificate(
pdf_signature, certificate_path, certificate_password=DEFAULT_CERTIFICATE_PASSWORD
):
pdf_signature.set_certificate(certificate_path, certificate_password)
def create_pkcs7_signature(
certificate_path, certificate_password=DEFAULT_CERTIFICATE_PASSWORD
):
signature = ap.forms.PKCS7(certificate_path, certificate_password)
signature.reason = "Document approval"
signature.contact_info = "qa@example.com"
signature.location = "New York, USA"
signature.authority = "Aspose.PDF Example"
return signature
def create_custom_signature_appearance():
appearance = ap.forms.SignatureCustomAppearance()
appearance.font_family_name = "Arial"
appearance.font_size = 10
appearance.show_contact_info = True
appearance.show_location = True
appearance.show_reason = True
return appearance
Sign a PDF with basic certificate parameters
This approach configures the certificate directly on the PdfFileSignature object. It is useful when you want a straightforward signing flow with standard signature metadata and no separate signature object management.
import aspose.pdf.facades as pdf_facades
import sys
from os import path
def sign_pdf_with_basic_parameters(infile, outfile, certificate_path):
pdf_signature = create_pdf_file_signature(infile)
try:
configure_signature_certificate(pdf_signature, certificate_path)
pdf_signature.sign(
1,
"Document approval",
"qa@example.com",
"New York, USA",
False,
create_signature_rectangle(),
)
pdf_signature.save(outfile)
finally:
pdf_signature.close()
Sign a PDF with a PKCS7 object
Use a PKCS7 object when you need to prepare the signature first and then pass it into the signing call. This pattern gives you more control over the signature settings and is a good basis for more advanced workflows.
import aspose.pdf.facades as pdf_facades
import sys
from os import path
def sign_pdf_with_certificate_object(infile, outfile, certificate_path):
pdf_signature = create_pdf_file_signature(infile)
try:
signature = create_pkcs7_signature(certificate_path)
pdf_signature.sign(1, False, create_signature_rectangle(), signature)
pdf_signature.save(outfile)
finally:
pdf_signature.close()
Sign a PDF with a named signature
If your document workflow relies on a predefined signature field name, pass that name to sign(). This makes it easier to reference the signature later for validation, processing, or integration with an approval workflow.
import aspose.pdf.facades as pdf_facades
import sys
from os import path
def sign_pdf_with_named_signature(infile, outfile, certificate_path):
pdf_signature = create_pdf_file_signature(infile)
try:
signature = create_pkcs7_signature(certificate_path)
signature.reason = "Approved by signing workflow"
pdf_signature.sign(
1,
DEFAULT_SIGNATURE_NAME,
"Approved by signing workflow",
"qa@example.com",
"New York, USA",
True,
create_signature_rectangle(),
signature,
)
pdf_signature.save(outfile)
finally:
pdf_signature.close()
Apply a visible signature
You can make the signature visible on the PDF page by assigning a custom appearance to the PKCS7 object. This is useful when the output document should show approval details such as the reason, location, and contact information to end users.
import aspose.pdf.facades as pdf_facades
import sys
from os import path
def apply_visible_signature(infile, outfile, certificate_path):
pdf_signature = create_pdf_file_signature(infile)
try:
signature = create_pkcs7_signature(certificate_path)
signature.reason = "Visible approval signature"
signature.custom_appearance = create_custom_signature_appearance()
pdf_signature.sign(
1,
"Visible approval signature",
"qa@example.com",
"New York, USA",
True,
create_signature_rectangle(),
signature,
)
pdf_signature.save(outfile)
finally:
pdf_signature.close()