Loading & saving document

15 Oct 2020 / 4 minutes to read

Opening an existing document

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

//Open an existing document from the file system using the constructor of WordDocument class.
WordDocument document = new WordDocument(fileName);
//Create an empty Word document instance.
WordDocument document = new WordDocument();
//Load or open an existing word document using the open method of WordDocument class.
document.open(fileName);

Opening an existing document from Stream

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

//Open an existing document from the stream using the constructor of the WordDocument class.
FileInputStream fileStreamPath = new FileInputStream("Input.docx");
WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic);
//Create an empty WordDocument instance.
FileInputStream fileStreamPath = new FileInputStream("Input.docx");
//Loads or opens an existing Word document using the open method of WordDocument class
WordDocument document = new WordDocument();
document.open(fileStreamPath, FormatType.Automatic);

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 the DocIO in ReadOnly mode. The following code sample explains the same.

//Create an empty WordDocument instance.
WordDocument document = new WordDocument();
//Load or open an existing word document using the read only stream.
document.openReadOnly("Template.docx", FormatType.Docx);

Saving a Word document to file system

You can save the created or manipulated Word document to the file system using the save method of WordDocument class.

//Create an empty WordDocument instance.
WordDocument document = new WordDocument();
//open an existing Word document using the Open method of WordDocument class.
document.open(fileName);
//To-Do some manipulation
//To-Do some manipulation
//Save the document in the file system.
document.save(outputFileName, FormatType.Docx);

Saving a Word document to Stream

You can also save the created or manipulated word document to the stream by using the 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
//Create an instance of the output stream.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//Save the document to stream.
document.save(stream, FormatType.Docx);

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 to stream the document to the client browser. So, this overload is suitable for a web application that references System.Web assembly.

//Create an empty WordDocument instance.
WordDocument document = new WordDocument();
//Open an existing Word document using the Open method of WordDocument class.
document.Open(fileName);
//To-Do some manipulation.
//To-Do some manipulation.
//Create an instance of the output stream.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//Save the document to the stream.
document.save(outputFileName, FormatType.Docx, Response, HttpContentDisposition.Attachment);

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 the DocIO’s DOM. The following code example shows how to close a WordDocument instance.

//Create an empty WordDocument instance.
WordDocument document = new WordDocument();
//Opens an existing Word document using the Open method of WordDocument class
document.Open(fileName);
//To-Do some manipulation.
//To-Do some manipulation.
//Create an instance of the output stream.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//Save the document to the stream.
document.save(stream, FormatType.Docx);
//Closes the document.
document.close();