Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 24, 2025 18:57
Show Gist options
  • Save aspose-com-gists/589736dd9965071e3bac8c4fcbb88608 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/589736dd9965071e3bac8c4fcbb88608 to your computer and use it in GitHub Desktop.

Revisions

  1. aspose-com-gists revised this gist Oct 23, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,3 @@
    Add digital signature to Word, PDF, PowerPoint Files and Images via Python
    Add digital signature to Word, PDF, PowerPoint Files and Images via Python

    https://products.aspose.com/total/python-net/signature/
  2. aspose-com-gists created this gist Sep 17, 2023.
    18 changes: 18 additions & 0 deletions add-digital-signature-to-word-documents.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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)
    33 changes: 33 additions & 0 deletions add-electronic-signature-to-pdf.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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")
    24 changes: 24 additions & 0 deletions add-esignature-to-image.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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"))
    15 changes: 15 additions & 0 deletions add-esignature-to-powerpoint-files.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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)
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Add digital signature to Word, PDF, PowerPoint Files and Images via Python