DEV Community

loading...

A REST API Solution to Merge Documents in .NET

tilalahmad profile image Tilal Ahmad Sana Originally published at blog.groupdocs.cloud ・2 min read

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.

Discussion (0)

pic
Editor guide