How to convert byte array response returned from backend to word document using Office.js?

Yatin Tripathi 106 Reputation points
2023-01-20T11:49:05.33+00:00

I am using compareDocuments function to compare two documents and returning compared document as a byte array as response to front end , I don't know how to convert this byte array and open it as word document on my desktop word after calling my API on taskpane Add-in without downloading the file to my system.

Below is my C# code for the API which I created: -

        [HttpPost]
        [Route("compare")]
        public async Task<object> compare(string original, string revised)
        {
            Word.Application wordApp = new Word.Application();
            wordApp.Visible = false;
            object wordTrue = (object)true;
            object wordFalse = (object)false;
            object fileToOpen = @original;
            object missing = Type.Missing;
            Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
                   ref missing, ref wordFalse, ref wordFalse, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref wordTrue, ref missing,
                   ref missing, ref missing, ref missing);

            object fileToOpen1 = @revised;
            Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
                   ref missing, ref wordFalse, ref wordFalse, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing);

            Word.Document doc = wordApp.CompareDocuments(doc1, doc2, Word.WdCompareDestination.wdCompareDestinationNew,
                                Word.WdGranularity.wdGranularityWordLevel,
                                true, true, true, true, true, true, true, true, true, true, "", true);

            doc1.Close(ref missing, ref missing, ref missing);
            doc2.Close(ref missing, ref missing, ref missing);

            // Hides both original and revised documents
            wordApp.ActiveWindow.ShowSourceDocuments = WdShowSourceDocuments.wdShowSourceDocumentsNone;

            wordApp.Visible = false;
            //doc.Activate();
            object filePath = @"C:\Users\yatin\OneDrive\Documents\compared_document.docx";
            doc.SaveAs(ref filePath);
            doc.Close();
            wordApp.Quit();

            byte[] byteArray;
            using (FileStream fs = new FileStream(filePath.ToString(), FileMode.Open, FileAccess.Read))
            {
                byteArray = new byte[fs.Length];
                fs.Read(byteArray, 0, (int)fs.Length);
            }

            
            // Return the byte array to the frontend
            return byteArray;

        }
Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
689 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,234 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,797 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
899 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,570 questions
0 comments No comments
{count} votes

Accepted answer
  1. Eugene Astafiev 891 Reputation points
    2023-01-23T12:52:58.4633333+00:00

    First of all, automating MS Word from any web server is not really a good idea.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    It seems you need to received the byte array and then save it as a file on the client side, see How to load Word document from byte array for more information. But I don't think any JS code can access the local filesystem for security reasons. The best what your application could do is to ask users to download the generated file. Or just open the file in Office365 editor prior uploading the generated file to the OneDrive server.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Eugene Astafiev 891 Reputation points
    2023-01-23T12:47:47.9833333+00:00

    First of all, automating MS Word from any web server is not really a good idea.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the [Considerations for server-side Automation of Office][1] article.

    It seems you need to received the byte array and then save it as a file on the client side, see [How to load Word document from byte array][2] for more information. But I don't think any JS code can access the local filesystem for security reasons. The best what your application could do is to ask users to download the generated file. Or just open the file in Office365 editor prior uploading the generated file to the OneDrive server.

    0 comments No comments

  2. Eugene Astafiev 891 Reputation points
    2023-01-23T12:48:24.87+00:00

    First of all, automating MS Word from any web server is not really a good idea.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    It seems you need to received the byte array and then save it as a file on the client side, see How to load Word document from byte array for more information. But I don't think any JS code can access the local filesystem for security reasons. The best what your application could do is to ask users to download the generated file. Or just open the file in Office365 editor prior uploading the generated file to the OneDrive server.

    0 comments No comments

  3. Eugene Astafiev 891 Reputation points
    2023-01-23T12:51:50.81+00:00

    First of all, automating MS Word from any web server is not really a good idea.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the [Considerations for server-side Automation of Office][1] article.

    It seems you need to receive the byte array and then save it as a file on the client side, see How to load Word document from byte array for more information. But I don't think any JS code can access the local filesystem for security reasons. The best what your application could do is to ask users to download the generated file. Or just open the file in Office365 editor prior uploading the generated file to the OneDrive server.

    0 comments No comments

  4. Eugene Astafiev 891 Reputation points
    2023-01-23T12:52:37.5833333+00:00

    First of all, automating MS Word from any web server is not really a good idea.

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

    It seems you need to receive the byte array and then save it as a file on the client side, see How to load Word document from byte array for more information. But I don't think any JS code can access the local filesystem for security reasons. The best what your application could do is to ask users to download the generated file. Or just open the file in Office365 editor prior uploading the generated file to the OneDrive server.

    0 comments No comments