Iterative Digital signing of the PDF document

How to apply digital signatures iteratively to a PDF document

GroupDocs.Signature provides DigitalSignOptions class to specify different amount of settings for Digital signature

How to add Digital signature:

  • 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.
  • Call the Sign method of Signature class instance and pass DigitalSignOptions to it.
  • 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

string filePath = "sample.pdf";
string fileName = Path.GetFileName(filePath);

string[] certificatePaths = new string[] { "certificate1.pfx", "certificate2.pfx" };

string outputFilePath = Path.Combine("output folder path", "SignWithDigitalIterative", fileName);
int iteration = 1;
string documentFile = filePath;

foreach (var certificatePath in certificatePaths)
{
    using (Signature signature = new Signature(documentFile))
    {
        DigitalSignOptions options = new DigitalSignOptions(certificatePath)
        {
            Password = "1234567890", // Certificate password
            Reason = $"Approved-{iteration}", // Digital certificate details
            Contact = $"John{iteration} Smith{iteration}",
            Location = $"Location-{iteration}",
            AllPages = true, // Sign all pages
            Left = 10 + 100 * (iteration - 1),
            Top = 10 + 100 * (iteration - 1),
            Width = 160,
            Height = 80,
            Margin = new Padding() { Bottom = 10, Right = 10 }
        };

        SignResult signResult = 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 files
string sourceFile = "sample.pdf";
string imageFilePath = "image.jpg";
string signedFile = "signed.pdf";
string resultOutputFile = "result.pdf";

// Paths to digital certificates for signing
string[] certificatePaths = new string[] { certificateFilePath1, certificateFilePath2 };
int iteration = 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.
private static DigitalSignOptions CreateDigitalSignOptions(string certificatePath, 
    string imageFilePath, int iteration)
{
    return new DigitalSignOptions(certificatePath)
    {
        Password = "1234567890",             // Certificate password
        Reason = "Sign",                     // Signature reason
        Contact = $"JohnSmith{iteration}",   // Contact details
        Location = $"Office{iteration}",     // Signature location
        Visible = true,
        Left = 80 + iteration * 5,           // Positioning for visibility
        Top = 600 + iteration * 5,
        Height = 50 + iteration * 5,
        Width = 200 + iteration * 5,
        ImageFilePath = imageFilePath,       // Image appearance for signature
        Appearance = new PdfDigitalSignatureAppearance
        {
            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.
  • Call the Sign method of Signature class instance and pass DigitalSignOptions to it.
  • 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 (var certificatePath in certificatePaths)
{
    using (Signature signature = new Signature("sample.pdf"))
    {
        // Generate signature options with helper method
        DigitalSignOptions options = CreateDigitalSignOptions(certificatePath, imageFilePath,
            iteration);

        // Update output file name on second iteration
        if (iteration == 1) signedFile = resultOutputFile;

        // Sign document and display result
        SignResult signResult = 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 files
string sourceFile = "sample.pdf";
string imageFilePath = "image.jpg";
string signedFile = "signed.pdf";
string resultOutputFile = "result.pdf";

// Paths to digital certificates for signing
string[] certificatePaths = new string[] { certificateFilePath1, certificateFilePath2 };
int iteration = 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.
private static DigitalSignOptions CreateDigitalSignOptions(string certificatePath, 
    string imageFilePath, int iteration)
{
    return new DigitalSignOptions(certificatePath)
    {
        // Specify the type of signature
        SignatureType = SignatureType.Digital,

        // Provide the certificate password
        Password = "1234567890",

        // Digital certificate details for signature metadata
        Reason = "Sign",                            // Reason for the signature
        Contact = "JohnSmith" + iteration,          // Contact information of the signer
        Location = "Office1" + iteration,           // Location where the document is signed

        // Enable visibility of the signature on the document
        Visible = true,

        // Specify the position and size of the signature on the page
        Left = 400,                                 // Horizontal position (X-axis)
        Top = 20 + iteration * 70,                  // Vertical position (Y-axis)
        Height = 70,                                // Height of the signature area
        Width = 200,                                // Width of the signature area

        // Add an image to represent the digital certificate's appearance
        ImageFilePath = imageFilePath,              // Path to the image file

        // Customize the signature's appearance
        Appearance = new PdfDigitalSignatureAppearance
        {
            // Set a custom font color for the signature text
            Foreground = Color.FromArgb(50, Color.Gray), 

            // Apply specific font settings for the signature
            FontFamilyName = "TimesNewRoman",        // Font family name
            FontSize = 12                            // Font size
        }
    };
}
  • Create a new instance of the Signature class and pass the source document path as a constructor parameter.
  • Call the Sign method of Signature class instance and pass DigitalSignOptions to it.
  • 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 (var certificatePath in certificatePaths)
{
    using (Signature signature = new Signature("sample.pdf"))
    {
        // Generate signature options with helper method
        DigitalSignOptions options = CreateDigitalSignOptions(certificatePath, imageFilePath, 
            iteration);

        // Update output file name on second iteration
        if (iteration == 1) signedFile = resultOutputFile;

        // Sign document and display result
        SignResult signResult = 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: Digital

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.