Esempio n. 1
0
        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 Run()
 {
     try
     {
         // ExStart:RemoveSignature
         // The path to the documents directory.
         string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
         // Create PdfFileSignature object
         PdfFileSignature pdfSign = new PdfFileSignature();
         // Open PDF document
         pdfSign.BindPdf(dataDir + "DigitallySign.pdf");
         // Get list of signature names
         IList names = pdfSign.GetSignNames();
         // Remove all the signatures from the PDF file
         for (int index = 0; index < names.Count; index++)
         {
             pdfSign.RemoveSignature((string)names[index]);
         }
         // Save updated PDF file
         pdfSign.Save(dataDir + "RemoveSignature_out_.pdf");
         // ExEnd:RemoveSignature
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
 public static void Run()
 {
     try
     {
         // ExStart:RemoveSignature                
         // The path to the documents directory.
         string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
         // Create PdfFileSignature object
         PdfFileSignature pdfSign = new PdfFileSignature();
         // Open PDF document
         pdfSign.BindPdf(dataDir + "DigitallySign.pdf");
         // Get list of signature names
         IList names = pdfSign.GetSignNames();
         // Remove all the signatures from the PDF file
         for (int index = 0; index < names.Count; index++)
         {
             pdfSign.RemoveSignature((string)names[index]);
         }
         // Save updated PDF file
         pdfSign.Save(dataDir + "RemoveSignature_out.pdf");
         // ExEnd:RemoveSignature
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
        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()
        {
            try
            {
                // ExStart:DigitallySign
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
                string pbxFile = "";
                string inFile  = dataDir + @"DigitallySign.pdf";
                string outFile = dataDir + @"DigitallySign_out_.pdf";
                using (Document document = new Document(inFile))
                {
                    using (PdfFileSignature signature = new PdfFileSignature(document))
                    {
                        PKCS7                    pkcs            = new PKCS7(pbxFile, "WebSales"); // Use PKCS7/PKCS7Detached objects
                        DocMDPSignature          docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
                        System.Drawing.Rectangle rect            = new System.Drawing.Rectangle(100, 100, 200, 100);
                        // Set signature appearance
                        signature.SignatureAppearance = dataDir + @"aspose-logo.jpg";
                        // Create any of the three signature types
                        signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
                        // Save output PDF file
                        signature.Save(outFile);
                    }
                }

                using (Document document = new Document(outFile))
                {
                    using (PdfFileSignature signature = new PdfFileSignature(document))
                    {
                        IList sigNames = signature.GetSignNames();
                        if (sigNames.Count > 0)                                                                     // Any signatures?
                        {
                            if (signature.VerifySigned(sigNames[0] as string))                                      // Verify first one
                            {
                                if (signature.IsCertified)                                                          // Certified?
                                {
                                    if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission
                                    {
                                        // Do something
                                    }
                                }
                            }
                        }
                    }
                }
                // ExEnd:DigitallySign
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static void Run()
        {
            try
            {
                // ExStart:DigitallySign
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
                string pbxFile = "";
                string inFile = dataDir + @"DigitallySign.pdf";
                string outFile = dataDir + @"DigitallySign_out.pdf";
                using (Document document = new Document(inFile))
                {
                    using (PdfFileSignature signature = new PdfFileSignature(document))
                    {
                        PKCS7 pkcs = new PKCS7(pbxFile, "WebSales"); // Use PKCS7/PKCS7Detached objects
                        DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
                        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
                        // Set signature appearance
                        signature.SignatureAppearance = dataDir + @"aspose-logo.jpg";
                        // Create any of the three signature types
                        signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
                        // Save output PDF file
                        signature.Save(outFile);
                    }
                }

                using (Document document = new Document(outFile))
                {
                    using (PdfFileSignature signature = new PdfFileSignature(document))
                    {
                        IList sigNames = signature.GetSignNames();
                        if (sigNames.Count > 0) // Any signatures?
                        {
                            if (signature.VerifySigned(sigNames[0] as string)) // Verify first one
                            {
                                if (signature.IsCertified) // Certified?
                                {
                                    if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission
                                    {
                                        // Do something
                                    }
                                }
                            }
                        }
                    }
                }
                // ExEnd:DigitallySign
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        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
        }
Esempio n. 9
0
        public static void Main(string[] args)
        {
            // The path to the documents directory.
            string dataDir = Path.GetFullPath("../../../Data/");

            string inFile  = dataDir + @"input.pdf";
            string outFile = dataDir + @"output.pdf";

            using (Document document = new Document(inFile))
            {
                using (PdfFileSignature signature = new PdfFileSignature(document))
                {
                    PKCS7                    pkcs            = new PKCS7(@"c:\test.pfx", "WebSales"); // Use PKCS7/PKCS7Detached objects
                    DocMDPSignature          docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
                    System.Drawing.Rectangle rect            = new System.Drawing.Rectangle(100, 100, 200, 100);
                    //set signature appearance
                    signature.SignatureAppearance = dataDir + @"aspose-logo.jpg";
                    //create any of the three signature types
                    signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
                    //save output PDF file
                    signature.Save(outFile);
                }
            }

            using (Document document = new Document(outFile))
            {
                using (PdfFileSignature signature = new PdfFileSignature(document))
                {
                    IList sigNames = signature.GetSignNames();
                    if (sigNames.Count > 0)                                                                     // Any signatures?
                    {
                        if (signature.VerifySigned(sigNames[0] as string))                                      // Verify first one
                        {
                            if (signature.IsCertified)                                                          // Certified?
                            {
                                if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission
                                {
                                    // Do something
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 10
0
        public static void Main(string[] args)
        {
            // The path to the documents directory.
            string dataDir = Path.GetFullPath("../../../Data/");

            string inFile = dataDir+ @"input.pdf";
            string outFile = dataDir + @"output.pdf";
            using (Document document = new Document(inFile))
            {
                using (PdfFileSignature signature = new PdfFileSignature(document))
                {
                    PKCS7 pkcs = new PKCS7(@"c:\test.pfx", "WebSales"); // Use PKCS7/PKCS7Detached objects
                    DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
                    //set signature appearance
                    signature.SignatureAppearance = dataDir + @"aspose-logo.jpg";
                    //create any of the three signature types
                    signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
                    //save output PDF file
                    signature.Save(outFile);
                }
            }

            using (Document document = new Document(outFile))
            {
                using (PdfFileSignature signature = new PdfFileSignature(document))
                {
                    IList sigNames = signature.GetSignNames();
                    if (sigNames.Count > 0) // Any signatures?
                    {
                        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 Run()
 {
     try
     {
         // ExStart:ExtractSignatureInfo
         // The path to the documents directory.
         string dataDir   = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
         string input     = dataDir + "DigitallySign.pdf";
         string pkcs1File = "";
         using (PdfFileSignature pdfFileSignature = new PdfFileSignature())
         {
             pdfFileSignature.BindPdf(input);
             IList <string> sigNames = pdfFileSignature.GetSignNames();
             if (sigNames.Count > 0)
             {
                 string sigName = sigNames[0] as string;
                 if (string.IsNullOrEmpty(sigName))
                 {
                     Stream cerStream = pdfFileSignature.ExtractCertificate(sigName);
                     if (cerStream != null)
                     {
                         using (cerStream)
                         {
                             byte[] bytes = new byte[cerStream.Length];
                             using (FileStream fs = new FileStream(dataDir + pkcs1File, FileMode.CreateNew))
                             {
                                 cerStream.Read(bytes, 0, bytes.Length);
                                 fs.Write(bytes, 0, bytes.Length);
                             }
                         }
                     }
                 }
             }
         }
         // ExEnd:ExtractSignatureInfo
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
 public static void Run()
 {
     try
     {
         // ExStart:ExtractSignatureInfo               
         // The path to the documents directory.
         string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
         string input = dataDir + "DigitallySign.pdf";
         string pkcs1File = "";
         using (PdfFileSignature pdfFileSignature = new PdfFileSignature())
         {
             pdfFileSignature.BindPdf(input);
             IList sigNames = pdfFileSignature.GetSignNames();
             if (sigNames.Count > 0)
             {
                 string sigName = sigNames[0] as string;
                 if (string.IsNullOrEmpty(sigName))
                 {
                     Stream cerStream = pdfFileSignature.ExtractCertificate(sigName);
                     if (cerStream != null)
                     {
                         using (cerStream)
                         {
                             byte[] bytes = new byte[cerStream.Length];
                             using (FileStream fs = new FileStream(dataDir + pkcs1File, FileMode.CreateNew))
                             {
                                 cerStream.Read(bytes, 0, bytes.Length);
                                 fs.Write(bytes, 0, bytes.Length);
                             }
                         }
                     }
                 }
             }
         }
         // ExEnd:ExtractSignatureInfo
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }       
 public static void Run()
 {
     try
     {
         // ExStart:ExtractImages               
         // The path to the documents directory.
         string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
         string input = dataDir + "DigitallySign.pdf";
         Document doc = new Document(input);
         using (PdfFileSignature signature = new PdfFileSignature(doc))
         {
             if (signature.ContainsSignature())
             {
                 foreach (string sigName in signature.GetSignNames())
                 {
                     string outFile = dataDir + "ExtractImages_out.jpg";
                     using (Stream imageStream = signature.ExtractImage(sigName))
                     {
                         if (imageStream != null)
                         {
                             using (System.Drawing.Image image = Bitmap.FromStream(imageStream))
                             {
                                 image.Save(outFile, ImageFormat.Jpeg);
                             }
                         }
                     }
                 }
             }
         }
         // ExEnd:ExtractImages
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }       
 public static void Run()
 {
     try
     {
         // ExStart:ExtractImages
         // The path to the documents directory.
         string   dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
         string   input   = dataDir + "DigitallySign.pdf";
         Document doc     = new Document(input);
         using (PdfFileSignature signature = new PdfFileSignature(doc))
         {
             if (signature.ContainsSignature())
             {
                 foreach (string sigName in signature.GetSignNames())
                 {
                     string outFile = dataDir + "ExtractImages_out.jpg";
                     using (Stream imageStream = signature.ExtractImage(sigName))
                     {
                         if (imageStream != null)
                         {
                             using (System.Drawing.Image image = Bitmap.FromStream(imageStream))
                             {
                                 image.Save(outFile, ImageFormat.Jpeg);
                             }
                         }
                     }
                 }
             }
         }
         // ExEnd:ExtractImages
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }