Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0 .NET4
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:
 
            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 22-May-12 21:13pm
Edited 22-May-12 21:14pm
v2
Rate this: bad
good
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"  >
</iframe>
 
in .cs file add
iframe1.Attributes["src"] = filesNameWithPath;
 
second solution
 
in .cs file add
 
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();
  Permalink  
Comments
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
good
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);
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);
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 370
1 Sergey Alexandrovich Kryukov 205
2 Prakriti Goyal 177
3 jlopez788 134
4 _Amy 110
0 OriginalGriff 6,762
1 Sergey Alexandrovich Kryukov 5,539
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Sep 2013
Copyright © CodeProject, 1999-2014
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