PdfLoadedDocument document = new PdfLoadedDocument(System.Convert.FromBase64String(tDocObj.tPayload.strDocBase64));
PdfCertificate cert = new PdfCertificate(selectedCertificate[0]);
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
// Position of signature box
float ox = tDocObj.tPayload.afSignatureBox[0];
float oy = page.Graphics.Size.Height - tDocObj.tPayload.afSignatureBox[1] - tDocObj.tPayload.afSignatureBox[3];
float w = tDocObj.tPayload.afSignatureBox[2];
float h = tDocObj.tPayload.afSignatureBox[3];
PdfSignature signature = new PdfSignature(document, page, cert, "Signature" + DateTime.Now.ToString("yyyyMMdd HHmmssK"));
signature.TimeStampServer = new TimeStampServer(new Uri("http://rfc3161timestamp.globalsign.com/advanced"));
signature.DocumentPermissions = PdfCertificationFlags.AllowComments | PdfCertificationFlags.AllowFormFill;
signature.Bounds = new RectangleF(ox, oy, w, h);
signature.EnableValidationAppearance = false;
signature.EnableLtv = true;
signature.ContactInfo = "CONTACT";
signature.LocationInfo = "LOCATION";
signature.Reason = "REASON";
string htmlText = String.Format("Digitally signed by\n{0}\nDate: {1}", selectedCertificate[0].GetNameInfo(X509NameType.SimpleName, false), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ssK"));
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, new PdfStandardFont(PdfFontFamily.Helvetica, 14), PdfBrushes.Black);
richTextElement.TextAlign = TextAlign.Left;
PdfTemplate template = new PdfTemplate(w, h);
richTextElement.Draw(template.Graphics, new RectangleF(new PointF(0, 0), new SizeF(w, h)));
PdfBitmap image = new PdfBitmap("Logo.png");
//Draw the logo
PdfGraphicsState state = template.Graphics.Save();
template.Graphics.SetTransparency(0.1f);
if (image.Height > template.Graphics.ClientSize.Height || image.Width > template.Graphics.ClientSize.Width)
template.Graphics.DrawImage(image, PointF.Empty, new SizeF(w, h));
else
template.Graphics.DrawImage(image, 0, 0);
template.Graphics.Restore(state);
signature.Appearance.Normal.Graphics.DrawPdfTemplate(template, new PointF(0, 0));
//Saves and closes the document.
FileStream tOutStream = new FileStream(strTempOutFile, FileMode.Create);
document.Save(tOutStream);
document.Close(true);
tOutStream.Close();