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
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
ReplyDeleteYes .doc's are open able in 2007 and 2010. But from Office 2007 Word Supports .docx as it's default format.
ReplyDeleteWhat should we do with Word 2003 doc file
ReplyDeleteIf 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