Esempio n. 1
0
        /// <summary>
        /// Automatically create redaction definitions for a document and a given set of text-matching rules,
        /// producing a new markup JSON file that can be used in a subsequent operation to actually apply the
        /// redaction definitions to the document.
        /// </summary>
        /// <param name="sourceDocument">Source document the redactions should be created for.</param>
        /// <param name="rules">Rules defining what content in the document should have a redaction region created for it.</param>
        /// <returns><see cref="RemoteWorkFile"/> for the created markup JSON file.</returns>
        public async Task <RemoteWorkFile> CreateRedactionsAsync(RemoteWorkFile sourceDocument, IEnumerable <RedactionMatchRule> rules)
        {
            RedactionMatchRule[] rulesArray = rules.ToArray();

            // Make sure we use the existing affinity token, if defined.
            AffinitySession affinitySession = this.restClient.CreateAffinitySession(sourceDocument.AffinityToken);

            string json = this.BuildPostRedactionCreatorsRequestJson(sourceDocument, rules.ToArray());

            // Start the redaction creation process
            using (HttpResponseMessage response = await affinitySession.PostAsync("/v2/redactionCreators", new StringContent(json, Encoding.UTF8, "application/json")))
            {
                await this.ThrowIfPostRedactionCreatorsError(rulesArray, response);

                json = await response.Content.ReadAsStringAsync();
            }

            JObject process   = JObject.Parse(json);
            string  processId = (string)process["processId"];

            // Wait for the process to complete
            using (HttpResponseMessage response = await affinitySession.GetFinalProcessStatusAsync($"/v2/redactionCreators/{processId}"))
            {
                await this.ThrowIfGetRedactionCreatorsError(response);

                json = await response.Content.ReadAsStringAsync();
            }

            process = JObject.Parse(json);

            return(new RemoteWorkFile(affinitySession, (string)process["output"]["markupFileId"], affinitySession.AffinityToken, "json"));
        }
Esempio n. 2
0
        /// <summary>
        /// Burns a markup JSON file into a document, producing a new PDF.
        /// </summary>
        /// <param name="sourceDocument">Existing <see cref="RemoteWorkFile" />
        /// to use as the source document.</param>
        /// <param name="markupJson">Existing <see cref="RemoteWorkFile" />
        /// containing the markup JSON you want burned into the source
        /// document.</param>
        /// <returns>RemoteWorkFile for the new PDF.</returns>
        public async Task <RemoteWorkFile> BurnMarkupAsync(RemoteWorkFile sourceDocument, RemoteWorkFile markupJson)
        {
            // Make sure we use the existing affinity token, if defined.
            AffinitySession affinitySession = this.restClient.CreateAffinitySession(sourceDocument.AffinityToken);

            // Make sure markupJson has the same affinity as the sourceDocument
            markupJson = await markupJson.GetInstanceWithAffinity(affinitySession, sourceDocument.AffinityToken);

            string json = this.BuildPostMarkupBurnersRequestJson(sourceDocument, markupJson);

            // Start the redaction creation process
            using (HttpResponseMessage response = await affinitySession.PostAsync("/PCCIS/V1/MarkupBurner", new StringContent(json, Encoding.UTF8, "application/json")))
            {
                await this.ThrowIfPostMarkupBurnersError(response);

                json = await response.Content.ReadAsStringAsync();
            }

            JObject process   = JObject.Parse(json);
            string  processId = (string)process["processId"];

            // Wait for the process to complete
            using (HttpResponseMessage response = await affinitySession.GetFinalProcessStatusAsync($"/PCCIS/V1/MarkupBurner/{processId}"))
            {
                await this.ThrowIfGetMarkupBurnersError(response);

                json = await response.Content.ReadAsStringAsync();
            }

            process = JObject.Parse(json);

            return(new RemoteWorkFile(affinitySession, (string)process["output"]["documentFileId"], affinitySession.AffinityToken, "pdf"));
        }
