This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/document-converter/document-converter-services/merge-or-combine/code-sample-merge-files.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Code sample for merging files with API

Below is the sample code for merging files with the API:

/// <summary>
/// Simple merge sample.
/// Takes the files found in the source folder and merges them into the `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 code sample above uses the OpenService and CloseService methods from DocumentConverterServiceClient sample code.