Code samples
Pattern redaction sample code
The sample code for pattern redaction uses the OpenService
and CloseService
methods from the Document Converter Service client sample code:
/// <summary> /// Configure the bindings and endpoints and open the service using the specified address. /// </summary> /// <param name="address">URL of endpoint</param> /// <returns>An instance of the Web Service.</returns> public static DocumentConverterServiceClient OpenService(string address) { DocumentConverterServiceClient client = null; try { BasicHttpBinding binding = new BasicHttpBinding(); // Use standard Windows Security. binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; // Increase the client timeout to deal with (very) long running requests. binding.SendTimeout = TimeSpan.FromMinutes(120); binding.ReceiveTimeout = TimeSpan.FromMinutes(120); // Set the maximum document size to 50MB. binding.MaxReceivedMessageSize = 50 * 1024 * 1024; binding.ReaderQuotas.MaxArrayLength = 50 * 1024 * 1024; binding.ReaderQuotas.MaxStringContentLength = 50 * 1024 * 1024; EndpointAddress epa = new EndpointAddress(new Uri(address)); client = new DocumentConverterServiceClient(binding, epa); client.OpenAsync(); return client; } catch (Exception) { CloseService(client); throw; } } /// <summary> /// Check if the client is open and then close it. /// </summary> /// <param name="client">The client to close.</param> public static void CloseService(DocumentConverterServiceClient client) { if (client != null && client.State == CommunicationState.Opened) client.CloseAsync().GetAwaiter().GetResult(); }
Pattern highlighting sample code
Pattern highlighting identifies specific patterns in a document and visually highlights them without removing the underlying content. This example shows how to implement pattern highlighting using the API:
/// <summary> /// Perform pattern redaction on the supplied file, writing the result into the target folder. /// </summary> /// <param name="ServiceURL">URL endpoint for the PDF Converter service.</param> /// <param name="sourceFileName">Source filename.</param> /// <param name="targetFolder">Target folder to receive the output file.</param> static void PatternRedaction(string ServiceURL, string sourceFileName, string targetFolder) { DocumentConverterServiceClient client = null; try { // Create minimum `OpenOptions` object. OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); // Create minimum `PatternHighlightSettings`. PatternHighlightSettings patternHighlightSettings = new PatternHighlightSettings(); // Set the highlight color. patternHighlightSettings.Red = 0; patternHighlightSettings.Green = 0; patternHighlightSettings.Blue = 255; patternHighlightSettings.Alpha = 255; patternHighlightSettings.Pattern = "\"374245455400126\""; // Create target folder if required. if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } // ** Read the source file into a byte array. byte[] sourceFile = File.ReadAllBytes(sourceFileName); // ** Open the service and configure the bindings. client = OpenService(ServiceURL); // ** Carry out the highlighting. byte[] result = client.PatternHighlight(sourceFile, openOptions, patternHighlightSettings); // ** Save the results. if (result != null) { if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } string filename = Path.GetFileNameWithoutExtension(sourceFileName); string destinationFileName = Path.GetFullPath(Path.Combine(targetFolder, filename + "-highlighted.pdf")); using (FileStream fs = File.Create(destinationFileName)) { fs.Write(result, 0, result.Length); fs.Close(); } Console.WriteLine("File converted to " + destinationFileName); // Open the destination file. ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = destinationFileName; psi.UseShellExecute = true; Process.Start(psi); } else { Console.WriteLine("Nothing returned"); } } catch (FaultException<WebServiceFaultException> ex) { Console.WriteLine($"FaultException occurred: ExceptionType: {ex.Detail.ExceptionType.ToString()}"); Console.WriteLine(); Console.WriteLine($"Error Detail: {string.Join(Environment.NewLine, ex.Detail.ExceptionDetails)}"); Console.WriteLine($"Error message: {ex.Message}"); Console.WriteLine(); Console.WriteLine($"Error reason: {ex.Reason}"); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Data.ToString()); } finally { if (client != null) { CloseService(client); } } }
Smart redaction sample code
The sample code for smart redaction uses the OpenService
and CloseService
methods from the Document Converter service client sample code:
/// <summary> /// Configure the bindings and endpoints and open the service using the specified address. /// </summary> /// <param name="address">URL of the endpoint.</param> /// <returns>An instance of the Web Service.</returns> public static DocumentConverterServiceClient OpenService(string address) { DocumentConverterServiceClient client = null; try { BasicHttpBinding binding = new BasicHttpBinding(); // Use standard Windows Security. binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; // Increase the client timeout to deal with (very) long running requests. binding.SendTimeout = TimeSpan.FromMinutes(120); binding.ReceiveTimeout = TimeSpan.FromMinutes(120); // Set the maximum document size to 50MB. binding.MaxReceivedMessageSize = 50 * 1024 * 1024; binding.ReaderQuotas.MaxArrayLength = 50 * 1024 * 1024; binding.ReaderQuotas.MaxStringContentLength = 50 * 1024 * 1024; EndpointAddress epa = new EndpointAddress(new Uri(address)); client = new DocumentConverterServiceClient(binding, epa); client.OpenAsync(); return client; } catch (Exception) { CloseService(client); throw; } } /// <summary> /// Check if the client is open and then close it. /// </summary> /// <param name="client">The client to close.</param> public static void CloseService(DocumentConverterServiceClient client) { if (client != null && client.State == CommunicationState.Opened) client.CloseAsync().GetAwaiter().GetResult(); }