Loading & saving document

13 Aug 20264 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 read-only documents or read-only streams using the openReadOnly method. If the Word document you want to read is opened by another application such as Microsoft Word, you can still open the same document using DocIO in read-only 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 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 stream.
document.save(stream, FormatType.Docx);

Sending a document 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.
//Set the response headers so the browser downloads the document as an attachment.
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-Disposition", "attachment; filename=\"" + outputFileName + "\"");
//Save the document to the servlet response output stream.
document.save(response.getOutputStream(), FormatType.Docx);

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();