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

Time:01-24

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. I want two show the compared doc on word Desktop.

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;

        }

I want to use this API with my frontend ADD-IN and show the compared document on the frontend device without downloading it, everytime I run my code the compared document opens up on server side ,so I tried converting the compared doc to byte array and send it as a response to frontend, but I cannot open the word doc on desktop in frontend. Is there any way we can solve this issue?

CodePudding user response:

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.

  • Related