Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello,
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:

C#
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();
try
{
    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);

    oWordDoc.Activate();
}
catch (Exception ex)
{
}
finally
{
    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
Updated 22-May-12 21:14pm
v2

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


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

second solution

in .cs file add

C#
Response.Clear();
        Response.ContentType = "application/msword";
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + sFilename + "\"");
        Response.Flush();
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();
        ms.Close();
        Response.BinaryWrite(arrbyte);
        Response.End();
 
Share this answer
 
Comments
Nithin Sundar 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 28-May-12 1:38am    
I have decided to use the word app itself the second way. Thanks!
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);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
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);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900