Word to Html and Display as Image in asp.net...................................

Unknown | 6:29 AM |

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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
        <table>
       
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="fUpload" runat="server"/></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /></td>
            </tr>
        </table>
   <div>
  <p><asp:Image ID="Image1" runat="server" Visible="false" BorderStyle="Dotted"
    BorderWidth="1px" /></p>

    </div>
    </div>
    </form>
</body>
</html>

Default.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Threading;
using System.Drawing;
using System.Web;
using System.Net;
using System.Globalization;
using System.Web.Resources;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Linq;
using System.Windows.Forms;

public partial class _Default : System.Web.UI.Page
{
    protected Microsoft.Office.Interop.Word.ApplicationClass objWord = new ApplicationClass();  
    protected string strPathToUpload; //Path to upload files "Uploaded"
    protected string strPathToConvert; //Path to convert uploaded files and save
    object fltDocFormat = 10; //For filtered HTML Output
    protected object missing = System.Reflection.Missing.Value;
    protected object readOnly = false; //Open file in readOnly mode
    protected object isVisible = false;//The process has to be in invisible mode

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //Code to check if user has selected any file on the form
        if (!(fUpload.HasFile))
        {
         
        }
        else
        {
            try
            {
                //To check the file extension if it is word document or something else
                string strFileName = fUpload.FileName;          
                string[] strSep = fUpload.FileName.Split('.');
                int arrLength = strSep.Length - 1;
                string strExt = strSep[arrLength].ToString().ToUpper();
                //Save the uploaded file to the folder
                strPathToUpload = Server.MapPath("Uploaded");
                //Map-path to the folder where html to be saved
                strPathToConvert = Server.MapPath("WordToHtml");
                object FileName = strPathToUpload + "\\" + fUpload.FileName;
                object FileToSave = strPathToConvert + "\\" + fUpload.FileName + ".htm";
                if (strExt.ToUpper().Equals("DOC"))
                {
                    fUpload.SaveAs(strPathToUpload + "\\" + fUpload.FileName);
                 
                    //open the file internally in word. In the method all the parameters should be passed by object reference
                    objWord.Documents.Open(ref FileName, 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, ref missing, ref missing);
                    //Do the background activity
                    objWord.Visible = false;
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(ref FileToSave, ref fltDocFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                   
                }
                else
                {
                 
                }
                //Close/quit word
                objWord.Quit(ref missing, ref missing, ref missing);
                Thread.Sleep(1000);
                HtmlGenericControl thegen = new HtmlGenericControl();

                StreamReader rdr = null;
           
                rdr = new StreamReader(FileToSave.ToString());
             
                object content = rdr.ReadToEnd();
                rdr.Close();
             
                 string _bitmapPath = "~/Bitmaps/Thumb.jpg";
                // NOTE: ASP.NET must have write access to the Bitmaps folder
                //Bitmap bmp = WebsiteThumbnail.GetThumbnail(FileToSave.ToString(), 900, 600, 300, 200);
                 Bitmap bmp = WebsiteThumbnail.GetThumbnail(FileToSave.ToString(), 900, 2400, 900, 2400);
          bmp.Save(Server.MapPath(_bitmapPath), System.Drawing.Imaging.ImageFormat.Jpeg);
          Image1.ImageUrl = _bitmapPath;
          Image1.Visible = true;
             
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }      
    }

 
}

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....