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 As Stream, _
ByVal As PdfSignatureMode, _
ByVal As Boolean _
) As GdPictureStatus
public GdPictureStatus ApplySignature(
Stream ,
PdfSignatureMode ,
bool
)
public function ApplySignature(
: Stream;
: PdfSignatureMode;
: Boolean
): GdPictureStatus;
public function ApplySignature(
: Stream,
: PdfSignatureMode,
: boolean
) : GdPictureStatus;
public: GdPictureStatus ApplySignature(
Stream* ,
PdfSignatureMode ,
bool
)
public:
GdPictureStatus ApplySignature(
Stream^ ,
PdfSignatureMode ,
bool
)
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.
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)", "contact@orpalis.com")
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)", "contact@orpalis.com");
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();
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)", "contact@orpalis.com")
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)", "contact@orpalis.com");
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