Thu Nov 21, 2019 10:15 am
public byte[] DigitallySignPdf(byte[] pdfdata, int StatusId)
{
Response response = new Response();
byte[] signedPdfdata = null;
try
{
Guid UserId = Guid.Empty;
if (Session != null)
{
UserId = Guid.Parse(Session["UserID"].ToString());
}
//Spire.License.LicenseProvider.SetLicenseKey(key);
//Spire.License.LicenseProvider.SetLicenseFileName("license.elic");
//Response res = new Response();
Spire.License.LicenseProvider.LoadLicense();
PdfDocument doc = new PdfDocument();
doc.LoadFromBytes(pdfdata);
int pageCount = doc.Pages.Count;
//Response certificateResponse = new Response();
////certificateResponse = _userService.GetUserCertificate(UserId);
UserCertificateModel ucm = new UserCertificateModel();
ucm = _userService.GetUserCertificate(UserId).obj;
ucm.Password = SecurityHelper.DecryptPassword(ucm.Password);
PdfCertificate cert = new PdfCertificate(ucm.Certificate, ucm.Password);
//PdfSignature signature = new PdfSignature(doc, doc.Pages[pageCount - 1], cert, "Signature1");
PdfSignature signature = new PdfSignature(doc, doc.Pages[0], cert, "Signature1");
signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(150, 90));
signature.GraphicsMode = GraphicMode.SignDetail;//.SignImageAndSignDetail;
signature.Location = new PointF(0, doc.PageSettings.Size.Height - 50);
signature.LocationInfo = "Ireland,Dublin";
signature.NameLabel = "Digitally Signed by:";
signature.Name = signature.Certificate.GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, true);
//signature.ContactInfoLabel = "ContactInfo:";
//signature.ContactInfo = signature.Certificate.GetNameInfo(System.Security.Cryptography.X509Certificates.X509NameType.SimpleName, true);
signature.LocationInfoLabel = "Location:";
signature.SignFontColor = Color.Red;
signature.ReasonLabel = "Organisation: ";
signature.Reason = "DMS Governance";
signature.DateLabel = "Date:";
signature.Date = DateTime.Now;
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
signature.Certificated = false;
signature.SignDetailsFont = new PdfFont(PdfFontFamily.TimesRoman, 5f);
signature.SignNameFont = new PdfFont(PdfFontFamily.Courier, 10);
//signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
//save and reload
//doc.SaveToStream(stream);
//doc = new PdfDocument(stream);
using (MemoryStream to_stream = new MemoryStream())
{
doc.SaveToStream(to_stream, FileFormat.PDF);
signedPdfdata = to_stream.ToArray();
}
}
catch (Exception ex)
{
if (!(ex is CustomArgNullException) && !(ex is CustomInvalidOpException))
_exceptionHandler.HandleException(ref ex, "TaskController => DigitallySignPdf");
response.IsError = true;
response.ErrorCode = "";
response.ErrorMessage = ex.Message;
}
return signedPdfdata;
}
============================================================
Also the file(FileConatinsInvisibleSinnature) is getting a signature which is no longer visible because of the Location being PointF(0,doc.PageSettings.Size.Height).
Login to view the files attached to this post.