public async Task CreateRedactionsAsync_fails_with_a_useful_error_message_when_the_source_document_is_unusable()
        {
            PrizmDocServerClient prizmDocServer = Util.CreatePrizmDocServerClient();

            await UtilAssert.ThrowsExceptionWithMessageAsync <RestApiErrorException>(
                async() =>
            {
                await prizmDocServer.CreateRedactionsAsync("documents/corrupted-page-count.pdf", new[] { new RegexRedactionMatchRule("wat") });
            }, "The remote server encountered an error when trying to create redactions for the given document. There may be a problem with the document itself.");
        }
예제 #2
0
        public async Task When_work_file_does_not_exist()
        {
            PrizmDocServerClient prizmDocServer = Util.CreatePrizmDocServerClient();

            AffinitySession affinitySession = Util.RestClient.CreateAffinitySession();
            RemoteWorkFile  validWorkFile   = await affinitySession.UploadAsync("documents/confidential-contacts.pdf");

            RemoteWorkFile invalidWorkFile = new RemoteWorkFile(affinitySession, "non-existent-id", validWorkFile.AffinityToken, "pdf");

            await UtilAssert.ThrowsExceptionWithMessageAsync <RestApiErrorException>(
                async() =>
            {
                await prizmDocServer.CreateRedactionsAsync(invalidWorkFile, new[] { new RegexRedactionMatchRule("dummy rule") });
            }, "Could not use the given RemoteWorkFile as the source document: the work file resource could not be found on the remote server. It may have expired.");
        }
        public async Task Single_rule_with_invalid_FillColor()
        {
            PrizmDocServerClient prizmDocServer = Util.CreatePrizmDocServerClient();

            await UtilAssert.ThrowsExceptionWithMessageAsync <RestApiErrorException>(
                async() =>
            {
                var rules = new[]
                {
                    new RegexRedactionMatchRule(@"\d\d\d-\d\d-\d\d\d\d")
                    {
                        RedactWith = new RedactionCreationOptions()
                        {
                            FillColor = "Bluuuuue",
                        },
                    },
                };

                await prizmDocServer.CreateRedactionsAsync("documents/confidential-contacts.pdf", rules);
            }, "RedactionMatchRule has invalid RedactWith.FillColor for remote server: \"Bluuuuue\"");
        }
        public async Task Single_rule_with_invalid_BorderThickness()
        {
            PrizmDocServerClient prizmDocServer = Util.CreatePrizmDocServerClient();

            var rules = new[]
            {
                new RegexRedactionMatchRule(@"\d\d\d-\d\d-\d\d\d\d")
                {
                    RedactWith = new RedactionCreationOptions()
                    {
                        BorderThickness = 0,
                    },
                },
            };

            await UtilAssert.ThrowsExceptionWithMessageAsync <RestApiErrorException>(
                async() =>
            {
                await prizmDocServer.CreateRedactionsAsync("documents/confidential-contacts.pdf", rules);
            }, "RedactionMatchRule has invalid RedactWith.BorderThickness for remote server: 0. Remote server requires a value greater than zero.");
        }
        public async Task Multiple_rules_where_one_has_invalid_FillColor()
        {
            PrizmDocServerClient prizmDocServer = Util.CreatePrizmDocServerClient();

            var rules = new[]
            {
                new RegexRedactionMatchRule(@"\d\d\d-\d\d-\d\d\d\d"),
                new RegexRedactionMatchRule(@"\S+@acme\.com")
                {
                    RedactWith = new RedactionCreationOptions()
                    {
                        FillColor = "REDDD",
                    },
                },
            };

            await UtilAssert.ThrowsExceptionWithMessageAsync <RestApiErrorException>(
                async() =>
            {
                await prizmDocServer.CreateRedactionsAsync("documents/confidential-contacts.pdf", rules);
            }, "RedactionMatchRule at index 1 has invalid RedactWith.FillColor for remote server: \"REDDD\"");
        }
