DEV Community

Tilal Ahmad Sana
Tilal Ahmad Sana

Posted on • Originally published at blog.groupdocs.cloud

5 1

A REST API Solution to Merge Documents in .NET

In this tutorial, I will show you how to merge Word documents easily and accurately without losing the formatting and contents. I am using GroupDocs.Merger Cloud, REST API solution to merge and split a wide range of document formats on any platform, without installing any plugin or software. It supports all common file formats, like PDF, Microsoft Word documents, Excel spreadsheets, PowerPoint presentations, plain and formatted text, and a long list of supported document formats.

I will upload source documents to cloud storage from local disk and merge.

Here are the Steps to Merge Word Documents:

  • Sign up with groupdocs.cloud and get App SID and App Key to authenticate your rest API calls
  • Create a new project in Visual Studio
  • Install GroupDocs.Merger Cloud SDK for .NET NuGet Package
  • Use this code to merge multiple documents
var configuration = new GroupDocs.Merger.Cloud.Sdk.Client.Configuration(MyAppSid, MyAppKey);
var apiInstance_Document = new GroupDocs.Merger.Cloud.Sdk.Api.DocumentApi(configuration);
var apiInstance_File = new GroupDocs.Merger.Cloud.Sdk.Api.FileApi(configuration);

var pathToSourceFiles = @"C:/Temp/input/";
var remoteFolder = "Temp/";
var joinItem_list = new List<JoinItem>();
try
{

    DirectoryInfo dir = new DirectoryInfo(pathToSourceFiles);
    System.IO.FileInfo[] files = dir.GetFiles();
    foreach (System.IO.FileInfo file in files)
    {
        var request_upload = new GroupDocs.Merger.Cloud.Sdk.Model.Requests.UploadFileRequest(remoteFolder + file.Name, File.Open(file.FullName, FileMode.Open));
        var response_upload = apiInstance_File.UploadFile(request_upload);
        var item = new JoinItem
        {
            FileInfo = new GroupDocs.Merger.Cloud.Sdk.Model.FileInfo
            { FilePath = remoteFolder + file.Name }
        };
        joinItem_list.Add(item);
    }

    var options = new JoinOptions
    {
        JoinItems = joinItem_list,
        OutputPath = remoteFolder + "Merged_Document.docx"
    };

    var request = new JoinRequest(options);
    var response = apiInstance_Document.Join(request);

    Console.WriteLine("Output file path: " + response.Path);

}
catch (Exception e)
{
    Console.WriteLine("Exception while Merging Documents: " + e.Message);
}

That's it, Isn't that simple. Read for more details.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay