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 { // ** 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.ProcessChangesAsync(sourceFile, openOptions, conversionSettings).GetAwaiter().GetResult(); // ** 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); } catch (FaultException <WebServiceFaultException> ex) { Console.WriteLine("FaultException occurred: ExceptionType: " + ex.Detail.ExceptionType.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { CloseService(client); } }