Adding Text / Images to word document.


In this article we are going to see How we can add Text and Images to a word document. We are also going to modify the word document and save it back in the original location. The word document is placed in the WebApplication itself. So that the Application can easily locate it.

In this article we are going to see How we can add Text and Images to a word document. We are also going to modify the word document and save it back in the original location.

1.The first step to work with Word document in .NET is you need to add a COM reference to your website by right clicking in the solution explorer on References->Add Reference. Click on the COM tab and locate the Microsoft Word 12.0 Object Library. Click Select and OK.
Add Reference to ms word COM library
2.After adding reference, add this directive
using Microsoft.Office.Interop.Word;
3.Add a button to the Default.aspx page.
onclick="btnMyWordDoc_Click" />
4.Create a WordDocs folder to save the word documents in your web application and create Images folder for all the images in your web application. Refer the below directory structure.
Web Application Directory Structure
5. In the button click event handler "btnMyWordDoc_Click", Add the below code. This code adds some text to the word document. Adds an Image located in the Images folder of your website to the word document. Keeps the Word document open so that you can edit it and save it back in the same location.


//This code is to create or edit an existing word document. Add image to this document. Edit the word document
//and save it back in the same location.
protected void btnMyWordDoc_Click(object sender, EventArgs e)
{
//We are using this instance of the System.Reflection.Missing class to represent missing values in the SaveAs and AddPicture methods below.
object missing = System.Reflection.Missing.Value;

object Visible=true;

object start1 = 0;

object end1 = 0;

// instantiate a new instance of a Word application
ApplicationClass WordApp = new ApplicationClass();

Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

Range rng = adoc.Range(ref start1, ref missing);


try

{
//Use verdana font to write to word.
rng.Font.Name = "Verdana";
//Type following text into word doc.
rng.InsertAfter("This is my first word document.!");
//Word document is located in WordDocs folder in our Web Application.
object filename = Server.MapPath(@"WordDocs/FirstWordDoc.doc");
//SaveAs method accepts first argument of type object which is the filename,second argument is file format and so on.
adoc.SaveAs(ref filename, 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, ref missing);
//Opens the word document for editing. You can edit the word document and save it(By default it saves in the same location).
WordApp.Visible = true;

WordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;

//Add the Image located at "Images/Quote1.jpg" in the Images folder of our Web Application.
rng.InlineShapes.AddPicture(Server.MapPath(@"Images/Quote1.jpg"), ref missing, ref missing, ref missing);

}

catch (Exception ex)
{
//In case of any exception display it.
Response.Write(ex.Message);
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: