public void Can_serialize_a_fully_detailed_instance() { var rule = new RegexRedactionMatchRule("My Pattern") { RedactWith = new RedactionCreationOptions() { Reason = "HIGHLY CONFIDENTIAL", FillColor = "#FF0000", BorderColor = "#00FF00", FontColor = "#0000FF", BorderThickness = 2, Data = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" }, }, }, }; using (var stringWriter = new StringWriter()) { PrizmDocRestApiJsonSerializer.Instance.Serialize(stringWriter, rule); Assert.AreEqual("{\"find\":{\"type\":\"regex\",\"pattern\":\"My Pattern\"},\"redactWith\":{\"type\":\"RectangleRedaction\",\"reason\":\"HIGHLY CONFIDENTIAL\",\"fontColor\":\"#0000FF\",\"fillColor\":\"#FF0000\",\"borderColor\":\"#00FF00\",\"borderThickness\":2,\"data\":{\"key1\":\"value1\",\"key2\":\"value2\"}}}", stringWriter.ToString()); } }
public void Can_serialize_a_minimal_instance() { var rule = new RegexRedactionMatchRule("My Pattern"); using (var stringWriter = new StringWriter()) { PrizmDocRestApiJsonSerializer.Instance.Serialize(stringWriter, rule); Assert.AreEqual("{\"find\":{\"type\":\"regex\",\"pattern\":\"My Pattern\"},\"redactWith\":{\"type\":\"RectangleRedaction\"}}", stringWriter.ToString()); } }
public void Serializes_nothing_when_pattern_is_null() { var rule = new RegexRedactionMatchRule(); using (var stringWriter = new StringWriter()) { PrizmDocRestApiJsonSerializer.Instance.Serialize(stringWriter, rule); Assert.AreEqual(string.Empty, stringWriter.ToString()); } }
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); }