Beispiel #1
0
        public void ProtectWithScreenReaderRestrictions(
            [Values(
                 FileAuthentication.Password /*,
                                              * Ignore: http://stackoverflow.com/questions/40045745/itextsharp-object-reference-error-on-pdfstamper-for-certificate-protected-file
                                              * FileAuthentication.CertificateFile,
                                              * FileAuthentication.CertificateStore*/)] FileAuthentication protectAuth,
            [Values(
                 true,
                 false)] bool allowScreenReaders)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.ChangeProtection.Resources.Protect.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "Protect.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <ChangeProtectionProvider>();

            ConfigureInputFileFunctionValues(designer, FileAuthentication.None, inputFilePath);
            ConfigureProtectFunctionValues(designer, protectAuth, Encryption.AES256, true, true, allowScreenReaders: allowScreenReaders);
            designer.Properties[Pdf.Common.PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <ChangeProtectionProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            PdfComparer.AssertProtection(outputFilePath, protectAuth, this.authenticationManager, true, true,
                                         expectedAllowScreenReaders: allowScreenReaders);

            if (protectAuth == FileAuthentication.Password)
            {
                using (var permissionsAuthHelper = new AuthenticationManager(permissionsPassword))
                {
                    PdfComparer.AssertProtectionAllRights(outputFilePath, FileAuthentication.Password, permissionsAuthHelper, true, true);
                }
            }
        }
        public PdfReader GetReader(string filePath, FileAuthentication fileAuth)
        {
            CheckFileExists(filePath);

            switch (fileAuth)
            {
            case FileAuthentication.None:
                return(new PdfReader(filePath));

            case FileAuthentication.Password:
                return(new PdfReader(filePath, Encoding.UTF8.GetBytes(Password)));

            case FileAuthentication.CertificateFile:
            case FileAuthentication.CertificateStore:
                if (!Certificate.HasPrivateKey)
                {
                    throw new NotSupportedException("Certificate must have a private key.");
                }

                return(new PdfReader(filePath, DotNetUtilities.FromX509Certificate(Certificate), DotNetUtilities.GetKeyPair(Certificate.PrivateKey).Private));

            default:
                throw new NotSupportedException("Invalid FileAuthentication specified.");
            }
        }
