/// <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.Close(); } }
/// <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(); } }
/// <summary> /// Configure the Bindings, endpoints and open the service using the specified address. /// </summary> /// <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; // ** Specify an identity (any identity) in order to get it past .net3.5 sp1 EndpointIdentity epi = EndpointIdentity.CreateUpnIdentity("unknown"); EndpointAddress epa = new EndpointAddress(new Uri(address), epi); client = new DocumentConverterServiceClient(binding, epa); client.Open(); return(client); } catch (Exception) { CloseService(client); throw; } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { // ** Get request body data. string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string FileName = data?.fileName; string FileContent = data?.fileContent; byte[] convFile = null; DocumentConverterServiceClient client = null; try { byte[] sourceFile = Convert.FromBase64String(FileContent); // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = FileName; openOptions.FileExtension = Path.GetExtension(FileName); // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); conversionSettings.Fidelity = ConversionFidelities.Full; conversionSettings.Quality = ConversionQuality.OptimizeForPrint; convFile = client.ConvertAsync(sourceFile, openOptions, conversionSettings).GetAwaiter().GetResult(); } catch (Exception ex) { return(new BadRequestObjectResult(new { message = ex.ToString() })); } finally { CloseService(client); } return((ActionResult) new OkObjectResult(new { processed_file_content = convFile, })); }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Delete any processed files from a previous run foreach (FileInfo f in new DirectoryInfo(".").GetFiles("*_ocr.pdf")) { f.Delete(); } // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { // ** If nothing is specified then read the first PDF file from the current folder. string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.pdf"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to OCR."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } byte[] sourceFile = File.ReadAllBytes(sourceFileName); // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); openOptions.FileExtension = Path.GetExtension(sourceFileName); // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); // ** OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCRSettings ocr = new OCRSettings(); ocr.Language = OCRLanguage.English.ToString(); ocr.Performance = OCRPerformance.Slow; ocr.WhiteList = string.Empty; ocr.BlackList = string.Empty; conversionSettings.OCRSettings = ocr; // ** OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR OCR // ** Carry out the conversion. Console.WriteLine("Processing file " + sourceFileName + "."); byte[] convFile = client.ProcessChanges(sourceFile, openOptions, conversionSettings); // ** Write the processed file back to the file system with a PDF extension. string destinationFileName = Path.GetFileNameWithoutExtension(sourceFileName) + "_ocr.pdf"; using (FileStream fs = File.Create(destinationFileName)) { fs.Write(convFile, 0, convFile.Length); fs.Close(); } Console.WriteLine("File written to " + destinationFileName); // ** Open the generated PDF file in a PDF Reader Console.WriteLine("Launching file in PDF Reader"); Process.Start(destinationFileName); } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } Console.ReadKey(); }
private void CloseService(DocumentConverterServiceClient client) { if (client != null && client.State == CommunicationState.Opened) client.Close(); }
//private void InsertSeedRecords(bool associated, bool primaryDoc, int consentTypeId, string fName, string lName, int pcid) //{ // System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath); // var conStr = config.AppSettings.Settings["DBConnection"].Value; // using (var sqlConnection = new SqlConnection(conStr)) // { // sqlConnection.Open(); // var cmdTreatment = new SqlCommand("insert into Physican(Associated,Primary_Doctor,ConsentTypeID,Fname,Lname,PCID) values('" + (associated == true ? 1 : 0) + ",'" + (primaryDoc == true ? 1 : 0) + ",'" + consentTypeId + ",'" + fName + ",'" + lName + "'," + pcid + "," + "')", sqlConnection); // cmdTreatment.ExecuteNonQuery(); // } //} private DocumentConverterServiceClient OpenService(string address) { DocumentConverterServiceClient client = null; try { var binding = new BasicHttpBinding(); // ** Use standard Windows Authentication binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; // ** Allow long running conversions binding.SendTimeout = TimeSpan.FromMinutes(30); binding.ReceiveTimeout = TimeSpan.FromMinutes(30); // ** Allow file sizes of 50MB binding.MaxReceivedMessageSize = 50 * 1024 * 1024; binding.ReaderQuotas.MaxArrayLength = 50 * 1024 * 1024; binding.ReaderQuotas.MaxStringContentLength = 50 * 1024 * 1024; // ** We need to specify an identity (any identity) in order to get it past .net3.5 sp1 EndpointIdentity epi = EndpointIdentity.CreateUpnIdentity("unknown"); var epa = new EndpointAddress(new Uri(address), epi); client = new DocumentConverterServiceClient(binding, epa); client.Open(); return client; } catch (Exception) { CloseService(client); throw; } }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { //** Delete any split files from a previous test run. foreach (string file in Directory.GetFiles(Directory.GetCurrentDirectory(), "spf-*.pdf")) { File.Delete(file); } // ** If nothing is specified then read the first PDF file from the current folder. string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.pdf"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to split."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } byte[] sourceFile = File.ReadAllBytes(sourceFileName); // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); openOptions.FileExtension = "pdf"; // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); // ** Create the ProcessingOptions for the splitting task. ProcessingOptions processingOptions = new ProcessingOptions() { MergeSettings = null, SplitOptions = new FileSplitOptions() { FileNameTemplate = "spf-{0:D3}", FileSplitType = FileSplitType.ByNumberOfPages, BatchSize = 5, BookmarkLevel = 0 }, SourceFiles = new SourceFile[1] { new SourceFile() { MergeSettings = null, OpenOptions = openOptions, ConversionSettings = conversionSettings, File = sourceFile } } }; // ** Carry out the splittng. Console.WriteLine("Splitting file " + sourceFileName); BatchResults batchResults = client.ProcessBatch(processingOptions); // ** Process the returned files foreach (BatchResult result in batchResults.Results) { Console.WriteLine("Writing split file " + result.FileName); File.WriteAllBytes(result.FileName, result.File); } Console.WriteLine("Finished."); } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } Console.ReadKey(); }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { // ** Only .docx, .xlsx or .pptx filea are accepted string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.docx"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to convert and watermark."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } byte[] sourceFile = File.ReadAllBytes(sourceFileName); // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); openOptions.FileExtension = Path.GetExtension(sourceFileName); // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); // ** Format must be the same as the input format. // ** Resolve by parsing the extension into OtputFormat enum. // ** Leading dot (.) must be removed to make the parsing work. string format = Path.GetExtension(sourceFileName).TrimStart(new char[] { '.' }); conversionSettings.Format = (OutputFormat)Enum.Parse(typeof(OutputFormat), format, true); conversionSettings.Fidelity = ConversionFidelities.Full; conversionSettings.Quality = ConversionQuality.OptimizeForPrint; // ** Get the list of watermarks to apply. conversionSettings.Watermarks = CreateWatermarks(); // ** Carry out the watermarking. Console.WriteLine("Watermarking file " + sourceFileName); byte[] watermarkedFile = client.ApplyWatermark(sourceFile, openOptions, conversionSettings); // ** Write the watermarked file back to the file system with. string destinationFileName = Path.GetDirectoryName(sourceFileName) + @"\" + Path.GetFileNameWithoutExtension(sourceFileName) + "_Watermarked." + conversionSettings.Format; using (FileStream fs = File.Create(destinationFileName)) { fs.Write(watermarkedFile, 0, watermarkedFile.Length); fs.Close(); } Console.WriteLine("File watermarked to " + destinationFileName); // ** Open the generated file Console.WriteLine("Opening result document."); Process.Start(destinationFileName); } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } Console.ReadKey(); }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { // ** If nothing is specified then read the first PDF file from the current folder. string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.pdf"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to convert to PDF/A."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } byte[] sourceFile = File.ReadAllBytes(sourceFileName); // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); openOptions.FileExtension = "pdf"; // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); // ** Set output to PDF/A conversionSettings.PDFProfile = PDFProfile.PDF_A2B; // ** Specify output settings as we want to force post processing of files. OutputFormatSpecificSettings_PDF osf = new OutputFormatSpecificSettings_PDF(); osf.PostProcessFile = true; // ** We need to specify ALL values of an object, so use these for PDF/A osf.FastWebView = false; osf.EmbedAllFonts = true; osf.SubsetFonts = false; conversionSettings.OutputFormatSpecificSettings = osf; // ** Carry out the conversion. Console.WriteLine("Converting file " + sourceFileName + " to PDF/A."); byte[] convFile = client.ProcessChangesAsync(sourceFile, openOptions, conversionSettings).GetAwaiter().GetResult(); // ** Write the converted file back to the file system with a PDF extension. string destinationFileName = Path.GetFileNameWithoutExtension(sourceFileName) + "_PDFA.pdf"; using (FileStream fs = File.Create(destinationFileName)) { fs.Write(convFile, 0, convFile.Length); fs.Close(); } Console.WriteLine("File converted to " + destinationFileName); } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.doc*"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to convert and watermark."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } byte[] sourceFile = File.ReadAllBytes(sourceFileName); // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); openOptions.FileExtension = Path.GetExtension(sourceFileName); // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); conversionSettings.Fidelity = ConversionFidelities.Full; conversionSettings.Quality = ConversionQuality.OptimizeForPrint; // ** Get the list of watermarks to apply. conversionSettings.Watermarks = CreateWatermarks(); // ** Carry out the conversion. Console.WriteLine("Converting file " + sourceFileName); byte[] convFile = client.ConvertAsync(sourceFile, openOptions, conversionSettings).GetAwaiter().GetResult(); // ** Write the converted file back to the file system with a PDF extension. string destinationFileName = Path.Combine(Path.GetDirectoryName(sourceFileName), Path.GetFileNameWithoutExtension(sourceFileName) + "." + conversionSettings.Format); using (FileStream fs = File.Create(destinationFileName)) { fs.Write(convFile, 0, convFile.Length); fs.Close(); } Console.WriteLine("File converted to " + destinationFileName); } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { // ** If nothing is specified then read the first PDF file from the current folder. string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.pdf"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to extract data from."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } byte[] sourceFile = File.ReadAllBytes(sourceFileName); // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); //** Set the absolute minimum open options OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(sourceFileName); openOptions.FileExtension = "pdf"; // ** Set the absolute minimum conversion settings // ** and set output format to XFDF ConversionSettings conversionSettings = new ConversionSettings(); conversionSettings.Fidelity = ConversionFidelities.Full; conversionSettings.Format = OutputFormat.XFDF; // ** Carry out the conversion. Console.WriteLine("Exporting data from file " + sourceFileName + " to XFDF."); byte[] convFile = client.ConvertAsync(sourceFile, openOptions, conversionSettings).GetAwaiter().GetResult(); // ** Write the converted file back to the file system with a PDF extension. string destinationFileName = Path.GetFileNameWithoutExtension(sourceFileName) + ".xfdf"; using (FileStream fs = File.Create(destinationFileName)) { fs.Write(convFile, 0, convFile.Length); fs.Close(); } Console.WriteLine("Data exported to " + destinationFileName); } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } }
static void Main(string[] args) { DocumentConverterServiceClient client = null; try { // ** Delete any processed files from a previous run foreach (FileInfo f in new DirectoryInfo(".").GetFiles("*_ocr.pdf")) { f.Delete(); } // ** Determine the source file and read it into a byte array. string sourceFileName = null; if (args.Length == 0) { // ** If nothing is specified then read the first PDF file from the current folder. string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.pdf"); if (sourceFiles.Length > 0) { sourceFileName = sourceFiles[0]; } else { Console.WriteLine("Please specify a document to OCR."); Console.ReadKey(); return; } } else { sourceFileName = args[0]; } // ** Open the service and configure the bindings client = OpenService(SERVICE_URL); // ** Specify the various OCR related settings. ProcessingOptions processingOptions = new ProcessingOptions() { // ** Set up array of source files. For OCR we can pass in only a single file at a time. SourceFiles = new SourceFile[] { new SourceFile() { // ** Set the binary content File = File.ReadAllBytes(sourceFileName), // ** Create absolute minimum OpenOptions OpenOptions = new OpenOptions() { OriginalFileName = Path.GetFileName(sourceFileName), FileExtension = Path.GetExtension(sourceFileName), } } }, // ** Define OCR settings OCRSettings = new OCRSettings() { // ** Select OCR engine, change this to 'Muhimbi' to use the default Muhimbi OCR engine. OCREngine = "PrimeOCR", // ** Set language, keep in mind that different OCR engines support different languages (e.g. in the Muhimbi OCR engine, use 'English', not 'English_UK' / 'English_US' Language = OCRLanguage.English_UK.ToString(), // ** Set the desired output, in this case we want the OCRed PDF and OCRed text to be returned separately. OutputType = OCROutputType.Text | OCROutputType.PDF, // ** Include PrimeOCR specific properties. These settings cannot be passed into the Muhimbi default OCR engine. OCREngineSpecificSettings = new OCREngineSpecificSettings_PrimeOCR() { // ** Predefined accuracy levels can be used as well as manually defined integer values. AccuracyLevel = (int)PrimeOCR_AccuracyLevel.Level6, // ** Set various other parameters AutoZone = PrimeOCR_AutoZone.NoAutoZone, Deskew = PrimeOCR_Deskew.On, ImageProcessingOptions = PrimeOCR_ImageProcessingOptions.Autorotate | PrimeOCR_ImageProcessingOptions.Deshade | PrimeOCR_ImageProcessingOptions.Despeck | PrimeOCR_ImageProcessingOptions.Smooth, LexicalChecking = PrimeOCR_LexicalChecking.Lexical, PageQuality = PrimeOCR_PageQuality.NormalQuality, PrintType = PrimeOCR_PrintType.Machine, ZoneContent = PrimeOCR_ZoneContent.NoRestrictions }, } }; // ** Carry out the operation. Console.WriteLine("Processing file " + sourceFileName + "."); BatchResults results = client.ProcessBatch(processingOptions); // ** Get results. Both textual and PDF OCRResult ocredText = results.Results[0].OCRResult; byte[] ocredPdfFile = results.Results[0].File; // ** Process textual output if (ocredText != null) { // ** Write the text into a txt file string destFileName = Path.GetFileNameWithoutExtension(sourceFileName) + "_ocr.txt"; File.WriteAllText(destFileName, ocredText.Text); Console.WriteLine("Text file written to " + destFileName); // ** Show the file Console.WriteLine("Launching text file in reader"); Process.Start(destFileName); } else { Console.WriteLine("No text file was produced."); } // ** Process the resulting PDF if (ocredPdfFile != null) { // ** Write the processed file back to the file system with a PDF extension. string destFileName = Path.GetFileNameWithoutExtension(sourceFileName) + "_ocr.pdf"; File.WriteAllBytes(destFileName, ocredPdfFile); Console.WriteLine("PDF file written to " + destFileName); // ** Open the generated PDF file in a PDF Reader Console.WriteLine("Launching file in PDF Reader"); Process.Start(destFileName); } else { Console.WriteLine("No PDF file was generated."); } } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } Console.ReadKey(); }