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.
Source Code:
| Upload CV | |
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