Beispiel #3
0
        public void AddWatermark(
            [Values(
                 FileAuthentication.None,
                 FileAuthentication.Password /*,
                                              * Ignore: http://stackoverflow.com/questions/40045745/itextsharp-object-reference-error-on-pdfstamper-for-certificate-protected-file
                                              * FileAuthentication.CertificateFile,
                                              * FileAuthentication.CertificateStore*/)] FileAuthentication inputAuth,
            [Values(
                 WatermarkPosition.Above,
                 WatermarkPosition.Below)] WatermarkPosition position)
        {
            string inputFilePath     = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.AddWatermark.Resources.Watermark.pdf", this.inputDirectory);
            string watermarkFilePath = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.AddWatermark.Resources.Overlay.pdf", this.inputDirectory);
            string outputFilePath    = Path.Combine(this.outputDirectory, "Watermark.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <AddWatermarkProvider>();

            ConfigureInputFileFunctionValues(designer, inputAuth, inputFilePath);
            var watermarkPages = "4;1-2,2,2";

            ConfigureWatermarkFunctionValues(designer, FileAuthentication.None, watermarkFilePath, position, watermarkPages);
            designer.Properties[Pdf.Common.PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <AddWatermarkProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            PdfComparer.AssertText(outputFilePath, inputAuth, this.authenticationManager, "1\nWatermark\r\n2\nWatermark\r\n3\r\n4\nWatermark", null);
        }
        public void Protect(string filePath, FileAuthentication fileAuth)
        {
            CheckFileExists(filePath);

            if (fileAuth == FileAuthentication.None)
            {
                return;
            }

            string outputFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            using (var reader = new PdfReader(filePath))
                using (var outputStream = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                    using (var stamper = new PdfStamper(reader, outputStream))
                    {
                        switch (fileAuth)
                        {
                        case FileAuthentication.Password:
                            stamper.SetEncryption(PdfWriter.ENCRYPTION_AES_256, Password, Password, 0);
                            break;

                        case FileAuthentication.CertificateFile:
                        case FileAuthentication.CertificateStore:
                            var certificates = new Org.BouncyCastle.X509.X509Certificate[] { DotNetUtilities.FromX509Certificate(new X509Certificate2(CertificateFilePath, CertificateFilePassword)) };
                            stamper.SetEncryption(certificates, new int[] { 0 }, PdfWriter.ENCRYPTION_AES_256);
                            break;

                        default:
                            throw new NotSupportedException("Invalid FileAuthentication specified.");
                        }
                    }

            File.Delete(filePath);
            File.Move(outputFilePath, filePath);
        }
Beispiel #5
0
        public void SignWithInvisibleSignature(
            [Values(
                 FileAuthentication.None,
                 FileAuthentication.Password /*,
                                              * Ignore: http://stackoverflow.com/questions/40045745/itextsharp-object-reference-error-on-pdfstamper-for-certificate-protected-file
                                              * FileAuthentication.CertificateFile,
                                              * FileAuthentication.CertificateStore*/)] FileAuthentication inputAuth,
            [Values(
                 FileAuthentication.CertificateFile,
                 FileAuthentication.CertificateStore)] FileAuthentication signAuth,
            [Values(
                 "Sign.pdf",
                 "SignXFA.pdf")] string fileName)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile($"Twenty57.Linx.Components.Pdf.Tests.Sign.Resources.{fileName}", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, fileName);

            this.lockDocument = !this.lockDocument;

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <SignProvider>();

            ConfigureInputFileFunctionValues(designer, inputAuth, inputFilePath);
            ConfigureSignCertificateProperties(designer, signAuth, this.lockDocument);
            designer.Properties[PropertyNames.Placement].Value = SignaturePosition.Hidden;
            designer.Properties[Pdf.Common.PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <SignProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            PdfComparer.AssertPageSignature(outputFilePath, inputAuth, this.authenticationManager, signName, signLocation, signReason, this.lockDocument, 1, 0, 0, 0, 0);
        }
Beispiel #6
0
        private void ConfigureSignCertificateProperties(FunctionDesigner designer, FileAuthentication signAuth, bool lockDocument)
        {
            designer.Properties[PropertyNames.Operation].Value = Operation.Sign;

            switch (signAuth)
            {
            case FileAuthentication.CertificateFile:
                designer.Properties[PropertyNames.SignCertificateSource].Value       = CertificateSource.File;
                designer.Properties[PropertyNames.SignCertificateFilePath].Value     = this.authenticationManager.CertificateFilePath;
                designer.Properties[PropertyNames.SignCertificateFilePassword].Value = this.authenticationManager.CertificateFilePassword;
                break;

            case FileAuthentication.CertificateStore:
                designer.Properties[PropertyNames.SignCertificateSource].Value = CertificateSource.Store;
                designer.Properties[PropertyNames.SignCertificate].Value       = this.authenticationManager.StoredCertificate;
                break;

            default:
                throw new NotSupportedException();
            }

            designer.Properties[PropertyNames.SignSignedAt].Value         = signLocation;
            designer.Properties[PropertyNames.SignReason].Value           = signReason;
            designer.Properties[PropertyNames.SignLockAfterSigning].Value = lockDocument;
        }
Beispiel #7
0
 private void AssertOutputFile(string outputFilePath, FileAuthentication outputAuth, string expectedFilePath, int expectedNumberOfPages, string expectedText)
 {
     Assert.IsTrue(File.Exists(outputFilePath));
     Assert.AreEqual(expectedFilePath, outputFilePath);
     PdfComparer.AssertPageCount(outputFilePath, outputAuth, this.authenticationManager, expectedNumberOfPages);
     PdfComparer.AssertText(outputFilePath, outputAuth, this.authenticationManager, expectedText, null);
 }
Beispiel #8
0
        public static void AssertProtection(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager,
                                            bool expectedIsEncrypted,
                                            bool expectedDontEncryptMetadata,
                                            bool expectedAllowAssembly          = false,
                                            bool expectedAllowCopy              = false,
                                            bool expectedAllowDegradedPrinting  = false,
                                            bool expectedAllowFillIn            = false,
                                            bool expectedAllowModifyAnnotations = false,
                                            bool expectedAllowModifyContents    = false,
                                            bool expectedAllowPrinting          = false,
                                            bool expectedAllowScreenReaders     = false)
        {
            using (var comparer = new PdfComparer(filePath, fileAuth, authenticationManager))
            {
                Assert.AreEqual(expectedIsEncrypted, comparer.pdfReader.IsEncrypted());
                Assert.AreEqual(expectedDontEncryptMetadata, !comparer.pdfReader.IsMetadataEncrypted());

                if (expectedAllowAssembly && expectedAllowCopy && expectedAllowDegradedPrinting && expectedAllowFillIn &&
                    expectedAllowModifyAnnotations && expectedAllowModifyContents && expectedAllowPrinting && expectedAllowScreenReaders)
                {
                    Assert.IsTrue(comparer.pdfReader.IsOpenedWithFullPermissions);
                }
                else
                {
                    Assert.AreEqual(expectedAllowAssembly, PdfEncryptor.IsAssemblyAllowed((int)comparer.pdfReader.Permissions));
                    Assert.AreEqual(expectedAllowCopy, PdfEncryptor.IsCopyAllowed((int)comparer.pdfReader.Permissions));
                    Assert.AreEqual(expectedAllowDegradedPrinting, PdfEncryptor.IsDegradedPrintingAllowed((int)comparer.pdfReader.Permissions));
                    Assert.AreEqual(expectedAllowFillIn, PdfEncryptor.IsFillInAllowed((int)comparer.pdfReader.Permissions));
                    Assert.AreEqual(expectedAllowModifyAnnotations, PdfEncryptor.IsModifyAnnotationsAllowed((int)comparer.pdfReader.Permissions));
                    Assert.AreEqual(expectedAllowModifyContents, PdfEncryptor.IsModifyContentsAllowed((int)comparer.pdfReader.Permissions));
                    Assert.AreEqual(expectedAllowPrinting, PdfEncryptor.IsPrintingAllowed((int)comparer.pdfReader.Permissions));
                    Assert.AreEqual(expectedAllowScreenReaders, PdfEncryptor.IsScreenReadersAllowed((int)comparer.pdfReader.Permissions));
                }
            }
        }
Beispiel #9
0
        public void AddWatermarkWithAuthentication(
            [Values(
                 FileAuthentication.None,
                 FileAuthentication.Password,
                 FileAuthentication.CertificateFile,
                 FileAuthentication.CertificateStore)] FileAuthentication watermarkAuth)
        {
            string inputFilePath     = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.AddWatermark.Resources.Watermark.pdf", this.inputDirectory);
            string watermarkFilePath = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.AddWatermark.Resources.Overlay.pdf", this.inputDirectory);
            string outputFilePath    = Path.Combine(this.outputDirectory, "Watermark.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <AddWatermarkProvider>();

            ConfigureInputFileFunctionValues(designer, FileAuthentication.None, inputFilePath);
            var watermarkPages = string.Empty;

            ConfigureWatermarkFunctionValues(designer, watermarkAuth, watermarkFilePath, WatermarkPosition.Below, watermarkPages);
            designer.Properties[Pdf.Common.PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <AddWatermarkProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            PdfComparer.AssertText(outputFilePath, FileAuthentication.None, this.authenticationManager, "1\nWatermark\r\n2\nWatermark\r\n3\nWatermark\r\n4\nWatermark", null);
        }
Beispiel #10
0
 public static void AssertPageCount(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager,
                                    int expectedNumberOfPages)
 {
     using (var comparer = new PdfComparer(filePath, fileAuth, authenticationManager))
     {
         Assert.AreEqual(expectedNumberOfPages, comparer.pdfReader.NumberOfPages);
     }
 }
Beispiel #11
0
        public void ProtectWithPrintRestrictions(
            [Values(
                 FileAuthentication.Password /*,
                                              * Ignore: http://stackoverflow.com/questions/40045745/itextsharp-object-reference-error-on-pdfstamper-for-certificate-protected-file
                                              * FileAuthentication.CertificateFile,
                                              * FileAuthentication.CertificateStore*/)] FileAuthentication protectAuth,
            [Values(
                 Printing.None,
                 Printing.LowResolution,
                 Printing.HighResolution)] Printing printing)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.ChangeProtection.Resources.Protect.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "Protect.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <ChangeProtectionProvider>();

            ConfigureInputFileFunctionValues(designer, FileAuthentication.None, inputFilePath);
            ConfigureProtectFunctionValues(designer, protectAuth, Encryption.AES256, true, true, printing: printing);
            designer.Properties[Pdf.Common.PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <ChangeProtectionProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            bool allowDegradedPrinting = false;
            bool allowPrinting         = false;

            switch (printing)
            {
            case Printing.None:
                break;

            case Printing.LowResolution:
                allowDegradedPrinting = true;
                break;

            case Printing.HighResolution:
                allowDegradedPrinting = true;
                allowPrinting         = true;
                break;

            default:
                throw new NotSupportedException("Invalid Printing specified.");
            }

            PdfComparer.AssertProtection(outputFilePath, protectAuth, this.authenticationManager, true, true,
                                         expectedAllowDegradedPrinting: allowDegradedPrinting,
                                         expectedAllowPrinting: allowPrinting);

            if (protectAuth == FileAuthentication.Password)
            {
                using (var permissionsAuthHelper = new AuthenticationManager(permissionsPassword))
                {
                    PdfComparer.AssertProtectionAllRights(outputFilePath, FileAuthentication.Password, permissionsAuthHelper, true, true);
                }
            }
        }
Beispiel #12
0
 public static void AssertFieldSignature(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager,
                                         string expectedFieldName, string expectedSignName, string expectedLocation, string expectedReason, bool expectedLockDocument)
 {
     using (var comparer = new PdfComparer(filePath, fileAuth, authenticationManager))
     {
         string signatureName = comparer.GetSignatureName();
         Assert.AreEqual(expectedFieldName, signatureName);
         comparer.AssertCertificationLevel(expectedLockDocument);
         comparer.AssertSignatureDetails(signatureName, expectedSignName, expectedLocation, expectedReason);
     }
 }
Beispiel #13
0
 public static void AssertPageSignature(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager,
                                        string expectedSignName, string expectedLocation, string expectedReason, bool expectedLockDocument,
                                        int expectedPage, float expectedLeft, float expectedTop, float expectedWidth, float expectedHeight)
 {
     using (var comparer = new PdfComparer(filePath, fileAuth, authenticationManager))
     {
         string signatureName = comparer.GetSignatureName();
         comparer.AssertCertificationLevel(expectedLockDocument);
         comparer.AssertSignatureDetails(signatureName, expectedSignName, expectedLocation, expectedReason);
         comparer.AssertSignaturePosition(signatureName, expectedPage, expectedLeft, expectedTop, expectedWidth, expectedHeight);
     }
 }
Beispiel #14
0
		private void ConfigureInputFileFunctionValues(FunctionDesigner designer, FileAuthentication inputAuth, string inputFilePath)
		{
			ConfigureInputFileFunctionValues(
				designer,
				inputAuth,
				inputFilePath,
				PropertyNames.PdfFilePath,
				PropertyNames.AuthenticationType,
				PropertyNames.PdfPassword,
				PropertyNames.CertificateSource,
				PropertyNames.CertificateFilePath,
				PropertyNames.CertificateFilePassword,
				PropertyNames.Certificate);
		}
Beispiel #15
0
        public static void AssertText(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager,
                                      string expectedText, string expectedJavaScript)
        {
            using (var comparer = new PdfComparer(filePath, fileAuth, authenticationManager))
            {
                var pageText = new List <string>();
                for (int pageIndex = 1; pageIndex <= comparer.pdfReader.NumberOfPages; pageIndex++)
                {
                    pageText.Add(PdfTextExtractor.GetTextFromPage(comparer.pdfReader, pageIndex));
                }
                var pagesText = string.Join(Environment.NewLine, pageText);

                Assert.AreEqual(expectedText, pagesText);
                Assert.AreEqual(expectedJavaScript, comparer.pdfReader.JavaScript);
            }
        }
Beispiel #16
0
 public static void AssertFields(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager,
                                 IReadOnlyDictionary <string, string> expectedFieldValues)
 {
     using (var comparer = new PdfComparer(filePath, fileAuth, authenticationManager))
     {
         if (comparer.pdfReader.AcroFields.Xfa.XfaPresent)
         {
             Dictionary <string, string> fieldValues = comparer.pdfReader.AcroFields.Xfa.DatasetsSom.Name2Node.ToDictionary(field => field.Key, field => field.Value.InnerText);
             Assert.AreEqual(expectedFieldValues, fieldValues);
         }
         else
         {
             Dictionary <string, string> fieldValues = comparer.pdfReader.AcroFields.Fields.Keys.ToDictionary(key => key, key => comparer.pdfReader.AcroFields.GetField(key));
             Assert.AreEqual(expectedFieldValues, fieldValues);
         }
     }
 }
Beispiel #17
0
		public void ReadWithNoOutput(
			[Values(
				FileAuthentication.None,
				FileAuthentication.Password,
				FileAuthentication.CertificateFile,
				FileAuthentication.CertificateStore)] FileAuthentication inputAuth)
		{
			string inputFilePath = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.Read.Resources.Blank.pdf", this.inputDirectory);
			FunctionDesigner designer = ProviderHelpers.CreateDesigner<ReadProvider>();
			ConfigureInputFileFunctionValues(designer, inputAuth, inputFilePath);
			designer.Properties[PropertyNames.ReadText].Value = false;
			designer.Properties[PropertyNames.ReadFormData].Value = false;
			designer.Properties[PropertyNames.ReadSignature].Value = false;

			var tester = new FunctionTester<ReadProvider>();
			Assert.DoesNotThrow(() => tester.Execute(designer.GetProperties(), designer.GetParameters()));
		}
Beispiel #18
0
        public void Split(
            [Values(
                 FileAuthentication.None,
                 FileAuthentication.Password,
                 FileAuthentication.CertificateFile,
                 FileAuthentication.CertificateStore)] FileAuthentication inputAuth,
            [Values(
                 true,
                 false)] bool loopResults)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.PdfOperations.Resources.Split.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "Split.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <PdfOperationsProvider>();

            ConfigureInputFileFunctionValues(designer, inputAuth, inputFilePath);
            designer.Properties[PropertyNames.Operation].Value        = Operation.Split;
            designer.Properties[PropertyNames.SplitLoopResults].Value = loopResults;
            designer.Properties[PropertyNames.OutputFilePath].Value   = outputFilePath;

            var            tester = new FunctionTester <PdfOperationsProvider>();
            FunctionResult result = tester.Execute(designer.GetProperties(), designer.GetParameters());

            Assert.AreEqual(2, result.Value.NumberOfPages);
            if (loopResults)
            {
                Assert.AreEqual(2, result.ExecutionPathResult.Count());
                NextResult nextResult = result.ExecutionPathResult.ElementAt(0);
                Assert.AreEqual(ExecutionPathNames.PageFiles, nextResult.Name);
                AssertOutputFile(nextResult.Value, inputAuth, Path.Combine(this.outputDirectory, "Split_1.pdf"), 1, "1");

                nextResult = result.ExecutionPathResult.ElementAt(1);
                Assert.AreEqual(ExecutionPathNames.PageFiles, nextResult.Name);
                AssertOutputFile(nextResult.Value, inputAuth, Path.Combine(this.outputDirectory, "Split_2.pdf"), 1, "2");
            }
            else
            {
                Assert.AreEqual(2, result.Value.PageFiles.Count);
                string pageFile = result.Value.PageFiles[0];
                AssertOutputFile(pageFile, inputAuth, Path.Combine(this.outputDirectory, "Split_1.pdf"), 1, "1");

                pageFile = result.Value.PageFiles[1];
                AssertOutputFile(pageFile, inputAuth, Path.Combine(this.outputDirectory, "Split_2.pdf"), 1, "2");
            }
        }
Beispiel #19
0
        private void ConfigureWatermarkFunctionValues(FunctionDesigner designer, FileAuthentication watermarkAuth, string watermarkFilePath, WatermarkPosition position,
                                                      string watermarkPages)
        {
            designer.Properties[PropertyNames.Position].Value = position;
            designer.Properties[PropertyNames.Pages].Value    = watermarkPages;

            ConfigureInputFileFunctionValues(
                designer,
                watermarkAuth,
                watermarkFilePath,
                PropertyNames.FilePath,
                PropertyNames.AuthenticationType,
                PropertyNames.Password,
                PropertyNames.CertificateSource,
                PropertyNames.CertificateFilePath,
                PropertyNames.CertificateFilePassword,
                PropertyNames.Certificate);
        }
Beispiel #20
0
        protected void ConfigureInputFileFunctionValues(
            FunctionDesigner designer,
            FileAuthentication inputAuth,
            string inputFilePath,
            string inputFilePathPropertyName,
            string authenticationTypePropertyName,
            string inputPasswordPropertyName,
            string certificateSourcePropertyName,
            string certificateFilePathPropertyName,
            string certificateFilePasswordPropertyName,
            string certificatePropertyName)
        {
            this.authenticationManager.Protect(inputFilePath, inputAuth);
            designer.Properties[inputFilePathPropertyName].Value = inputFilePath;

            switch (inputAuth)
            {
            case FileAuthentication.None:
                designer.Properties[authenticationTypePropertyName].Value = AuthenticationType.None;
                break;

            case FileAuthentication.Password:
                designer.Properties[authenticationTypePropertyName].Value = AuthenticationType.Password;
                designer.Properties[inputPasswordPropertyName].Value      = this.authenticationManager.Password;
                break;

            case FileAuthentication.CertificateFile:
                designer.Properties[authenticationTypePropertyName].Value      = AuthenticationType.Certificate;
                designer.Properties[certificateSourcePropertyName].Value       = CertificateSource.File;
                designer.Properties[certificateFilePathPropertyName].Value     = this.authenticationManager.CertificateFilePath;
                designer.Properties[certificateFilePasswordPropertyName].Value = this.authenticationManager.Password;
                break;

            case FileAuthentication.CertificateStore:
                designer.Properties[authenticationTypePropertyName].Value = AuthenticationType.Certificate;
                designer.Properties[certificateSourcePropertyName].Value  = CertificateSource.Store;
                designer.Properties[certificatePropertyName].Value        = this.authenticationManager.StoredCertificate;
                break;

            default:
                throw new NotSupportedException("Invalid FileAuthentication specified.");
            }
        }
        public void FillFormXfa(
            [Values(
                 FileAuthentication.None,
                 FileAuthentication.Password /*,
                                              * Ignore: http://stackoverflow.com/questions/40045745/itextsharp-object-reference-error-on-pdfstamper-for-certificate-protected-file
                                              * FileAuthentication.CertificateFile,
                                              * FileAuthentication.CertificateStore*/)] FileAuthentication inputAuth)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.PdfOperations.Resources.FillFormXFA.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "FillXfa.pdf");

            var formData = new
            {
                form1_910_93_46FullName_910_93 = "John",
                form1_910_93_46Surname_910_93  = "Doe",
                form1_910_93_46Email_910_93    = "*****@*****.**",
                form1_910_93_46EmailMe_910_93  = true
            };

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <PdfOperationsProvider>();

            ConfigureInputFileFunctionValues(designer, inputAuth, inputFilePath);
            designer.Properties[PropertyNames.Operation].Value        = Operation.FillForm;
            designer.Properties[PropertyNames.FillFormFormData].Value = formData;
            designer.Properties[PropertyNames.OutputFilePath].Value   = outputFilePath;

            var tester = new FunctionTester <PdfOperationsProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            var formValues = new Dictionary <string, string>
            {
                { "form1[0].FullName[0]", formData.form1_910_93_46FullName_910_93 },
                { "form1[0].Surname[0]", formData.form1_910_93_46Surname_910_93 },
                { "form1[0].Email[0]", formData.form1_910_93_46Email_910_93 },
                { "form1[0].EmailMe[0]", (formData.form1_910_93_46EmailMe_910_93) ? "Yes" : "No" },
                { "form1[0]", "*****@*****.**" }
            };

            PdfComparer.AssertFields(outputFilePath, inputAuth, this.authenticationManager, formValues);
        }
Beispiel #22
0
        public void ProtectWithNoRestrictions(
            [Values(
                 FileAuthentication.Password,
                 FileAuthentication.CertificateFile,
                 FileAuthentication.CertificateStore)] FileAuthentication protectAuth)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.ChangeProtection.Resources.Protect.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "Protect.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <ChangeProtectionProvider>();

            ConfigureInputFileFunctionValues(designer, FileAuthentication.None, inputFilePath);
            ConfigureProtectFunctionValues(designer, protectAuth, Encryption.AES256, true);
            designer.Properties[Pdf.Common.PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <ChangeProtectionProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            PdfComparer.AssertProtectionAllRights(outputFilePath, protectAuth, this.authenticationManager, true, true);
        }
Beispiel #23
0
        private void ConfigureProtectFunctionValues(FunctionDesigner designer, FileAuthentication protectAuth, Encryption encryption, bool dontEncryptMetadata,
                                                    bool addDocumentRestrictions = false, Printing printing = Printing.None, Changes changes = Changes.None, bool allowCopy = false, bool allowScreenReaders = false)
        {
            designer.Properties[PropertyNames.Operation].Value = Operation.Protect;

            switch (protectAuth)
            {
            case FileAuthentication.None:
                designer.Properties[PropertyNames.ProtectProtection].Value = AuthenticationType.None;
                return;

            case FileAuthentication.Password:
                designer.Properties[PropertyNames.ProtectProtection].Value           = AuthenticationType.Password;
                designer.Properties[PropertyNames.ProtectDocumentOpenPassword].Value = this.authenticationManager.Password;
                designer.Properties[PropertyNames.ProtectPermissionsPassword].Value  = permissionsPassword;
                break;

            case FileAuthentication.CertificateFile:
                designer.Properties[PropertyNames.ProtectProtection].Value              = AuthenticationType.Certificate;
                designer.Properties[PropertyNames.ProtectCertificateSource].Value       = CertificateSource.File;
                designer.Properties[PropertyNames.ProtectCertificateFilePath].Value     = this.authenticationManager.CertificateFilePath;
                designer.Properties[PropertyNames.ProtectCertificateFilePassword].Value = this.authenticationManager.CertificateFilePassword;
                break;

            case FileAuthentication.CertificateStore:
                designer.Properties[PropertyNames.ProtectProtection].Value        = AuthenticationType.Certificate;
                designer.Properties[PropertyNames.ProtectCertificateSource].Value = CertificateSource.Store;
                designer.Properties[PropertyNames.ProtectCertificate].Value       = this.authenticationManager.StoredCertificate;
                break;
            }

            designer.Properties[PropertyNames.ProtectEncryption].Value          = encryption;
            designer.Properties[PropertyNames.ProtectDontEncryptMetadata].Value = dontEncryptMetadata;

            designer.Properties[PropertyNames.ProtectAddDocumentRestrictions].Value = addDocumentRestrictions;
            designer.Properties[PropertyNames.ProtectAllowPrinting].Value           = printing;
            designer.Properties[PropertyNames.ProtectAllowChanges].Value            = changes;
            designer.Properties[PropertyNames.ProtectAllowCopying].Value            = allowCopy;
            designer.Properties[PropertyNames.ProtectAllowScreenReaders].Value      = allowScreenReaders;
        }
        public void FillFormAcro(
            [Values(
                 FileAuthentication.None,
                 FileAuthentication.Password /*,
                                              * Ignore: http://stackoverflow.com/questions/40045745/itextsharp-object-reference-error-on-pdfstamper-for-certificate-protected-file
                                              * FileAuthentication.CertificateFile,
                                              * FileAuthentication.CertificateStore*/)] FileAuthentication inputAuth)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.PdfOperations.Resources.FillForm.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "Fill.pdf");

            var formData = new
            {
                First_32Name = "Jane",
                Surname      = "Woods",
                Gender       = "Female",
                AcceptTCs    = true
            };

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <PdfOperationsProvider>();

            ConfigureInputFileFunctionValues(designer, inputAuth, inputFilePath);
            designer.Properties[PropertyNames.Operation].Value        = Operation.FillForm;
            designer.Properties[PropertyNames.FillFormFormData].Value = formData;
            designer.Properties[PropertyNames.OutputFilePath].Value   = outputFilePath;

            var tester = new FunctionTester <PdfOperationsProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            var formValues = new Dictionary <string, string>
            {
                { "First Name", formData.First_32Name },
                { "Surname", formData.Surname },
                { "Gender", formData.Gender },
                { "AcceptTCs", (formData.AcceptTCs)? "Yes" : "No" }
            };

            PdfComparer.AssertFields(outputFilePath, inputAuth, this.authenticationManager, formValues);
        }
