Merge files with API
Below is the sample code for merging files with API:
/// <summary> /// Simple merge sample. /// Takes the files found in the source folder and merge them into the file targetFile. /// </summary> /// <param name="sourceFolder">Source folder for the source files to be merged</param> /// <param name="targetFile">File name and path for the merged file</param> static void MergeFiles(string sourceFolder, string targetFile) { Console.WriteLine($"Merging documents in {sourceFolder}"); DocumentConverterServiceClient client = null; try { // Create the folder for the merged file based on the target file name string targetFolder = Path.GetDirectoryName(targetFile); if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } // ** Open the service and configure the bindings client = OpenService(ServiceURL); // Processing options ProcessingOptions processingOptions = new ProcessingOptions(); // ** Specify the minimum level of merge settings MergeSettings mergeSettings = new MergeSettings(); mergeSettings.BreakOnError = false; processingOptions.MergeSettings = mergeSettings; //** Set the absolute minimum open options // ** Get the files to merge string[] files = Directory.GetFiles(sourceFolder); List<SourceFile> sourceFiles = new List<SourceFile>(); foreach (string file in files) { OpenOptions openOptions = new OpenOptions(); openOptions.OriginalFileName = Path.GetFileName(file); openOptions.FileExtension = Path.GetExtension(file); // ** Set the absolute minimum conversion settings. ConversionSettings conversionSettings = new ConversionSettings(); conversionSettings.Fidelity = ConversionFidelities.Full; conversionSettings.Quality = ConversionQuality.OptimizeForPrint; // Minimum merge settings FileMergeSettings fileMergeSettings = new FileMergeSettings(); fileMergeSettings.TopLevelBookmark = openOptions.OriginalFileName; byte[] sourceFileContent = File.ReadAllBytes(file); SourceFile sourceFile = new SourceFile(); sourceFile.OpenOptions = openOptions; sourceFile.ConversionSettings = conversionSettings; sourceFile.MergeSettings = fileMergeSettings; sourceFile.File = sourceFileContent; sourceFiles.Add(sourceFile); } // ** Assign source files processingOptions.SourceFiles = sourceFiles.ToArray(); // ** Carry out the conversion. BatchResults results = client.ProcessBatch(processingOptions); // ** Read the results of the merged file. byte[] mergedFile = results.Results[0].File; using (FileStream fs = File.Create(targetFile)) { fs.Write(mergedFile, 0, mergedFile.Length); fs.Close(); } Console.WriteLine("File converted to " + targetFile); } catch (FaultException<WebServiceFaultException> ex) { Console.WriteLine($"FaultException occurred: ExceptionType: {ex.Detail.ExceptionType.ToString()}"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { if (client != null) { CloseService(client); } } }
The above code sample uses the OpenService
and CloseService
methods from DocumentConverterServiceClient
sample code:
/// <summary> /// Perform Smart 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 SmartRedaction(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 SmartRedactionSettings SmartRedactionSettings smartRedactionSettings = new SmartRedactionSettings(); smartRedactionSettings = new SmartRedactionSettings(); // Set what needs to be redacted smartRedactionSettings.RedactCreditCardNumbers = BooleanEnum.True; smartRedactionSettings.RedactEmailAddresses = BooleanEnum.True; smartRedactionSettings.RedactPhoneNumbers = BooleanEnum.True; // 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 conversion. byte[] result = client.SmartRedaction(sourceFile, openOptions, smartRedactionSettings); // ** 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 + "-redacted.pdf")); using (FileStream fs = File.Create(destinationFileName)) { fs.Write(result, 0, result.Length); fs.Close(); } Console.WriteLine("File converted to " + destinationFileName); } 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); } } }