---
title: "Code sample for merging files with API"
canonical_url: "https://www.nutrient.io/guides/document-converter/document-converter-services/merge-or-combine/code-sample-merge-files/"
md_url: "https://www.nutrient.io/guides/document-converter/document-converter-services/merge-or-combine/code-sample-merge-files.md"
last_updated: "2026-06-09T10:25:14.372Z"
description: "Explore our code samples to learn how you can quickly merge PDF documents or combine different file formats into a single PDF using the C# code samples."
---

# Merge files with API

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

```csharp

        /// <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`](https://www.nutrient.io/guides/document-converter/document-converter-services/knowledge-base/open-and-close-documentconverterserviceclient/) sample code.
---

## Related pages

- [Merge PDF API](/guides/document-converter/document-converter-services/merge-or-combine.md)
- [Merge PDFs Using .NET Core](/guides/document-converter/document-converter-services/merge-or-combine/dotnet-core.md)
- [Merge PDFs using C#](/guides/document-converter/document-converter-services/merge-or-combine/csharp.md)
- [Java Merge PDF API](/guides/document-converter/document-converter-services/merge-or-combine/java.md)
- [Merge PDFs Using JavaScript](/guides/document-converter/document-converter-services/merge-or-combine/javascript.md)
- [Merge PDF Using PHP](/guides/document-converter/document-converter-services/merge-or-combine/php.md)