Beispiel #25
0
        public void ProtectWithRemoveProtection(
            [Values(
                 FileAuthentication.None,
                 FileAuthentication.Password,
                 FileAuthentication.CertificateFile,
                 FileAuthentication.CertificateStore)] FileAuthentication inputAuth)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.PdfOperations.Resources.Protect.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "Protect.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <PdfOperationsProvider>();

            ConfigureInputFileFunctionValues(designer, inputAuth, inputFilePath);
            ConfigureProtectFunctionValues(designer, FileAuthentication.None, Encryption.AES128, false);
            designer.Properties[PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <PdfOperationsProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            PdfComparer.AssertText(outputFilePath, FileAuthentication.None, this.authenticationManager, "Text on page 1", null);
        }
Beispiel #26
0
        public void ProtectWithChangeRestrictions(
            [Values(
                 FileAuthentication.Password /*,
                                              * Ignore: http://stackoverflow.com/questions/40045745/itextsharp-object-reference-error-on-pdfstamper-for-certificate-protected-file
                                              * FileAuthentication.CertificateFile,
                                              * FileAuthentication.CertificateStore*/)] FileAuthentication protectAuth,
            [Values(
                 Changes.None,
                 Changes.Assembly,
                 Changes.FillIn,
                 Changes.AnnotateAndFillIn,
                 Changes.AnyExpectExtract)] Changes changes)
        {
            string inputFilePath  = ResourceHelpers.WriteResourceToFile("Twenty57.Linx.Components.Pdf.Tests.PdfOperations.Resources.Protect.pdf", this.inputDirectory);
            string outputFilePath = Path.Combine(this.outputDirectory, "Protect.pdf");

            FunctionDesigner designer = ProviderHelpers.CreateDesigner <PdfOperationsProvider>();

            ConfigureInputFileFunctionValues(designer, FileAuthentication.None, inputFilePath);
            ConfigureProtectFunctionValues(designer, protectAuth, Encryption.AES256, true, true, changes: changes);
            designer.Properties[PropertyNames.OutputFilePath].Value = outputFilePath;

            var tester = new FunctionTester <PdfOperationsProvider>();

            tester.Execute(designer.GetProperties(), designer.GetParameters());

            bool allowAssembly          = false;
            bool allowFillIn            = false;
            bool allowModifyAnnotations = false;
            bool allowModifyContents    = false;

            switch (changes)
            {
            case Changes.None:
                break;

            case Changes.Assembly:
                allowAssembly = true;
                break;

            case Changes.FillIn:
                allowFillIn = true;
                break;

            case Changes.AnnotateAndFillIn:
                allowModifyAnnotations = true;
                allowFillIn            = true;
                break;

            case Changes.AnyExpectExtract:
                allowModifyContents    = true;
                allowModifyAnnotations = true;
                allowFillIn            = true;
                break;

            default:
                throw new NotSupportedException("Invalid Changes specified.");
            }

            PdfComparer.AssertProtection(outputFilePath, protectAuth, this.authenticationManager, true, true,
                                         expectedAllowAssembly: allowAssembly,
                                         expectedAllowFillIn: allowFillIn,
                                         expectedAllowModifyAnnotations: allowModifyAnnotations,
                                         expectedAllowModifyContents: allowModifyContents);

            if (protectAuth == FileAuthentication.Password)
            {
                using (var permissionsAuthHelper = new AuthenticationManager(permissionsPassword))
                {
                    PdfComparer.AssertProtectionAllRights(outputFilePath, FileAuthentication.Password, permissionsAuthHelper, true, true);
                }
            }
        }
Beispiel #27
0
 private PdfComparer(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager)
 {
     this.pdfReader = authenticationManager.GetReader(filePath, fileAuth);
 }
Beispiel #28
0
 public static void AssertProtectionAllRights(string filePath, FileAuthentication fileAuth, AuthenticationManager authenticationManager,
                                              bool expectedIsEncrypted,
                                              bool expectedDontEncryptMetadata)
 {
     AssertProtection(filePath, fileAuth, authenticationManager, expectedIsEncrypted, expectedDontEncryptMetadata, true, true, true, true, true, true, true, true);
 }