Reference Guide
Programming / PDF / How to digitally sign or certify PDF documents
In This Topic
    How to digitally sign or certify PDF documents
    In This Topic

    The possibility to digitally sign or certify your PDF documents becomes fairly important if you need to share documents or do business on the web. This example shows you all necessarily steps and their proper order to successfully follow the signing process.

    It is also very easy to apply multiple signatures at once using GdPicture. If your business requires to involve more than one digital signature to your PDF document in a time, you only need to reload the PDF document after every single signing. This way you will not invalidate the previously applied signatures.

    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    Dim caption As String = "Digital Signature Example"
    Dim oGdPicturePDF As New GdPicturePDF()
    'Loading of the PDF document you want to sign.
    Dim status As GdPictureStatus = oGdPicturePDF.LoadFromFile("file_to_sign.pdf", False)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The file can't be loaded.", caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'The proper order of mandatory steps is important.
    'Mandatory steps are the step #1 and the step #2 and the last step #5.
    'Step 1: Setting up the certicate, your digital ID file.
    status = oGdPicturePDF.SetSignatureCertificateFromP12("your_digital_ID.pfx", "your_password")
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureCertificateFromP12() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 2: Setting up the signature information. At least one parameter must be set, others may stay empty.
    status = oGdPicturePDF.SetSignatureInfo("Orpalis", "Important PDF", "Toulouse (France)", "contact@orpalis.com")
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureInfo() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Optional steps are the step #3 and the step #4.
    'You can select from these options what do you prefer. The order of these steps is not strictly determined.
    'Step 3a: Setting up the signature's location on the current page. If this step is ommited, the signature will be invisible.
    status = oGdPicturePDF.SetSignaturePos(300, 100, 200, 100)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignaturePos() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 3b: Setting up the text to be displayed within the signature's bounding box. The text will not be drawn if the signature is invisible.
    status = oGdPicturePDF.SetSignatureText("", "", 12, Color.Navy, TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, True)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureText() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 3c: Setting up the image to be displayed within the signature's bounding box. The image will not be drawn if the signature is invisible.
    Dim imageName As String = oGdPicturePDF.AddJpegImageFromFile("your_image.jpg")
    status = oGdPicturePDF.GetStat()
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method AddJpegImageFromFile() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    status = oGdPicturePDF.SetSignatureStampImage(imageName)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureStampImage() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 3d: Setting up the icon that represents the validity of the signature.
    status = oGdPicturePDF.SetSignatureValidationMark(True)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureValidationMark() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 4a: Setting up the signature certification level.
    'PdfSignatureCertificationLevel.NotCertified represents standard recipient digital signatures (approval signatures).
    'The other enumerations represent the certifying signatures that controls the way how recipients can change the document.
    status = oGdPicturePDF.SetSignatureCertificationLevel(PdfSignatureCertificationLevel.NotCertified)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureCertificationLevel() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 4b: Setting up the hash algorithm.
    'The used standard (default) is SHA256, the other ones are stronger.
    status = oGdPicturePDF.SetSignatureHash(PdfSignatureHash.SHA512)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureHash() has failed with the status " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 4c: Setting up the timestamp information.
    status = oGdPicturePDF.SetSignatureTimestampInfo("your_time_stamp_server_url", "your_username", "your_password")
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("The method SetSignatureTimestampInfo() has failed with the status " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
        GoTo [error]
    End If
    'Step 5: The last step - sign. This step must be the last one. All other optional steps may be done in any order.
    status = oGdPicturePDF.ApplySignature("signed.pdf", PdfSignatureMode.PdfSignatureModeAdobePPKMS, True)
    If status = GdPictureStatus.OK Then
        MessageBox.Show("The document has been signed successfully and the file has been saved.", caption, MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
        MessageBox.Show("The method ApplySignature() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    [error]:
    oGdPicturePDF.Dispose()
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    string caption = "Digital Signature Example";
    GdPicturePDF oGdPicturePDF = new GdPicturePDF();
    //Loading of the PDF document you want to sign.
    GdPictureStatus status = oGdPicturePDF.LoadFromFile("file_to_sign.pdf", false);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The file can't be loaded.", caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //The proper order of mandatory steps is important.
    //Mandatory steps are the step #1 and the step #2 and the last step #5.
    //Step 1: Setting up the certicate, your digital ID file.
    status = oGdPicturePDF.SetSignatureCertificateFromP12("your_digital_ID.pfx", "your_password");
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureCertificateFromP12() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 2: Setting up the signature information. At least one parameter must be set, others may stay empty.
    status = oGdPicturePDF.SetSignatureInfo("Orpalis", "Important PDF", "Toulouse (France)", "contact@orpalis.com");
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureInfo() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Optional steps are the step #3 and the step #4.
    //You can select from these options what do you prefer. The order of these steps is not strictly determined.
    //Step 3a: Setting up the signature's location on the current page. If this step is ommited, the signature will be invisible.
    status = oGdPicturePDF.SetSignaturePos(300, 100, 200, 100);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignaturePos() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 3b: Setting up the text to be displayed within the signature's bounding box. The text will not be drawn if the signature is invisible.
    status = oGdPicturePDF.SetSignatureText("", "", 12, Color.Navy, TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, true);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureText() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 3c: Setting up the image to be displayed within the signature's bounding box. The image will not be drawn if the signature is invisible.
    string imageName = oGdPicturePDF.AddJpegImageFromFile("your_image.jpg");
    status = oGdPicturePDF.GetStat();
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method AddJpegImageFromFile() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    status = oGdPicturePDF.SetSignatureStampImage(imageName);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureStampImage() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 3d: Setting up the icon that represents the validity of the signature.
    status = oGdPicturePDF.SetSignatureValidationMark(true);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureValidationMark() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 4a: Setting up the signature certification level.
    //PdfSignatureCertificationLevel.NotCertified represents standard recipient digital signatures (approval signatures).
    //The other enumerations represent the certifying signatures that controls the way how recipients can change the document.
    status = oGdPicturePDF.SetSignatureCertificationLevel(PdfSignatureCertificationLevel.NotCertified);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureCertificationLevel() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 4b: Setting up the hash algorithm.
    //The used standard (default) is SHA256, the other ones are stronger.
    status = oGdPicturePDF.SetSignatureHash(PdfSignatureHash.SHA512);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureHash() has failed with the status " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 4c: Setting up the timestamp information.
    status = oGdPicturePDF.SetSignatureTimestampInfo("your_time_stamp_server_url", "your_username", "your_password");
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("The method SetSignatureTimestampInfo() has failed with the status " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
        goto error;
    }
    //Step 5: The last step - sign. This step must be the last one. All other optional steps may be done in any order.
    status = oGdPicturePDF.ApplySignature("signed.pdf", PdfSignatureMode.PdfSignatureModeAdobePPKMS, true);
    if (status == GdPictureStatus.OK)
        MessageBox.Show("The document has been signed successfully and the file has been saved.", caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
    else
        MessageBox.Show("The method ApplySignature() has failed with the status: " + status.ToString(), caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
    error:
    oGdPicturePDF.Dispose();