PNG JPG BMP TIFF PDF
Aspose.PDF  for .NET

Merge PDF in .NET SDK

Merge two PDF files into one in C# Cloud API without the use of any software like Adobe PDF.

Get Started

How to Merge Multiple PDF Files Using C# Cloud API

In order to merge two PDF files we’ll use Aspose.PDF Cloud .NET SDK This Cloud SDK allows you to easily build cloud-based PDF creator, editor & converter apps in C#, ASP.NET, or other .NET languages for various cloud platforms. Open NuGet package manager, search for Aspose.PDF Cloud and install. You may also use the following command from the Package Manager Console.

Command


PM> Install-Package Aspose.Pdf-Cloud 

Steps for Merging PDF via Cloud .NET SDK

A basic merging pdfs programmatically with Aspose.PDF Cloud .NET SDK APIs can be done with just few lines of code.

  1. Create a new Configuration object with your Application Secret and Key
  2. Create an object to connect to the Cloud API
  3. Upload your document file
  4. Perform the merging
  5. Download the result
 

Merge Two PDF Files using C#


    public static async Task Merge()
    {
        const string localFolder = @"C:\Samples";
        const string storageTempFolder = "YourTempFolder";
        const string resultFileName = "output_merged.pdf";
        List<string> pdfFileNames = new List<string> {
            "sample.pdf", "PdfWithLayers.pdf", "PdfWithLinks.pdf"
        };

        // Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required). 
        var pdfApi = new PdfApi(AppSecret, AppSid);
    
        Aspose.Pdf.Cloud.Sdk.Model.MergeDocuments documetItems = new(new List<string>());
       
        foreach (var fileName in pdfFileNames)
        {
            using var file = File.OpenRead(Path.Combine(localFolder, fileName));
            await pdfApi.UploadFileAsync(Path.Combine(storageTempFolder, fileName), file);
            
            documetItems.List.Add(Path.Combine( storageTempFolder, fileName));
        }
      
        DocumentResponse response = await pdfApi.PutMergeDocumentsAsync(resultFileName, documetItems, folder: storageTempFolder);
    
        if (response == null)
            Console.WriteLine("MergeDocuments(): Unexpected error!");
        else if (response.Code < 200 || response.Code > 299)
            Console.WriteLine("MergeDocuments(): Failed to documents.");
        else
        {
            using Stream downloadStream = await pdfApi.DownloadFileAsync(Path.Combine(storageTempFolder, resultFileName));
            using FileStream localStream = File.Create(Path.Combine(localFolder, resultFileName));
            await downloadStream.CopyToAsync(localStream);
            Console.WriteLine("MergeDocuments(): documents successfully merged to '{0}' file.", resultFileName);
        }
    }