Lisa.Li wrote:Hello Aniketc068,
Thank you for your inquiry. Please confirm if the attachment is the final effect you want?
Sincerely,
Lisa
E-iceblue support team
no i modify something in the code the only feature to add in the same code is LTV enable option like True and False and same for Time stamp
give me complete code according to my code
and 1 more thing when i create the small box so why the SignDetail is not auto adjust according to box size
from cryptography.hazmat.primitives.serialization import pkcs12
from cryptography.hazmat.backends import default_backend
from cryptography.x509 import NameOID, ExtensionOID
from cryptography.x509 import ocsp, AuthorityInformationAccessOID
from cryptography.hazmat import backends
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.hashes import SHA256
from cryptography.x509 import load_der_x509_crl
from spire.pdf import PdfFont, PdfFontFamily, PdfStringFormat
from cryptography import x509
from spire.pdf.common import *
from spire.pdf import *
import datetime
import pytz
# Create a PdfDocument object
doc = PdfDocument()
# Load a PDF file
doc.LoadFromFile("b.pdf")
# Specify the path of the pfx certificate
pfxCertificatePath = "DS.pfx"
# Specify the password of the pdf certificate
pfxPassword = "asd"
# Load PFX file and extract CN (Common Name)
with open(pfxCertificatePath, 'rb') as pfx_file:
pfx_data = pfx_file.read()
# Convert the password to bytes
pfx_password_bytes = pfxPassword.encode('utf-8')
# Load the key and certificates
private_key, certificate, additional_certificates = pkcs12.load_key_and_certificates(
pfx_data, pfx_password_bytes
)
# Extract the CN (Common Name) from the certificate
cn = certificate.subject.get_attributes_for_oid(
NameOID.COMMON_NAME)[0].value
# Create a signature maker
signatureMaker = PdfOrdinarySignatureMaker(doc, pfxCertificatePath, pfxPassword)
# Show validity symbol in signature
signatureMaker.SetAcro6Layers(False)
# Extract the current date in the desired format (dd-MMM-yyyy)
current_date = datetime.datetime.now().strftime("%d-%b-%Y")
# Configure the signature properties like the signer's name, contact information, location, and sign reason
signature = signatureMaker.Signature
signature_text = f"Digitally signed by: {cn}\nDate: {current_date}\nLocation: Delhi\nReason: Sign\nCustom Text: for testing by aniket chaturvedi"
signature.Name = signature_text
# Create a custom signature appearance
appearance = PdfSignatureAppearance(signature)
# Set the graphic mode as SignImageAndSignDetail
appearance.GraphicMode = GraphicMode.SignImageAndSignDetail
# Define which pages to sign
# You can define the page numbers or ranges as follows:
#Example: "1,6,7", "1-3", "all", "first", "last"
txn_id = "xdgfvsfd654dfsdf"
sigpage = "all"
pages_to_sign = str(sigpage) # Define the pages here. Example: "1,6,7", "1-3", "All"
pages_to_sign = pages_to_sign.lower()
# Convert page ranges into a list of page numbers
page_numbers = []
if pages_to_sign == "all":
page_numbers = list(range(1, doc.Pages.Count + 1)) # Sign all pages
elif pages_to_sign == "first":
page_numbers = [1] # Sign the first page
elif pages_to_sign == "last":
page_numbers = [doc.Pages.Count] # Sign the last page
else:
# Handle page numbers and ranges
ranges = pages_to_sign.split(",") # Split by commas
for item in ranges:
if "-" in item: # Check if it's a range (e.g., "1-3")
start, end = item.split("-")
page_numbers.extend(range(int(start), int(end) + 1))
else:
try:
page_numbers.append(int(item)) # Single page number
except ValueError:
print(f"Warning: Invalid page number or range '{item}' skipped.")
# Add the signature to all specified pages (regardless of signature field existence)
for page_num in page_numbers:
if 1 <= page_num <= doc.Pages.Count: # Ensure page number is valid
page = doc.Pages[page_num - 1] # Adjust for zero-based index
# Create a unique signature field name for each page (this won't add a new field if Signature_Fields is blank)
signatureFieldName = f"MX Signer Server {txn_id}_Page{page_num}"
# Apply the signature to the page (without creating a new signature field)
signatureMaker.MakeSignature(signatureFieldName, page, 54.0, page.Size.Height - 100.0, 240.0, 80.0, appearance)
# Save the signed document with signature fields on the selected pages
doc.SaveToFile("aniket.pdf")
# Dispose resources
doc.Dispose()