Add digital signature to Word, PDF, PowerPoint Files and Images via Python
Forked from aspose-com-gists/add-digital-signature-to-word-documents.py
Created
February 24, 2025 18:56
-
-
Save thinkst-cs/c58181f1655110a2726cec6212cbe09d to your computer and use it in GitHub Desktop.
Sign Documents via Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
signatureLine = builder.insert_signature_line(aw.SignatureLineOptions()).signature_line | |
doc.save(docs_base.artifacts_dir + "SignDocuments.signature_line.docx") | |
signOptions = aw.digitalsignatures.SignOptions() | |
signOptions.signature_line_id = signatureLine.id | |
with open(docs_base.images_dir + "Enhanced Windows MetaFile.emf", "rb") as image_file: | |
signOptions.signature_line_image = image_file.read() | |
certHolder = aw.digitalsignatures.CertificateHolder.create(docs_base.my_dir + "morzal.pfx", "aw") | |
aw.digitalsignatures.DigitalSignatureUtil.sign(docs_base.artifacts_dir + "SignDocuments.signature_line.docx", | |
docs_base.artifacts_dir + "SignDocuments.new_signature_line.docx", certHolder, signOptions) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pdf as pdf | |
import aspose.pydrawing as drawing | |
# Set the source directory path | |
filePath = "C://Words//" | |
# Load the license in your application to crop the PDF | |
pdfCropLicense = pdf.License() | |
pdfCropLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic") | |
#Load the PDF file to crop | |
pdfDoc = pdf.Document(filePath + "GeneratedPdf.pdf") | |
#Instantiate the PdfFileSignature for the loaded PDF document | |
signature = pdf.facades.PdfFileSignature(pdfDoc) | |
#Load the certificate file along with the password | |
pkcs = pdf.forms.PKCS7(filePath + "sample.pfx", "123456789") | |
#Assign the access permissions | |
docMdpSignature = pdf.forms.DocMDPSignature(pkcs, pdf.forms.DocMDPAccessPermissions.FILLING_IN_FORMS) | |
#Set the rectangle for the signature placement | |
rect = drawing.Rectangle(150, 650, 450, 150) | |
#Set signature appearance | |
signature.signature_appearance = "sample.jpg" | |
#Sign the PDF file with the certify method | |
signature.certify(1, "Signature Insert Reason", "Contact", "Location", True, rect, docMdpSignature) | |
#Save digitally signed PDF file | |
signature.save("Digitally Signed PDF.pdf") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pycore as aspycore | |
from aspose.imaging import * | |
from aspose.imaging.brushes import * | |
from aspose.imaging.fileformats.jpeg import * | |
from aspose.imaging.imageoptions import * | |
import os | |
if 'TEMPLATE_DIR' in os.environ: | |
templates_folder = os.environ['TEMPLATE_DIR'] | |
else: | |
templates_folder = r"C:\Users\USER\Downloads\templates" | |
delete_output = 'SAVE_OUTPUT' not in os.environ | |
data_dir = templates_folder | |
with Image.load(os.path.join(data_dir, "template.tiff")) as canvas: | |
with Image.load(os.path.join(data_dir, "template.bmp")) as signature: | |
graphics = Graphics(canvas) | |
point = Point(canvas.width - signature.width, canvas.height - signature.height) | |
print(point) | |
graphics.draw_image(signature, point) | |
canvas.save(os.path.join(data_dir, "result.png"), PngOptions()) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "result.png")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
# Load presentation | |
with slides.Presentation("presentation.pptx") as pres: | |
# Create DigitalSignature object with PFX file and PFX password | |
signature = slides.DigitalSignature("certificate.pfx", "password") | |
# Comment new digital signature | |
signature.comments = "Signing with Aspose.Slides" | |
# Add digital signature to presentation | |
pres.digital_signatures.add(signature) | |
# Save presentation | |
pres.save("SignedPPT.pptx", slides.export.SaveFormat.PPTX) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment