Tuesday, September 27, 2011

Read, Edit and Create Microsoft Word documents in asp.net using docX


Microsoft word is mostly used in web applications. It is very easy to edit MS word documents on the server using docx.dll.
Docx is a .NET Library which allows word 2007 manipulation. You can download it from codeplex.

Create a web form containing a fileupload, Textbox and two buttons, one for upload and second for editing and saving the document. The code is below:
Source Code:
Upload CV
In the btnUpload event handler we write the code for saving the word file on the server and display it in the textbox with the help of docX.
Code behind:
protected void btnUpload_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile == true)
    {
        if ( System.IO.Path.GetExtension(FileUpload1.FileName)==".docx" )
        {
            FileUpload1.SaveAs(Server.MapPath("~\\CVs\\") + 
            FileUpload1.FileName);
            this.lblupload.Text = "CV successfully uploaded";                
            DocX doc = DocX.Load(Server.MapPath("~\\CVs\\") + FileUpload1.FileName);      
            \\ Created a Docx variable doc and using Docx.Load("word file") to load the word 2007 file                         
            txtWord.Text = doc.Text; 
            \\Set the text property of doc to textbox text property. Text property of doc returns all the text from the word document.               
       }
       else
       {
            Response.Write("");         
       }
    }
    else
    {
           Response.Write("");         
    }
} 
On the btnSave eventhandler we have deleted the previous word file and created a new word document and apply simple formatting and then put the content from textbox in the new word document and finally save it.
Code for saving edited data:
protected void btnSave_Click(object sender, EventArgs e)
{
    try
    {
         FileInfo file = new FileInfo(MapPath("~\\CVs\\") + 
         ViewState["CV"].ToString());                          
         if (file.Exists)
         {
             File.Delete(MapPath("~\\CVs\\") + ViewState["CV"].ToString());
             \\ Delete existing word file
         }
             DocX doc = DocX.Create(Server.MapPath("~\\CVs\\") +
             ViewState["CV"].ToString());
             \\ Create a new word document            
             Formatting f = new Formatting();
             f.FontFamily = new FontFamily("Tahoma");
             \\Defines font family
             doc.InsertParagraph(txtWord.Text,false,f);     
             doc.Save();            
   }
   catch(Exception)
   {
            
   }
}

With DocX it's very easy to play with word files but it supports only .docx format.
Any question or suggestion is appreciable.

Regards,
Waqar Farooq Janjua

4 comments:

  1. well not aplicable after 2007 mm.. ist .docs also openable by 2009 and 10? I think it is. So will it work for them too? Cause I think it will work for them but not for version older than 07

    ReplyDelete
  2. Yes .doc's are open able in 2007 and 2010. But from Office 2007 Word Supports .docx as it's default format.

    ReplyDelete
  3. What should we do with Word 2003 doc file

    ReplyDelete
    Replies
    1. If you are still using Office 2003 and wants to create, read, edit or even convert your document to other formats in .net/c# then i would recommend you to use this .NET Word Library , its not free but offers free trial and great support.

      Delete