Method SignAsync
SignAsync(String, FieldPosition, SignatureInfo, Behaviour)
Performs a signing process.
Declaration
Task<bool> SignAsync(string signFieldName, FieldPosition signFieldPosition, SignatureInfo signatureInfo = null, Behaviour behaviour = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | signFieldName | The name of the signature field. |
FieldPosition | signFieldPosition | The position of the signature field. |
SignatureInfo | signatureInfo | Signature field information. |
Behaviour | behaviour | Changes the behaviour of the signing process. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | The returned task results in in case of error.
|
Remarks
Signature fields in PDF documents have a field name.
If a signature field with the given signFieldName
exists in the document,
the signing applies to that field.
Else, if no signature field with the given signFieldName
exists
in the document, such a field is added at the given signFieldPosition
.
The optional signatureInfo
allows passing entries
for the signature field dictionary. Some of these entries might be used for the stamp, if used.
Whether a stamp shall be used can be controlled through the optional behaviour
parameter
among other things.
Behaviour
Examples
The following example uses the default behaviour to sign a document read from a file. To that end a new signature field is used. The position for that new field is calculated in the GetExamplePositionOnFirstPage function. There, the upper left corner of the added signature field is in the middle of the first page. The height set to 3cm, the width to 5cm. In case this does not fit the page dimension, a correction is done.
// C#
static FieldPosition? GetExamplePositionOnFirstPage(IEnumerable<IPageDimension>? pages)
{
int pageNumber = 0;
IPageDimension? firstPage = pages?.ElementAtOrDefault(pageNumber);
if (firstPage is null)
{
Console.WriteLine("This page does not exist");
return null;
}
//The length unit is inch/72.
double dpcm = 72.0 / 2.54;
double row0 = firstPage.Height / 2.0;
double column0 = firstPage.Width / 2.0;
double fieldHeight = 3.0 * dpcm;
double fieldWidth = 5.0 * dpcm;
double row1 = Math.Min(row0 + fieldHeight, firstPage.Height);
double column1 = Math.Min(column0 + fieldWidth, firstPage.Width);
FieldPosition signFieldPosition = new FieldPosition(row0, column0, row1, column1)
{
PageNumber = pageNumber
};
return signFieldPosition;
}
static SignatureInfo GetExampleSignatureInfo()
{
return new SignatureInfo()
{
Name = "Mrs. X. Ample",
Reason = "Good reason.",
Location = "Nice location.",
};
}
// Check out the documentation of the DeviceAPI on how to get an IDriver object:
// https://www.stepoverinfo.net/confluence/display/NETDEVAPI/
async Task SignTheExampleDocument(IDriver driver)
{
byte[] exampleDocument = File.ReadAllBytes(PathToExampleDocument);
using ISigning? signing = await SigningFactory.StartAsync(driver, exampleDocument);
if (signing is null)
{
Console.WriteLine("This should not happen. Probably the pdf service is not running.");
return;
}
IEnumerable<IPageDimension>? pages = await signing.Client.GetPagesAsync();
FieldPosition? signFieldPosition = GetExamplePositionOnFirstPage(pages);
if (signFieldPosition is null) return;
string signFieldName = $"SignField_{DateTime.UtcNow}".Replace('.', '_'); // No period characters.
bool success = await signing.SignAsync(
signFieldName, signFieldPosition, GetExampleSignatureInfo()));
if (!success)
{
Console.WriteLine("Signing failed.");
return
}
byte[] signedDocument = await signing.Client.DownloadPdfAsync());
Files.WriteAllBytes("SignedExample.pdf", signedDocument);
}
SignAsync()
Performs an xml signing process. A valid xml must be loaded to the StartAsync for this method to proceed
Declaration
Task<bool> SignAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Boolean> | bool |
Examples
IDriver? driver = Driver.FromCommunicationType(Sig.DeviceCom.CommunicationType.USB);; driver.IsSignFinishedEnabled = true;
if (driver is null) { return; } using ISigning? signing = await SigningFactory.StartAsync(driver, "Path to a valid xml"); if (signing is null) { Console.WriteLine("This should not happen. Check the logs."); return; }
bool success = await signing.SignAsync();