Click here to Skip to main content
11,478,066 members (71,147 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0 .NET4
I have a requirement where I need to display a Word Document in a ASP .NET webform page. So far what I have done is this:

            object fileName = "RandomDocument.docx";
            object readOnly = true;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application oWordApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document oWordDoc = new Document();
                oWordApp.Visible = false;
                oWordDoc = oWordApp.Documents.Open(ref fileName,
                                  ref missing, ref readOnly,
                                  ref missing, ref missing, ref missing,
                                  ref missing, ref missing, ref missing,
                                  ref missing, ref missing, ref isVisible,
                                  ref missing, ref missing, ref missing);
            catch (Exception ex)
                oWordApp.Visible = true;
            object startPosition = 0;
            object endPosition = (object)oWordDoc.Characters.Count;
            Range range = oWordDoc.Range(ref startPosition, ref endPosition);
            string text = range.Text;

As you can see I have no problem at all opening this document in a Word window and getting the text from it as well.

But I need this document to display on a ASP .NET page. Now I'm able to get the text from the last part in the code block it does not comprise of the exact formatting done in the Word document.

I have searched a lot but could not find a proper answer. Any pointers as to whether it is possible to display a read only view of the document on the page? Thanks in advance.
Posted 22-May-12 22:13pm
Edited 22-May-12 22:14pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

there is two solution but we can't open word in read only. you can set word file password protected.
first soution
<iframe frameborder ="1" height ="200px" width ="600px" id ="iframe1" runat ="server" scrolling ="auto"  >

in .cs file add
iframe1.Attributes["src"] = filesNameWithPath;

second solution

in .cs file add

        Response.ContentType = "application/msword";
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + sFilename + "\"");
byte[]databyte = File.ReadAllBytes(strFilepath);
        MemoryStream ms = new MemoryStream();
        ms.Write(databyte, 0, databyte.Length);
        ms.Position = 0;
        ms.Capacity = (int)ms.Length;
        byte[] arrbyte = ms.GetBuffer();
Nithin Sundar at 23-May-12 8:09am
Thanks for a response. But the second solution is not what I'm looking for. It just opens up a File Dialog while I want the document viewed inside the browser.

The first one seems to be what I need but it shows an empty Iframe even though I gave the whole file path. Am I doing anything wrong in this?
Nithin Sundar at 28-May-12 1:38am
I have decided to use the word app itself the second way. Thanks!
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

string filename = @"C:/.../xx.docx";
//string filename2 = @"xx.docx";
object file = filename;
object nullobj = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
//object file = path;
//object nullobj = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
txtFileContent.Text = doc.Content.Text;//data.GetData(DataFormats.Text).ToString();
doc.Close(ref nullobj, ref nullobj, ref nullobj);
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Frankie-C 160
1 Sergey Alexandrovich Kryukov 149
2 Sascha Lefèvre 145
3 F-ES Sitecore 130
4 siddartha kopparapu 103
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,321
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,325

Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 16 Sep 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100