Esempio n. 3
0
        private string BuildPostMarkupBurnersRequestJson(RemoteWorkFile sourceDocument, RemoteWorkFile markupJson)
        {
            // Build the input JSON
            using (var stringWriter = new StringWriter())
                using (JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter))
                {
                    // start req JSON
                    jsonWriter.WriteStartObject();

                    // start input object
                    jsonWriter.WritePropertyName("input");
                    jsonWriter.WriteStartObject();

                    jsonWriter.WritePropertyName("documentFileId");
                    jsonWriter.WriteValue(sourceDocument.FileId);

                    jsonWriter.WritePropertyName("markupFileId");
                    jsonWriter.WriteValue(markupJson.FileId);

                    // end input object
                    jsonWriter.WriteEndObject();

                    // end req JSON
                    jsonWriter.WriteEndObject();

                    string json = stringWriter.ToString();
                    return(json);
                }
        }
Esempio n. 4
0
        /// <summary>
        /// Upload a <see cref="Stream"/>, creating a new <see cref="RemoteWorkFile"/>.
        /// </summary>
        internal static async Task <RemoteWorkFile> UploadAsync(this AffinitySession affinitySession, Stream documentStream, string fileExtension = "txt", string affinityToken = null)
        {
            // Remove leading period on fileExtension if present.
            if (fileExtension != null && fileExtension.StartsWith("."))
            {
                fileExtension = fileExtension.Substring(1);
            }

            var req = new HttpRequestMessage(HttpMethod.Post, $"/PCCIS/V1/WorkFile?FileExtension={fileExtension}")
            {
                Content = new StreamContent(documentStream),
            };

            if (affinityToken != null)
            {
                req.Headers.Add("Accusoft-Affinity-Token", affinityToken);
            }

            string json;

            using (HttpResponseMessage response = await affinitySession.SendAsync(req))
            {
                await response.ThrowIfRestApiError();

                json = await response.Content.ReadAsStringAsync();
            }

            var info           = JsonConvert.DeserializeObject <PostWorkFileResponse>(json);
            var remoteWorkFile = new RemoteWorkFile(affinitySession, info.fileId, info.affinityToken, info.fileExtension);

            return(remoteWorkFile);
        }
Esempio n. 5
0
        /// <summary>
        /// Burns a markup JSON file into a document, producing a new PDF.
        /// </summary>
        /// <param name="sourceDocument">Existing <see cref="RemoteWorkFile" />
        /// to use as the source document.</param>
        /// <param name="localFilePathToMarkupJson">Path to a local markup.json
        /// file containing the markup you want burned in to the source
        /// document.</param>
        /// <returns>RemoteWorkFile for the new PDF.</returns>
        public async Task <RemoteWorkFile> BurnMarkupAsync(RemoteWorkFile sourceDocument, string localFilePathToMarkupJson)
        {
            AffinitySession affinitySession = this.restClient.CreateAffinitySession();

            RemoteWorkFile markupJson = await affinitySession.UploadAsync(localFilePathToMarkupJson, sourceDocument.AffinityToken);

            return(await this.BurnMarkupAsync(sourceDocument, markupJson));
        }
Esempio n. 6
0
        /// <summary>
        /// <para>
        /// Applies redactions in a markup JSON file to a document, producing a
        /// new redacted plain text file.
        /// </para>
        /// <para>
        /// Note that redaction options (like redaction reason, border
        /// color, fill color, etc.) are not used by PrizmDoc Server when
        /// redacting to plain text. Instead, PrizmDoc Server will simply
        /// indicate when a portion of a line was redacted with the string
        /// <c>"&lt;Text Redacted&gt;"</c>.
        /// </para>
        /// </summary>
        /// <param name="localFilePathToSourceDocument">Path to a local file to
        /// use as the source document.</param>
        /// <param name="markupJson">Existing <see cref="RemoteWorkFile" />
        /// containing the markup JSON you want burned into the source
        /// document.</param>
        /// <param name="outputLineEndingFormat">Line ending to use in the
        /// output plain text file, such as <c>"\n"</c> or
        /// <c>"\r\n"</c>.</param>
        /// <returns>RemoteWorkFile for the new plain text file.</returns>
        public async Task <RemoteWorkFile> RedactToPlainTextAsync(string localFilePathToSourceDocument, RemoteWorkFile markupJson, string outputLineEndingFormat)
        {
            AffinitySession affinitySession = this.restClient.CreateAffinitySession();

            RemoteWorkFile sourceDocument = await affinitySession.UploadAsync(localFilePathToSourceDocument, markupJson.AffinityToken);

            return(await this.RedactToPlainTextAsync(sourceDocument, markupJson, outputLineEndingFormat));
        }
