Signing a PDF using a TimeStamp Server
| Minimum Version | Q4 2025 |
|---|
The TimeStampServer class encapsulates the necessary details to communicate with an external Timestamp Authority (TSA), including the endpoint URL, optional authentication credentials and timeout for requests. The TimeStampServer can be set via the SignatureSettings.
string certificateFilePath = "JohnDoe.pfx"; //"certificate.pfx";
string certificateFilePassword = "johndoe"; //"password";
int signatureFieldWidth = 200;
int signatureFieldHeight = 50;
int signaturePositionLeft = 10;
int signaturePositionTop = 10;
X509Certificate2 certificate = new X509Certificate2(
File.ReadAllBytes(certificateFilePath),
certificateFilePassword,
X509KeyStorageFlags.EphemeralKeySet);
SignatureField pdfSignature = new SignatureField("SignatureField");
TimeStampServer timeStampServer = new TimeStampServer("url", TimeSpan.FromSeconds(10));
Signature signature = new Signature(certificate);
signature.Settings.TimeStampServer = timeStampServer;
pdfSignature.Signature = signature;
Form pdfForm = new Form();
pdfForm.FormSource = new FormSource();
pdfForm.FormSource.Size = new Size(signatureFieldWidth, signatureFieldHeight);
FixedContentEditor editor = new FixedContentEditor(pdfForm.FormSource);
pdfForm.Position.Translate(signaturePositionLeft, signaturePositionTop);
editor.DrawText($"{"Signed on"} {DateTime.Now.ToString("yyyy.MM.dd HH:mm")}");
SignatureWidget signatureWidget = pdfSignature.Widgets.AddWidget();
signatureWidget.Content.NormalContentSource = pdfForm.FormSource;
signatureWidget.Rect = new Rect(signaturePositionLeft, signaturePositionTop, signatureFieldWidth, signatureFieldHeight);
signatureWidget.RecalculateContent();
RadFixedDocument fixedDocument = new RadFixedDocument();
RadFixedPage pdfPage = fixedDocument.Pages.AddPage();
pdfPage.Annotations.Add(signatureWidget);
FixedContentEditor pageEditor = new FixedContentEditor(pdfPage);
pageEditor.Position.Translate(signaturePositionLeft, signaturePositionTop);
pageEditor.DrawForm(pdfForm.FormSource);
fixedDocument.AcroForm.FormFields.Add(pdfSignature);
signatureWidget.RecalculateContent();
PdfFormatProvider provider = new PdfFormatProvider();
string signedDocumentFilePath = "Signed.pdf";
File.Delete(signedDocumentFilePath);
using (System.IO.Stream output = new System.IO.FileStream(signedDocumentFilePath,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite))
{
provider.Export(fixedDocument, output, TimeSpan.FromSeconds(10));
}
Process.Start(new ProcessStartInfo() { FileName = signedDocumentFilePath, UseShellExecute = true });
The produced result document indicates a valid embedded timestamp:
Creating TimeStampServer with Credentials
The following example shows how to initialize a new instance of the TimeStampServer class with the specified URL, credentials, and timeout:
//Alternative TimeStampServer initialization with credentials
string URL = "url";
string username = "username";
string password = "password";
TimeSpan timeout = TimeSpan.FromSeconds(10);
TimeStampServer timeStampServer = new TimeStampServer(URL, username, password, timeout);