void oldVerify() { bool hasCert = false; bool verifiedCert = false; // Load PDF document using (Document pdfDocument = new Document("c:\\temp\\valid signed contract.pdf")) { using (PdfFileSignature signature = new PdfFileSignature(pdfDocument)) { IList <string> sigNames = signature.GetSignNames(); if (sigNames.Count > 0) // Any signatures? { verifiedCert = signature.VerifySignature(sigNames[0] as string); if (signature.VerifySigned(sigNames[0] as string)) // Verify first one { if (signature.IsCertified) // Certified? { if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission { // Do something } } } } } } }
public static void VerifySignatures() { // ExStart:VerifySignatures // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_TechnicalArticles(); // Create PdfFileSignature object PdfFileSignature pdfVerify = new PdfFileSignature(); // Bind input PDF file pdfVerify.BindPdf(dataDir + "inFile.pdf"); // Check if PDF contains signature bool isSigned = pdfVerify.ContainsSignature(); // All signatures have names Signaure#, this names kit generates automatically bool isSignatureVerified = pdfVerify.VerifySignature("Signature1"); // However we can set necessary name manualy if we use signature fields (see below) bool isSignatureVerified2 = pdfVerify.VerifySignature("Signature from Alice"); // ExEnd:VerifySignatures }
private static void Run_34561_tests() { // ExStart:Run_34561_tests new License().SetLicense(@"E:\Aspose.Pdf.lic"); string inSingleSignedFile = @"C:\pdftest\PDFNEWNET_34561_SingleSigned.pdf"; string outSingleUnsignedFile = @"C:\pdftest\PDFNEWNET_34561_SingleUnSigned.pdf"; string inOutSingleResignedFile = @"C:\pdftest\PDFNEWNET_34561_SingleReSigned.pdf"; PdfFileSignature pdfSignSingle = new PdfFileSignature(); pdfSignSingle.BindPdf(inSingleSignedFile); IList names = pdfSignSingle.GetSignNames(); Stream pfx = new FileStream(@"C:\pdftest\test1.pfx", FileMode.Open); PKCS7 pcks = new PKCS7(pfx, "test1"); string sigNameSingle = names[0] as string; if (sigNameSingle != null && sigNameSingle != string.Empty) { pdfSignSingle.RemoveSignature(sigNameSingle, false); pdfSignSingle.Save(outSingleUnsignedFile); PdfFileSignature pdfSignSingle2 = new PdfFileSignature(); pdfSignSingle2.BindPdf(outSingleUnsignedFile); pdfSignSingle2.SignatureAppearance = @"C:\pdftest\butterfly.jpg"; pdfSignSingle2.Sign("Signature1", pcks); pdfSignSingle2.Save(inOutSingleResignedFile); pdfSignSingle2.BindPdf(inOutSingleResignedFile); // Assert.True(pdfSignSingle2.VerifySignature("Signature1")); Console.Write("Signature 1 check result : " + pdfSignSingle2.VerifySignature("Signature1").ToString() + " \n"); } // Test file with multiple signatures string outManyUnsignedFile = @"C:\pdftest\PDFNEWNET_34561_ManyUnSigned.pdf"; string inOutManyResignedFile = @"C:\pdftest\PDFNEWNET_34561_ManyReSigned.pdf"; PdfFileSignature pdfSignMany = new Aspose.Pdf.Facades.PdfFileSignature(); IList sigNames = pdfSignMany.GetSignNames(); foreach (string sigName in sigNames) { pdfSignMany.RemoveSignature(sigName, false); } pdfSignMany.Save(outManyUnsignedFile); PdfFileSignature pdfSignMany2 = new PdfFileSignature(); pdfSignMany2.BindPdf(outManyUnsignedFile); pdfSignMany2.Sign("Signature1", pcks); pdfSignMany2.Save(inOutManyResignedFile); pdfSignMany2.BindPdf(inOutSingleResignedFile); // Assert.IsTrue(pdfSignMany2.VerifySignature("Signature1")); Console.Write("Signature 2 check result : " + pdfSignMany2.VerifySignature("Signature1").ToString() + " "); // ExEnd:Run_34561_tests }
public static void Run() { // ExStart:1 // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures(); File.Copy(dataDir + "blank.pdf", dataDir + "externalSignature1.pdf", true); using (FileStream fs = new FileStream(dataDir + "externalSignature1.pdf", FileMode.Open, FileAccess.ReadWrite)) { using (Document doc = new Document(fs)) { SignatureField field1 = new SignatureField(doc.Pages[1], new Rectangle(100, 400, 10, 10)); // Sign with certificate selection in the windows certificate store System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser); store.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly); // Manually chose the certificate in the store System.Security.Cryptography.X509Certificates.X509Certificate2Collection sel = System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection); Aspose.Pdf.Forms.ExternalSignature externalSignature = new Aspose.Pdf.Forms.ExternalSignature(sel[0]) { Authority = "Me", Reason = "Reason", ContactInfo = "Contact" }; field1.PartialName = "sig1"; doc.Form.Add(field1, 1); field1.Sign(externalSignature); doc.Save(); } } using (PdfFileSignature pdfSign = new PdfFileSignature(new Document(dataDir + "externalSignature1.pdf"))) { IList <string> sigNames = pdfSign.GetSignNames(); for (int index = 0; index <= sigNames.Count - 1; index++) { if (!pdfSign.VerifySigned(sigNames[index]) || !pdfSign.VerifySignature(sigNames[index])) { throw new ApplicationException("Not verified"); } } } // ExEnd:1 }
public static void Run() { try { // ExStart:VerifyValidSignature // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures(); PdfFileSignature pdfSign = new PdfFileSignature(); pdfSign.BindPdf(dataDir + "DigitallySign.pdf"); if (pdfSign.VerifySignature("Signature1")) { Console.WriteLine("Signature Verified"); } // ExEnd:VerifyValidSignature } catch (Exception ex) { Console.WriteLine(ex.Message); } }