コード例 #1
0
        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
        }
コード例 #2
0
        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
        }
コード例 #3
0
 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);
     }
 }
コード例 #4
0
        public static void Run()
        {
            // ExStart:HideDigitallySignedByCaption
            // The path to the documents directory.
            string dataDir   = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
            string inPfxFile = dataDir + "SampleCertificate.pfx";
            string inFile    = dataDir + "input.pdf";
            string outFile   = dataDir + "output.pdf";

            using (PdfFileSignature pdfSign = new PdfFileSignature())
            {
                pdfSign.BindPdf(inFile);
                //create a rectangle for signature location
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(310, 45, 200, 50);

                //create any of the three signature types
                PKCS7 pkcs = new PKCS7(inPfxFile, "idsrv3test");
                SignatureCustomAppearance signatureCustomAppearance = new SignatureCustomAppearance();
                signatureCustomAppearance.FontSize           = 6;
                signatureCustomAppearance.FontFamilyName     = "Times New Roman";
                signatureCustomAppearance.DigitalSignedLabel = "Signed by me";
                pkcs.CustomAppearance = signatureCustomAppearance;
                // sign the PDF file
                pdfSign.Sign(1, true, rect, pkcs);
                //save output PDF file
                pdfSign.Save(outFile);
            }
            // ExEnd:HideDigitallySignedByCaption
        }
コード例 #5
0
 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);
     }
 }
コード例 #6
0
 public static void Run()
 {
     try
     {
         // ExStart:DigitallySignature
         string pbxFile = "";
         // The path to the documents directory.
         string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
         // Create PdfFileSignature object and bind input and output PDF files
         PdfFileSignature pdfSign = new PdfFileSignature();
         pdfSign.BindPdf(dataDir + "DigitallySign.pdf");
         // Create a rectangle for signature location
         System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
         // Set signature appearance
         pdfSign.SignatureAppearance = dataDir + "aspose-logo.jpg";
         // Create any of the three signature types
         PKCS1 signature = new PKCS1(pbxFile, "password"); // PKCS#1 or
         
         pdfSign.Sign(1, "Signature Reason", "Contact", "Location", true, rect, signature);
         // Save output PDF file
         pdfSign.Save(dataDir + "DigitallySignature_out.pdf");
         // ExEnd:DigitallySignature
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
コード例 #7
0
        public static void Run()
        {
            try
            {
                // ExStart:DigitallySignature
                string pbxFile = "";
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
                // Create PdfFileSignature object and bind input and output PDF files
                PdfFileSignature pdfSign = new PdfFileSignature();
                pdfSign.BindPdf(dataDir + "DigitallySign.pdf");
                // Create a rectangle for signature location
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
                // Set signature appearance
                pdfSign.SignatureAppearance = dataDir + "aspose-logo.jpg";
                // Create any of the three signature types
                PKCS1 signature = new PKCS1(pbxFile, "password"); // PKCS#1 or

                pdfSign.Sign(1, "Signature Reason", "Contact", "Location", true, rect, signature);
                // Save output PDF file
                pdfSign.Save(dataDir + "DigitallySignature_out.pdf");
                // ExEnd:DigitallySignature
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #8
0
        public static void Run()
        {
            try
            {
                // ExStart:RemoveRights
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
                string input   = dataDir + "DigitallySign.pdf";
                using (PdfFileSignature pdfSign = new PdfFileSignature())
                {
                    pdfSign.BindPdf(input);
                    if (pdfSign.ContainsUsageRights())
                    {
                        pdfSign.RemoveUsageRights();
                    }

                    pdfSign.Document.Save(dataDir + "RemoveRights_out_.pdf");
                }
                // ExEnd:RemoveRights
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #9
0
        public static void Run()
        {
            try
            {
                // ExStart:RemoveRights               
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
                string input = dataDir + "DigitallySign.pdf";
                using (PdfFileSignature pdfSign = new PdfFileSignature())
                {
                    pdfSign.BindPdf(input);
                    if (pdfSign.ContainsUsageRights())
                    {
                        pdfSign.RemoveUsageRights();
                    }

                    pdfSign.Document.Save(dataDir + "RemoveRights_out.pdf");
                }
                // ExEnd:RemoveRights
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }       
コード例 #10
0
        public static void VerifyPDFSigned()
        {
            // ExStart:VerifyPDFSigned               
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();

            PdfFileSignature pdfSign = new PdfFileSignature();
            pdfSign.BindPdf(dataDir + "DigitallySign.pdf");
            pdfSign.ContainsSignature();
            // Any signatures?
            pdfSign.Close();
            // ExEnd:VerifyPDFSigned  
        }
コード例 #11
0
        public static void VerifyPDFSigned()
        {
            // ExStart:VerifyPDFSigned
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();

            PdfFileSignature pdfSign = new PdfFileSignature();

            pdfSign.BindPdf(dataDir + "DigitallySign.pdf");
            pdfSign.ContainsSignature();
            // Any signatures?
            pdfSign.Close();
            // ExEnd:VerifyPDFSigned
        }
コード例 #12
0
 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
 }
コード例 #13
0
        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
        }
コード例 #14
0
 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);
     }
 }       
コード例 #15
0
 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);
     }
 }
コード例 #16
0
 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);
     }
 }       
コード例 #17
0
 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);
     }
 }