Loading & saving document

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 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)
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Opens an existing document through constructor of `WordDocument` class
WordDocument document = new WordDocument(assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx"),FormatType.Docx);
//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);
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Opens an existing document through constructor of `WordDocument` class
WordDocument document = new WordDocument(assembly.GetManifestResourceStream("XamarinFormsApp1.Assets.Test.docx"),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)
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//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(assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx"),FormatType.Docx);
//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);
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//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(assembly.GetManifestResourceStream("XamarinFormsApp1.Assets.Test.docx"),FormatType.Automatic);

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
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)
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Loads or opens an existing Word document from stream
Stream inputStream = assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx");
//Opens an existing document through constructor of `WordDocument` class
WordDocument document = new WordDocument(inputStream, FormatType.Docx);
//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);
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Loads or opens an existing Word document from stream
Stream inputStream = assembly.GetManifestResourceStream("XamarinFormsApp1.Assets.Hello World.docx");
//Opens an existing document through constructor of `WordDocument` class
WordDocument document = new WordDocument(inputStream, 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)
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
	//Loads or opens an existing Word document from stream
	Stream inputStream = assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx");
	//Loads or opens an existing Word document through Open method of WordDocument class
	document.Open(inputStream, FormatType.Docx);
}
//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);
}
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
	//Loads or opens an existing Word document from stream
	Stream inputStream = assembly.GetManifestResourceStream("XamarinFormsApp1.Assets.Hello World.docx");
	//Loads or opens an existing Word document through Open method of WordDocument class
	document.Open(inputStream, FormatType.Automatic);
}

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.

//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")
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Loads or opens an existing Word document from stream
Stream inputStream = assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx");
//Opens an existing encrypted document through constructor of WordDocument class
WordDocument document = new WordDocument(inputStream, FormatType.Docx, "password");
//DocIO supports Encryption in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms alone.
//DocIO supports Encryption in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms alone.
//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")
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
	//Loads or opens an existing Word document from stream
	Stream inputStream = assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx");
	//Loads or opens an existing encrypted Word document through Open method of WordDocument class
	document.Open(inputStream, FormatType.Docx, "password");
}
//DocIO supports Encryption in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms alone.
//DocIO supports Encryption in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms alone.

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.

//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)
//DocIO supports saving Word document to file system in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms alone.
//DocIO supports saving Word document to file system in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms alone.
//DocIO supports saving Word document to file system in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms alone.

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
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)
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument());
{
	//Loads or opens an existing Word document from stream
	Stream inputStream = assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx");
	//Loads or opens an existing Word document through Open method of WordDocument class
	document.Open(inputStream, FormatType.Docx);
	//To-Do some manipulation
	//To-Do some manipulation
	//Creates an instance of memory stream
	MemoryStream stream = new MemoryStream();
	//Saves the Word file to MemoryStream
	await document.SaveAsync(stream, FormatType.Docx);
	//Saves the stream as Word file in local machine
	Save(stream, "Result.docx");
}

// Saves the Word document
async void Save(MemoryStream streams, string filename)
{
	streams.Position = 0;
	StorageFile stFile;
	if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
	{
		FileSavePicker savePicker = new FileSavePicker();
		savePicker.DefaultFileExtension = ".docx";
		savePicker.SuggestedFileName = filename;
		savePicker.FileTypeChoices.Add("Word Documents", new List<string>() {".docx"});
		stFile = await savePicker.PickSaveFileAsync();
	}
	else
	{
		StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
		stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	}
	if (stFile != null)
	{
		using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
		{
			//Write compressed data from memory to file
			using (Stream outstream = zipStream.AsStreamForWrite())
			{
				byte[] buffer = streams.ToArray();
				outstream.Write(buffer, 0, buffer.Length);
				outstream.Flush();
			}
		}
	}
}
//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);
	stream.Position = 0;
	//Download Word document in the browser
	return File(stream, "application/msword", "Result.docx");
}
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
	//Loads or opens an existing Word document from stream
	Stream inputStream = assembly.GetManifestResourceStream("XamarinFormsApp1.Assets.Hello World.docx");
	//Loads or opens an existing Word document through Open method of WordDocument class
	document.Open(inputStream, 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);
	//Save the stream as a file in the device and invoke it for viewing
	Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Result.docx", "application/msword", stream);
}

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 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)
//Saving and sending the Word document to a client browser from a web site is suitable for web applications alone.
//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");
//Saving and sending the Word document to a client browser from a web site is suitable for web applications alone.

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
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()
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
	//Loads or opens an existing Word document from stream
	Stream inputStream = assembly.GetManifestResourceStream("CreateWordSample.Assets.Test.docx");
	//Loads or opens an existing Word document through Open method of WordDocument class
	document.Open(inputStream, FormatType.Docx);
	//To-Do some manipulation
	//To-Do some manipulation
	//Creates an instance of memory stream
	MemoryStream stream = new MemoryStream();
	//Saves the Word file to MemoryStream
	await document.SaveAsync(stream, FormatType.Docx);
	//Saves the stream as Word file in local machine
	Save(stream, "Result.docx");
	//Closes the document
	document.Close();
}
//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()
	stream.Position = 0;
	//Download Word document in the browser
	return File(stream, "application/msword", "Result.docx");
}
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Creates an empty WordDocument instance
using (WordDocument document = new WordDocument())
{
	//Loads or opens an existing Word document from stream
	Stream inputStream = assembly.GetManifestResourceStream("XamarinFormsApp1.Assets.Hello World.docx");
	//Loads or opens an existing Word document through Open method of WordDocument class
	document.Open(inputStream, 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);
	//Save the stream as a file in the device and invoke it for viewing
	Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Result.docx", "application/msword", stream);
	//Closes the document
	document.Close();
}