GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / ApplySignature Method / ApplySignature(Stream,PdfSignatureMode,Boolean) Method
A Stream object, where the currently loaded PDF document will be saved to after the successful signing.
A member of the PdfSignatureMode enumeration. Specifies the electronic signature technology (security handler) to be used during the signing process.
Specifies if the output PDF document should be linearized when saving.
Example





In This Topic
ApplySignature(Stream,PdfSignatureMode,Boolean) Method
In This Topic
Applies the digital signature or the certificate on the current page of the loaded PDF document according to all settings you have provided. This method uses the output filename as a Stream parameter.

Please follow the example below to find out the correct steps for setting up the particular parameters you need. Some of these steps are mandatory, some of them are optional. The ApplySignature(Stream,PdfSignatureMode,Boolean) method is always the last one you need to call. Be aware that all settings are applied to the currently loaded PDF document.

You are also allowed to successfully apply more than one digital signature to the PDF document in a time. You only need to reload the currently signed PDF document after every single signing. This way you will not invalidate the previously applied signature.

Syntax
'Declaration
 
Public Overloads Function ApplySignature( _
   ByVal OutputStream As Stream, _
   ByVal SignatureMode As PdfSignatureMode, _
   ByVal Linearization As Boolean _
) As GdPictureStatus
public function ApplySignature( 
    OutputStream: Stream;
    SignatureMode: PdfSignatureMode;
    Linearization: Boolean
): GdPictureStatus; 
public function ApplySignature( 
   OutputStream : Stream,
   SignatureMode : PdfSignatureMode,
   Linearization : boolean
) : GdPictureStatus;
public: GdPictureStatus ApplySignature( 
   Stream* OutputStream,
   PdfSignatureMode SignatureMode,
   bool Linearization
) 
public:
GdPictureStatus ApplySignature( 
   Stream^ OutputStream,
   PdfSignatureMode SignatureMode,
   bool Linearization
) 

Parameters

OutputStream
A Stream object, where the currently loaded PDF document will be saved to after the successful signing.
SignatureMode
A member of the PdfSignatureMode enumeration. Specifies the electronic signature technology (security handler) to be used during the signing process.
Linearization
Specifies if the output PDF document should be linearized when saving.

Return Value

A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.

We strongly recommend always checking this status first.

Remarks
This method is only allowed for use with non-encrypted documents.

Just to remind you that PPKLite is the old technology, prefer PPKMS. If you need to meet EU requirements, choose CAdES. PPKLite uses PKCS#1 standard and PPKMS uses PKCS#7 standard.

Likewise, be aware that from 2016 onward the SHA-1 algorithm is deprecated, therefore the SHA-256 algorithm is used as default. You can change it using the SetSignatureHash method.

This method requires the Digital Signatures component to run.

Example
How to sign a PDF document. The example shows you all steps you need to follow for successful signing.
Dim caption As String = "Digital Signature"
Dim gdpicturePDF As New GdPicturePDF()
            
Dim status As GdPictureStatus = gdpicturePDF.LoadFromFile("file_to_sign.pdf", False)
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The file can't be loaded.", caption)
    Goto [Error]
End If
            
'Mandatory steps are step #1 and step #2 and the last step #5.
            
'Step 1 : Set the certificate, your digital ID file.
status = gdpicturePDF.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)
    Goto [Error]
End If
            
'Step 2 : Set the signature information. At least one parameter must be set, others may stay empty.
status = gdpicturePDF.SetSignatureInfo("Orpalis", "Important PDF", "Toulouse (France)", "[email protected]")
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The method SetSignatureInfo() has failed with the status: " + status.ToString(), caption)
    Goto [Error]
End If
            
'Optional steps.
            
'Step 3a : Set the signature's location on the current page. If this step is omitted, the signature will be invisible.
status = gdpicturePDF.SetSignaturePos(300, 100, 200, 100)
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The method SetSignaturePos() has failed with the status: " + status.ToString(), caption)
    Goto [Error]
End If
            
'Step 3b : Set the text to be displayed within the signature's bounding box. The text will not be drawn if the signature is invisible.
status = gdpicturePDF.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)
    Goto [Error]
End If
            
'Step 3c : Set 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 = gdpicturePDF.AddJpegImageFromFile("your_image.jpg")
status = gdpicturePDF.GetStat()
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The method AddJpegImageFromFile() has failed with the status: " + status.ToString(), caption)
    Goto [Error]
End If
status = gdpicturePDF.SetSignatureStampImage(imageName)
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The method SetSignatureStampImage() has failed with the status: " + status.ToString(), caption)
    Goto [Error]
