Open and Save Word document in ASP.NET

8 Dec 20233 minutes to read

Syncfusion DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can open and save a Word document in ASP.NET Web Forms.

Steps to open and save Word document programmatically:

Step 1: Create a new ASP.NET Web application project.

Create ASP.NET Web application in Visual Studio

Step 2: Install the Syncfusion.DocIO.AspNet NuGet package as a reference to your project from NuGet.org.

Install Syncfusion.DocIO.AspNet NuGet package

NOTE

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your application to use our components.

Step 3: Add a new Web Form in your project. Right click on the project and select Add > New Item and add a Web Form from the list. Name it as MainPage.

Step 4: Add a new button in the MainPage.aspx as shown below.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" Text="Open and Save Document" OnClick="OnButtonClicked" />
    </div>
    </form>
</body>
</html>

Step 5. Include the following namespace in your MainPage.aspx.cs file.

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

Step 6: Include the below code snippets in the click event of the button in MainPage.aspx.cs, to open an existing Word document in ASP.NET.

//Open an existing Word document.
WordDocument document = new WordDocument("Input.docx");

Step 7: Add below code example to add a paragraph in the Word document.

//Access the section in a Word document.
IWSection section = document.Sections[0];
//Add a new paragraph to the section.
IWParagraph paragraph = section.AddParagraph();
paragraph.ParagraphFormat.FirstLineIndent = 36;
paragraph.BreakCharacterFormat.FontSize = 12f;
IWTextRange text = paragraph.AppendText("In 2000, Adventure Works Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the Adventure Works Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.");
text.CharacterFormat.FontSize = 12f;

Step 8: Add below code example to save the Word document in ASP.NET.

//Save the Word document and download as attachment.
document.Save("Sample.docx", FormatType.Docx, HttpContext.Current.Response, HttpContentDisposition.Attachment);

You can download a complete working sample from GitHub.

By executing the program, you will get the Word document as follows.

ASP.Net Web Open and save output Word document

Click here to explore the rich set of Syncfusion Word library (DocIO) features.