Loading & saving document

25 Jun 202617 minutes to read

Namespaces required

The following namespaces of Essential® DocIO need to be included in your application to load and save the Word document.

NOTE

Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.

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

Opening an existing document

You can open an existing Word document by using either the Open method or the constructor of WordDocument class

//Opens an existing document from stream through constructor of `WordDocument` class
FileStream fileStreamPath = new FileStream(@"Data/Hello World.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic);
//Opens an existing document from file system through constructor of WordDocument class
WordDocument document = new WordDocument(fileName);
'Opens an existing document from file system through constructor of WordDocument class
Dim document As New WordDocument(fileName)
//Opens an existing document from stream through constructor of `WordDocument` class
FileStream fileStreamPath = new FileStream(@"Data/Hello World.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Creates an empty Word document instance
WordDocument document = new WordDocument();
//Loads or opens an existing word document through Open method of WordDocument class
document.Open(fileStreamPath, FormatType.Automatic);
//Creates an empty Word document instance
WordDocument document = new WordDocument();
//Loads or opens an existing word document through Open method of WordDocument class
document.Open(fileName);
'Creates an empty Word document instance
Dim document As New WordDocument()
'Loads or opens an existing word document through Open method of WordDocument class
document.Open(fileName)

You can download a complete working sample from GitHub.

Opening an existing document from Stream

You can open an existing document from stream by using either the overloads of Open methods or the constructor of WordDocument class

//Opens an existing document from stream through constructor of `WordDocument` class
FileStream fileStreamPath = new FileStream(@"Data/Hello World.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Opens an existing document from stream through constructor of WordDocument class
WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic);
//Opens an existing document from stream through constructor of WordDocument class
WordDocument document = new WordDocument(wordDocumentStream, FormatType.Automatic);
'Opens an existing document from stream through constructor of WordDocument class
Dim document As New WordDocument(wordDocumentStream, FormatType.Automatic)
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
    //Loads or opens an existing Word document from stream
    FileStream fileStreamPath = new FileStream(@"Data/Hello World.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    //Loads or opens an existing Word document through Open method of WordDocument class 
    document.Open(fileStreamPath, FormatType.Automatic);
}
//Creates an empty WordDocument instance
WordDocument document = new WordDocument();
//Loads or opens an existing Word document through Open method of WordDocument class
document.Open(wordDocumentStream, FormatType.Automatic);
'Creates an empty WordDocument instance
Dim document As New WordDocument()
'Loads or opens an existing word document through Open method of WordDocument class
document.Open(wordDocumentStream, FormatType.Automatic)

You can download a complete working sample from Stream from GitHub.

Opening an Encrypted Word document

You can open an existing encrypted Word document from either the file system or the stream by using the following overloads as shown.

//Open an existing document from stream through constructor of WordDocument class.
FileStream fileStreamPath = new FileStream(@"Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an encrypted Word document.
WordDocument document = new WordDocument(fileStreamPath, "password");
//Opens an existing encrypted document through constructor of WordDocument class
WordDocument document = new WordDocument(fileName, FormatType.Automatic, "password");
'Opens an existing encrypted document through constructor of WordDocument class
Dim document As New WordDocument(fileName, FormatType.Automatic, "password")
//Open an existing document from stream through constructor of WordDocument class.
FileStream fileStreamPath = new FileStream(@"Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an encrypted Word document.
WordDocument document = new WordDocument(fileStreamPath, "password");
//Creates an empty Word document instance
WordDocument document = new WordDocument();
//Loads or opens an existing encrypted Word document through Open method of WordDocument class
document.Open(wordDocumentStream, FormatType.Automatic, "password");
'Creates an empty Word document instance
Dim document As New WordDocument()
'Loads or opens an existing encrypted Word document through Open method of WordDocument class
document.Open(wordDocumentStream, FormatType.Automatic, "password")

You can download a complete working sample from Stream from GitHub.

Opening the read only Word document

You can open the ready only documents or read only streams using the OpenReadOnly method. If the Word document for reading is opened by any other application such as Microsoft Word, then the same document can be opened using DocIO in ReadOnly mode. The following code sample demonstrates the same.

//DocIO supports OpenReadOnly Word documents in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms alone.
//Creates an empty WordDocument instance
WordDocument document = new WordDocument();
//Loads or opens an existing word document using read only stream
document.OpenReadOnly("Template.docx", Syncfusion.DocIO.FormatType.Docx);
'Creates an empty WordDocument instance 
Dim document As WordDocument = New WordDocument
'Loads or opens an existing word document using read only stream
document.OpenReadOnly("Template.docx", Syncfusion.DocIO.FormatType.Docx)

You can also open an existing encrypted document in read only mode using the overloads as mentioned below.

//DocIO supports OpenReadOnly Word documents in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms alone.
//Creates an empty WordDocument instance
WordDocument document = new WordDocument();
//Loads or opens an existing encrypted word document using read only stream
document.OpenReadOnly("Template.docx", Syncfusion.DocIO.FormatType.Docx , "password");
'Creates an empty WordDocument instance 
Dim document As WordDocument = New WordDocument
'Loads or opens an existing encrypted word document using read only stream
document.OpenReadOnly("Template.docx", Syncfusion.DocIO.FormatType.Docx, "password")

You can download a complete working sample from GitHub.

Saving a Word document to file system

You can save the created or manipulated Word document to file system using Save method of WordDocument class. When you do not provide the format type, then the document is saved in Word 97-2003 (*.doc) format.

//Open an existing WordDocument
FileStream inputStream = new FileStream(inputFileName, FileMode.Open);
WordDocument document = new WordDocument(inputStream, FormatType.Docx);
//To-Do some manipulation
//To-Do some manipulation
//Saving the Word document
FileStream outputStream = new FileStream("Sample.docx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
document.Save(outputStream, FormatType.Docx);
document.Close();
outputStream.Flush();
outputStream.Dispose();
//Creates an empty WordDocument instance
WordDocument document = new WordDocument();
//opens an existing Word document through Open method of WordDocument class
document.Open(fileName);
//To-Do some manipulation
//To-Do some manipulation
//Saves the document in file system
document.Save(outputFileName, FormatType.Docx);
'Creates an empty WordDocument instance
Dim document As New WordDocument()
'opens an existing Word document through Open method of WordDocument class
document.Open(fileName)
'To-Do some manipulation
'To-Do some manipulation
'Saves the document in file system
document.Save(outputFileName, FormatType.Docx)

You can download a complete working sample from GitHub.

Saving a Word document to Stream

You can also save the created or manipulated word document to stream by using overloads of Save methods

//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
    //Loads or opens an existing Word document from stream
    FileStream fileStreamPath = new FileStream(@"Data/Hello World.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    //Loads or opens an existing Word document through Open method of WordDocument class 
    document.Open(fileStreamPath, FormatType.Automatic);
    //To-Do some manipulation
    //To-Do some manipulation
    //Creates an instance of memory stream
    MemoryStream stream = new MemoryStream();
    //Saves the document to stream
    document.Save(stream, FormatType.Docx);
    //Closes the document
    document.Close();
}
//Creates an empty WordDocument instance
WordDocument document = new WordDocument();
//Opens an existing Word document through Open method of WordDocument class
document.Open(fileName);
//To-Do some manipulation
//To-Do some manipulation
//Creates an instance of memory stream
MemoryStream stream = new MemoryStream();
//Saves the document to stream
document.Save(stream, FormatType.Docx);
'Creates an empty WordDocument instance
Dim document As New WordDocument()
'Opens an existing Word document through Open method of WordDocument class
document.Open(fileName)
'To-Do some manipulation
'To-Do some manipulation
'Creates an instance of memory stream
Dim stream As New MemoryStream()
'Saves the document to stream
document.Save(stream, FormatType.Docx)

You can download a complete working sample from GitHub.

Sending to a client browser

You can save and send the document to a client browser from a web site or web application by invoking the following shown overload of Save method. This method explicitly makes use of an instance of HttpResponse as its parameter in order to stream the document to client browser. So this overload is suitable for web application that references System.Web assembly.

//Creates a new instance of WordDocument (Empty Word Document)
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
//Appends the text to the created paragraph
paragraph.AppendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.");
MemoryStream stream = new MemoryStream();
//Saves the Word document to  MemoryStream
document.Save(stream, FormatType.Docx);
document.Close();
stream.Position = 0;
//Download Word document in the browser
return File(stream, "application/msword", "Result.docx");
//Creates an empty WordDocument instance
WordDocument document = new WordDocument();
//Opens an existing Word document through Open method of WordDocument class
document.Open(fileName);
//To-Do some manipulation
//To-Do some manipulation
//Creates an instance of memory stream
MemoryStream stream = new MemoryStream();
//Saves the document to stream
document.Save(outputFileName, FormatType.Docx, Response, HttpContentDisposition.Attachment);
'Creates an empty WordDocument instance
Dim document As New WordDocument()     
'Opens an existing Word document through Open method of WordDocument class
document.Open(fileName)
'To-Do some manipulation
'To-Do some manipulation
'Creates an instance of memory stream
Dim stream As New MemoryStream()
'Saves the document to stream
document.Save(outputFileName, FormatType.Docx, Response, HttpContentDisposition.Attachment)

You can download a complete working sample from GitHub.

NOTE

If you are using Syncfusion.DocIO.Net.Core package, then the Save API used in the above sample is not available in it. So, we suggest you to save the document as stream and then download. You can download a complete working sample from GitHub.

Closing a document

Once the document manipulation and save operation are completed, you should close the instance of WordDocument, in order to release all the memory consumed by DocIO’s DOM. The following code example illustrates how to close a WordDocument instance.

//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
    //Loads or opens an existing Word document from stream
    FileStream fileStreamPath = new FileStream(@"Data/Hello World.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    //Loads or opens an existing Word document through Open method of WordDocument class 
    document.Open(fileStreamPath, FormatType.Automatic);
    //To-Do some manipulation
    //To-Do some manipulation
    //Creates an instance of memory stream
    MemoryStream stream = new MemoryStream();
    //Saves the document to stream
    document.Save(stream, FormatType.Docx);
    //Closes the document
    document.Close();
}
//Creates an empty WordDocument instance
WordDocument document = new WordDocument();
//opens an existing word document through Open method of WordDocument class
document.Open(fileName);
//To-Do some manipulation
//To-Do some manipulation
//Creates an instance of memory stream
MemoryStream stream = new MemoryStream();
//Saves the document to stream
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
'creates an empty WordDocument instance
Dim document As New WordDocument()
'opens an existing word document through Open method of WordDocument class
document.Open(fileName)
'To-Do some manipulation
'To-Do some manipulation
'Creates an instance of memory stream
Dim stream As New MemoryStream()
'Saves the document to stream
document.Save(stream, FormatType.Docx)
'Closes the document
document.Close()

You can download a complete working sample from GitHub.

See Also

Frequently Asked Questions