Read document array of word bytes in browser

Asked

Viewed 322 times

4

I have a routine of upload and download on a website that is working. However, I cannot do view word type files, directly in the browser, I wish to view as pdf. What I’m doing is saving the document to disk and sending the byte array to itextsharp.

FileStream stream = new FileStream(@"c:\arquivo.doc", FileMode.Create);
//Escrevo arquivo no fluxo
stream.Write(doc.Arquivo, 0, doc.Arquivo.Length);
//Fecho fluxo pra finalmente salvar em disco
stream.Close();

new FileStreamResult(stream, "application/vnd.ms-word");

2 answers

1

Without using external components (such as Aspose.Words, which is paid), can be done as follows, using Word interoperability objects:

private Microsoft.Office.Interop.Word.ApplicationClass MSdoc;
object Unknown = Type.Missing;

private void word2PDF(object Source, object Target)
{   
    if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();

    try
    {
        MSdoc.Visible = false;
        MSdoc.Documents.Open(ref Source, ref Unknown,
             ref Unknown, ref Unknown, ref Unknown,
             ref Unknown, ref Unknown, ref Unknown,
             ref Unknown, ref Unknown, ref Unknown,
             ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
         MSdoc.Application.Visible = false;
          MSdoc.WindowState =   Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;

        object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

        MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                ref Unknown, ref Unknown, ref Unknown,
                ref Unknown, ref Unknown, ref Unknown,
                ref Unknown, ref Unknown, ref Unknown,
                ref Unknown, ref Unknown, ref Unknown,
               ref Unknown, ref Unknown);
      }
       catch (Exception e)
      {
        MessageBox.Show(e.Message);
       }
     finally
      {
        if (MSdoc != null)
        {
            MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
            //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
        }

        WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown);
    }
}

I took it from here.

The problem is that it requires Word installed on the server, which is not a good one for ASP.NET MVC applications.

I intend to improve this answer as soon as I get a good alternative at no cost that makes this conversion.

  • Truth install word is a problem, tried to find another solution and so far nothing.

  • puts, so far I haven’t found any open source solution for this problem. Someone has?

0

  • There is no other way to view without getting by plugin? I’m trying to chat to pdf and it doesn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.