Esempio n. 7
0
        /// <summary>
        /// Automatically create redaction definitions for a document and a given set of text-matching rules,
        /// producing a new markup JSON file that can be used in a subsequent operation to actually apply the
        /// redaction definitions to the document.
        /// </summary>
        /// <param name="localFilePath">Path to a local file to use as the source document for which redactions should be created.</param>
        /// <param name="rules">Rules defining what content in the document should have a redaction region created for it.</param>
        /// <returns><see cref="RemoteWorkFile"/> for the created markup JSON file.</returns>
        public async Task <RemoteWorkFile> CreateRedactionsAsync(string localFilePath, IEnumerable <RedactionMatchRule> rules)
        {
            AffinitySession affinitySession = this.restClient.CreateAffinitySession();

            RemoteWorkFile sourceDocument = await affinitySession.UploadAsync(localFilePath);

            return(await this.CreateRedactionsAsync(sourceDocument, rules));
        }
Esempio n. 8
0
        private string BuildPostPlainTextRedactorsRequestJson(RemoteWorkFile sourceDocument, RemoteWorkFile markupJson, string outputLineEndingFormat)
        {
            // Build the input JSON
            using (var stringWriter = new StringWriter())
                using (JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter))
                {
                    // start req JSON
                    jsonWriter.WriteStartObject();

                    // start input object
                    jsonWriter.WritePropertyName("input");
                    jsonWriter.WriteStartObject();

                    jsonWriter.WritePropertyName("source");
                    jsonWriter.WriteStartObject();
                    jsonWriter.WritePropertyName("fileId");
                    jsonWriter.WriteValue(sourceDocument.FileId);
                    jsonWriter.WriteEndObject();

                    jsonWriter.WritePropertyName("markup");
                    jsonWriter.WriteStartObject();
                    jsonWriter.WritePropertyName("fileId");
                    jsonWriter.WriteValue(markupJson.FileId);
                    jsonWriter.WriteEndObject();

                    jsonWriter.WritePropertyName("dest");
                    jsonWriter.WriteStartObject();
                    jsonWriter.WritePropertyName("lineEndings");
                    jsonWriter.WriteValue(outputLineEndingFormat);
                    jsonWriter.WriteEndObject();

                    // end input object
                    jsonWriter.WriteEndObject();

                    // end req JSON
                    jsonWriter.WriteEndObject();

                    string json = stringWriter.ToString();
                    return(json);
                }
        }
Esempio n. 9
0
        private string BuildPostRedactionCreatorsRequestJson(RemoteWorkFile sourceDocument, RedactionMatchRule[] rules)
        {
            // Build the input JSON
            using (var stringWriter = new StringWriter())
                using (JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter))
                {
                    // start req JSON
                    jsonWriter.WriteStartObject();

                    // start input object
                    jsonWriter.WritePropertyName("input");
                    jsonWriter.WriteStartObject();

                    // source
                    jsonWriter.WritePropertyName("source");
                    jsonWriter.WriteStartObject();
                    jsonWriter.WritePropertyName("fileId");
                    jsonWriter.WriteValue(sourceDocument.FileId);
                    jsonWriter.WriteEndObject();

                    // rules
                    jsonWriter.WritePropertyName("rules");
                    jsonWriter.WriteStartArray();
                    foreach (RedactionMatchRule rule in rules)
                    {
                        PrizmDocRestApiJsonSerializer.Instance.Serialize(jsonWriter, rule);
                    }

                    jsonWriter.WriteEndArray();

                    // end input object
                    jsonWriter.WriteEndObject();

                    // end req JSON
                    jsonWriter.WriteEndObject();

                    string json = stringWriter.ToString();
                    return(json);
                }
        }