예제 #6
0
        private static async Task MainAsync()
        {
            // Delete any existing output file before we get started.
            File.Delete("redacted.pdf");

            var prizmDocServer = new PrizmDocServerClient(Environment.GetEnvironmentVariable("BASE_URL"), Environment.GetEnvironmentVariable("API_KEY"));

            // -----------------------------------------------------------------
            // Step 1: Create markup JSON containing definitions of the areas we
            //         want to redact.
            // -----------------------------------------------------------------

            // Define a rule which will create a redaction for any text in a
            // document which looks like a social security number (###-##-####),
            // and use the text "(b)(6)" in the center of the redaction
            // rectangle as the reason for redaction.
            var ssnRule = new RegexRedactionMatchRule(@"\d\d\d-\d\d-\d\d\d\d")
            {
                RedactWith = new RedactionCreationOptions()
                {
                    Reason = "(b)(6)",
                },
            };

            // Define a rule which will create a redaction for any text in a
            // document which looks like an email address (this is a very basic
            // regex, matching things like [email protected]) and use the
            // text "(b)(6)" in the center of the redaction rectangle as
            // the reason for redaction.
            var emailRule = new RegexRedactionMatchRule(@"\S+@\S+\.\S+")
            {
                RedactWith = new RedactionCreationOptions()
                {
                    Reason = "(b)(6)",
                },
            };

            // Define a rule which will create a redaction for all occurrences
            // of "Bruce Wayne" in a document, use the text "(b)(1)" in the
            // center of the redaction rectangle as the reason for redaction,
            // customize various colors used, and attach some arbitrary
            // key/value string data to all redaction definitions which are
            // created. This arbitrary data will be present in the output markup
            // JSON file.
            var bruceWayneRule = new RegexRedactionMatchRule(@"Bruce Wayne")
            {
                RedactWith = new RedactionCreationOptions()
                {
                    Reason          = "(b)(1)",
                    FontColor       = "#FDE311",
                    FillColor       = "#000080",
                    BorderColor     = "#000000",
                    BorderThickness = 2,

                    // This arbitrary data will simply be present in the generated markup JSON.
                    Data = new Dictionary <string, string>
                    {
                        { "arbitrary-key-1", "arbitrary-value-1" },
                        { "arbitrary-key-2", "arbitrary-value-2" },
                    },
                },
            };

            var rules = new[] { ssnRule, emailRule, bruceWayneRule };

            // Automatically create a markup.json file with redaction
            // definitions based upon regular expression rules for a given
            // document. Any text in the document which matches one of the regex
            // rules will have a redaction definition created for that portion
            // of the document. The output markup.json file with its redaction
            // definitions can later be burned into the document.
            RemoteWorkFile markupJson = await prizmDocServer.CreateRedactionsAsync("confidential-contacts.pdf", rules);

            // -----------------------------------------------------------------
            // Step 2: Burn the markup JSON into the original document,
            //         producing a new, redacted PDF.
            // -----------------------------------------------------------------
            RemoteWorkFile redactedPdf = await prizmDocServer.BurnMarkupAsync("confidential-contacts.pdf", markupJson);

            // Save the result to "redacted.pdf"
            await redactedPdf.SaveAsync("redacted.pdf");
        }
        public async Task Can_create_redactions()
        {
            // Arrange
            PrizmDocServerClient prizmDocServer = Util.CreatePrizmDocServerClient();

            var ssn = new RegexRedactionMatchRule(@"\d\d\d-\d\d-\d\d\d\d")
            {
                RedactWith = new RedactionCreationOptions()
                {
                    Reason = "(b)(6)",
                    Data   = new Dictionary <string, string>
                    {
                        { "rule", "SSN" },
                    },
                },
            };

            var email = new RegexRedactionMatchRule(@"\S+@\S+\.\S+")
            {
                RedactWith = new RedactionCreationOptions()
                {
                    Reason = "(b)(6)",
                    Data   = new Dictionary <string, string>
                    {
                        { "rule", "email" },
                    },
                },
            };

            var bruceWayne = new RegexRedactionMatchRule(@"Bruce Wayne")
            {
                RedactWith = new RedactionCreationOptions()
                {
                    Reason = "Not Batman",
                },
            };

            var rules = new[]
            {
                ssn,
                email,
                bruceWayne,
            };

            // Act
            RemoteWorkFile result = await prizmDocServer.CreateRedactionsAsync("documents/confidential-contacts.pdf", rules);

            // Assert: Verify the expected redactions were created for the test document
            JObject markup;

            using (var memoryStream = new MemoryStream())
            {
                await result.CopyToAsync(memoryStream);

                string markupJson = Encoding.ASCII.GetString(memoryStream.ToArray());
                markup = JObject.Parse(markupJson);
            }

            List <JToken> marks                     = markup["marks"].Children().ToList();
            List <JToken> redactions                = marks.Where(x => (string)x["type"] == "RectangleRedaction").ToList();
            List <JToken> firstPageRedactions       = redactions.Where(x => (int)x["pageNumber"] == 1).ToList();
            List <JToken> secondPageRedactions      = redactions.Where(x => (int)x["pageNumber"] == 2).ToList();
            List <JToken> firstPageSsnRedactions    = firstPageRedactions.Where(x => x["data"] != null && (string)x["data"]["rule"] == "SSN").ToList();
            List <JToken> secondPageSsnRedactions   = secondPageRedactions.Where(x => x["data"] != null && (string)x["data"]["rule"] == "SSN").ToList();
            List <JToken> firstPageEmailRedactions  = firstPageRedactions.Where(x => x["data"] != null && (string)x["data"]["rule"] == "email").ToList();
            List <JToken> secondPageEmailRedactions = secondPageRedactions.Where(x => x["data"] != null && (string)x["data"]["rule"] == "email").ToList();
            List <JToken> bruceWayneRedactions      = redactions.Where(x => (string)x["reason"] == "Not Batman").ToList();

            Assert.AreEqual(18, marks.Count);
            Assert.AreEqual(18, redactions.Count);
            Assert.AreEqual(13, firstPageRedactions.Count);
            Assert.AreEqual(5, secondPageRedactions.Count);
            Assert.AreEqual(6, firstPageSsnRedactions.Count);
            Assert.AreEqual(3, secondPageSsnRedactions.Count);
            Assert.AreEqual(6, firstPageEmailRedactions.Count);
            Assert.AreEqual(2, secondPageEmailRedactions.Count);
            Assert.AreEqual(1, bruceWayneRedactions.Count);
        }