End If
            
'Step 3d : Set the icon that represents the validity of the signature.
status = gdpicturePDF.SetSignatureValidationMark(True)
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The method SetSignatureValidationMark() has failed with the status: " + status.ToString(), caption)
    Goto [Error]
End If
            
'Step 4a : Set the signature certification level.
status = gdpicturePDF.SetSignatureCertificationLevel(PdfSignatureCertificationLevel.NotCertified)
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The method SetSignatureCertificationLevel() has failed with the status: " + status.ToString(), caption)
    Goto [Error]
End If
            
'Step 4b : Set the hash algorithm.
status = gdpicturePDF.SetSignatureHash(PdfSignatureHash.SHA256)
If status <> GdPictureStatus.OK Then
    MessageBox.Show("The method SetSignatureHash() has failed with the status " + status.ToString(), caption)
    Goto [Error]
End If
            
'Step 4c : Set the timestamp information.
status = gdpicturePDF.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)
    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.
Dim streamSigned As New System.IO.FileStream("signed.pdf", System.IO.FileMode.Create)
status = gdpicturePDF.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)
Else
    MessageBox.Show("The method ApplySignature() has failed with the status: " + status.ToString(), caption)
End If
streamSigned.Dispose()
            
[error]:
gdpicturePDF.Dispose()
string caption = "Digital Signature";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
            
GdPictureStatus status = gdpicturePDF.LoadFromFile("file_to_sign.pdf", false);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The file can't be loaded.", caption);
    goto error;
}
            
//Mandatory steps are step #1 and step #2 and the last step #5.
            
//Step 1 : Set the certificate, your digital ID file.
status = gdpicturePDF.SetSignatureCertificateFromP12("your_digital_ID.pfx", "your_password");
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method SetSignatureCertificateFromP12() has failed with the status: " + status.ToString(), caption);
    goto error;
}
            
//Step 2 : Set the signature information. At least one parameter must be set, others may stay empty.
status = gdpicturePDF.SetSignatureInfo("Orpalis", "Important PDF", "Toulouse (France)", "[email protected]");
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method SetSignatureInfo() has failed with the status: " + status.ToString(), caption);
    goto error;
}
            
//Optional steps.
            
//Step 3a : Set the signature's location on the current page. If this step is omitted, the signature will be invisible.
status = gdpicturePDF.SetSignaturePos(300, 100, 200, 100);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method SetSignaturePos() has failed with the status: " + status.ToString(), caption);
    goto error;
}
            
//Step 3b : Set the text to be displayed within the signature's bounding box. The text will not be drawn if the signature is invisible.
status = gdpicturePDF.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);
    goto error;
}
            
//Step 3c : Set the image to be displayed within the signature's bounding box. The image will not be drawn if the signature is invisible.
string imageName = gdpicturePDF.AddJpegImageFromFile("your_image.jpg");
status = gdpicturePDF.GetStat();
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method AddJpegImageFromFile() has failed with the status: " + status.ToString(), caption);
    goto error;
}
status = gdpicturePDF.SetSignatureStampImage(imageName);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method SetSignatureStampImage() has failed with the status: " + status.ToString(), caption);
    goto error;
}
            
//Step 3d : Set the icon that represents the validity of the signature.
status = gdpicturePDF.SetSignatureValidationMark(true);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method SetSignatureValidationMark() has failed with the status: " + status.ToString(), caption);
    goto error;
}
            
//Step 4a : Set the signature certification level.
status = gdpicturePDF.SetSignatureCertificationLevel(PdfSignatureCertificationLevel.NotCertified);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method SetSignatureCertificationLevel() has failed with the status: " + status.ToString(), caption);
    goto error;
}
            
//Step 4b : Set the hash algorithm.
status = gdpicturePDF.SetSignatureHash(PdfSignatureHash.SHA256);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("The method SetSignatureHash() has failed with the status " + status.ToString(), caption);
    goto error;
}
            
//Step 4c : Set the timestamp information.
status = gdpicturePDF.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);
    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.
System.IO.FileStream streamSigned = new System.IO.FileStream("signed.pdf", System.IO.FileMode.Create);
status = gdpicturePDF.ApplySignature(streamSigned, PdfSignatureMode.PdfSignatureModeAdobePPKMS, true);
if (status == GdPictureStatus.OK)
    MessageBox.Show("The document has been signed successfully and the file has been saved.", caption);
else
    MessageBox.Show("The method ApplySignature() has failed with the status: " + status.ToString(), caption);
streamSigned.Dispose();
            
error:
gdpicturePDF.Dispose();
See Also