Define the paths for the input PDF file, the digital certificates, and specify the output path where the signed documents will be saved.
Create a new instance of the Signature class and pass the source document path as a constructor parameter.
Instantiate the DigitalSignOptions object with the required certificate and its password, and configure additional properties such as reason, contact, location, and position.
After signing, update the document path for the next iteration.
Analyze the SignResult to check the newly created signatures if needed.
Iterate through each certificate, repeating the above steps for each one.
This example demonstrates how to iteratively sign a PDF document with multiple digital certificates. See SignResult
stringfilePath="sample.pdf";stringfileName=Path.GetFileName(filePath);string[]certificatePaths=newstring[]{"certificate1.pfx","certificate2.pfx"};stringoutputFilePath=Path.Combine("output folder path","SignWithDigitalIterative",fileName);intiteration=1;stringdocumentFile=filePath;foreach(varcertificatePathincertificatePaths){using(Signaturesignature=newSignature(documentFile)){DigitalSignOptionsoptions=newDigitalSignOptions(certificatePath){Password="1234567890",// Certificate passwordReason=$"Approved-{iteration}",// Digital certificate detailsContact=$"John{iteration} Smith{iteration}",Location=$"Location-{iteration}",AllPages=true,// Sign all pagesLeft=10+100*(iteration-1),Top=10+100*(iteration-1),Width=160,Height=80,Margin=newPadding(){Bottom=10,Right=10}};SignResultsignResult=signature.Sign(outputFilePath,options);documentFile=outputFilePath;Console.WriteLine($"\nSource document signed successfully {iteration++}-"+$"time with {signResult.Succeeded.Count} signature(s).\n"+$"File saved at {outputFilePath}.");}}
Iteratively sign with custom appearance and image
This example demonstrates how to iteratively sign a PDF document with multiple digital certificates, customizing the appearance and adding an image to each signature. See SignResult
Here are the steps:
Setting up variables: define the paths for the input PDF file, the digital certificates, and specify the output path where the signed documents will be saved.
// File paths for source document, image, and output filesstringsourceFile="sample.pdf";stringimageFilePath="image.jpg";stringsignedFile="signed.pdf";stringresultOutputFile="result.pdf";// Paths to digital certificates for signingstring[]certificatePaths=newstring[]{certificateFilePath1,certificateFilePath2};intiteration=0;
Helper method to generate the DigitalSignOptions object with the required certificate and its password, and configure additional properties such as reason, contact, location, and position. An image and custom appearance labels are also set.
privatestaticDigitalSignOptionsCreateDigitalSignOptions(stringcertificatePath,stringimageFilePath,intiteration){returnnewDigitalSignOptions(certificatePath){Password="1234567890",// Certificate passwordReason="Sign",// Signature reasonContact=$"JohnSmith{iteration}",// Contact detailsLocation=$"Office{iteration}",// Signature locationVisible=true,Left=80+iteration*5,// Positioning for visibilityTop=600+iteration*5,Height=50+iteration*5,Width=200+iteration*5,ImageFilePath=imageFilePath,// Image appearance for signatureAppearance=newPdfDigitalSignatureAppearance{ReasonLabel="Reason Label",DigitalSignedLabel="Digital Signed Label"}};}
Create a new instance of the Signature class and pass the source document path as a constructor parameter.
After signing, update the document path for the next iteration.
Analyze the SignResult to check the newly created signatures if needed.
Iterate through each certificate, repeating the above steps for each one.
foreach(varcertificatePathincertificatePaths){using(Signaturesignature=newSignature("sample.pdf")){// Generate signature options with helper methodDigitalSignOptionsoptions=CreateDigitalSignOptions(certificatePath,imageFilePath,iteration);// Update output file name on second iterationif(iteration==1)signedFile=resultOutputFile;// Sign document and display resultSignResultsignResult=signature.Sign(signedFile,options);Console.WriteLine($"\nDocument signed successfully {iteration + 1}-time with "+$"{signResult.Succeeded.Count} signature(s).\nFile saved at {signedFile}.");iteration++;}}
Customize digital appearance with color, image and font
This example demonstrates how to apply a digital signature with a customized appearance, including a foreground color, image, and specific font settings.
Here are the steps:
Setting up variables: define the paths for the input PDF file, the digital certificates, and specify the output path where the signed documents will be saved.
// File paths for source document, image, and output filesstringsourceFile="sample.pdf";stringimageFilePath="image.jpg";stringsignedFile="signed.pdf";stringresultOutputFile="result.pdf";// Paths to digital certificates for signingstring[]certificatePaths=newstring[]{certificateFilePath1,certificateFilePath2};intiteration=0;
Helper method to generate the DigitalSignOptions object with the required certificate and its password, and configure additional properties such as reason, contact, location, and position. An image and custom appearance labels are also set.
privatestaticDigitalSignOptionsCreateDigitalSignOptions(stringcertificatePath,stringimageFilePath,intiteration){returnnewDigitalSignOptions(certificatePath){// Specify the type of signatureSignatureType=SignatureType.Digital,// Provide the certificate passwordPassword="1234567890",// Digital certificate details for signature metadataReason="Sign",// Reason for the signatureContact="JohnSmith"+iteration,// Contact information of the signerLocation="Office1"+iteration,// Location where the document is signed// Enable visibility of the signature on the documentVisible=true,// Specify the position and size of the signature on the pageLeft=400,// Horizontal position (X-axis)Top=20+iteration*70,// Vertical position (Y-axis)Height=70,// Height of the signature areaWidth=200,// Width of the signature area// Add an image to represent the digital certificate's appearanceImageFilePath=imageFilePath,// Path to the image file// Customize the signature's appearanceAppearance=newPdfDigitalSignatureAppearance{// Set a custom font color for the signature textForeground=Color.FromArgb(50,Color.Gray),// Apply specific font settings for the signatureFontFamilyName="TimesNewRoman",// Font family nameFontSize=12// Font size}};}
Create a new instance of the Signature class and pass the source document path as a constructor parameter.
After signing, update the document path for the next iteration.
Analyze the SignResult to check the newly created signatures if needed.
Iterate through each certificate, repeating the above steps for each one.
foreach(varcertificatePathincertificatePaths){using(Signaturesignature=newSignature("sample.pdf")){// Generate signature options with helper methodDigitalSignOptionsoptions=CreateDigitalSignOptions(certificatePath,imageFilePath,iteration);// Update output file name on second iterationif(iteration==1)signedFile=resultOutputFile;// Sign document and display resultSignResultsignResult=signature.Sign(signedFile,options);Console.WriteLine($"\nDocument signed successfully {iteration + 1}-time with "+$"{signResult.Succeeded.Count} signature(s).\nFile saved at {signedFile}.");iteration++;}}
The following image demonstrates the result:
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: