Pages

Wednesday, 3 October 2012

How to display Word data in iFrame on Webpage using C#



Steps:

1. Create a Web Page as below (Default.aspx):


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #iFrame
        {
            height: 689px;
            width: 773px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Button ID="btnword" runat="server" Text="Show Word"
            onclick="btnword_Click" />
        <br />
        <br />
    <iframe id="iFrame" runat="server"></iframe>
    </div>
    </form>
</body>
</html>

 
2. Add Reference in project:

3. Add below code in Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;

public partial class _Default : System.Web.UI.Page
{
    public string Message = string.Empty;           // To store the Error or Message
    private Microsoft.Office.Interop.Word.ApplicationClass Word;     // The Interop Object for Word
    object Unknown = Type.Missing;                   // For passing Empty values
    public enum StatusType { SUCCESS, FAILED };     // To Specify Success or Failure Types
    public StatusType Status;                       // To know the Current Status

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnword_Click(object sender, EventArgs e)
    {
        string strFilePath = Server.MapPath(@"ConvertedLocation\");
        string strFile = "JoinsSqlServer2005.doc";
        string[] File = strFile.Split('.');
        string strExtension = File[1].ToString();
        string strUrl = "http://" + Request.Url.Authority + "/WordinIFrame/ConvertedLocation/";

        string Filename = strFilePath + strFile.Split('.')[0] + ".html";

        if (System.IO.File.Exists(Filename))
        {
            System.IO.File.Delete(Filename);
        }

        ConvertHTMLFromWord(strFilePath + strFile, strFilePath + strFile.Split('.')[0] + ".html");

        iFrame.Attributes["src"] = strUrl + strFile.Split('.')[0] + ".html";

    }
    public void ConvertHTMLFromWord(object Source, object Target)
    {
        if (Word == null)                         // Check for the prior instance of the OfficeWord Object
            Word = new Microsoft.Office.Interop.Word.ApplicationClass();

        try
        {
            // To suppress window display the following code will help
            Word.Visible = false;
            Word.Application.Visible = false;
            Word.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;



            Word.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);

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

            Word.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);

            Status = StatusType.SUCCESS;
            Message = Status.ToString();
        }
        catch (Exception e)
        {
            Message = "Error :" + e.Message.ToString().Trim();
        }
        finally
        {
            if (Word != null)
            {
                Word.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                Word.Quit(ref Unknown, ref Unknown, ref Unknown);
            }
        }
    }

}
 

4.  Execute the code and click on "Show Word" Button. After clicking on button you will see word file in iFrame as below.

7 comments:

  1. Can we edit and save to the path again.

    ReplyDelete
  2. And What is "WordinIFrame" in URL??

    ReplyDelete
  3. this is unsupported by Microsoft as mentioned in
    https://support.microsoft.com/en-us/kb/257757

    ReplyDelete
  4. Images are not visible in the iframe , whereas it is visible in the original word document

    ReplyDelete
  5. Images are not visible in the iframe , whereas it is visible in the original word document

    ReplyDelete
  6. This method or property is not available because a document window is not active.

    ReplyDelete