Save a Document

Most of the tasks you need to perform with Aspose.Words involve saving a document. To save a document Aspose.Words provides the save method of the Document class. There are overloads that allow saving a document to a file or stream. The document can be saved in any save format supported by Aspose.Words. For the list of all supported save formats, see the SaveFormat enumeration.

Save a Document to a File

Simply use the save method with a file name. Aspose.Words will determine the save format from the file extension that you specify.

The following code example shows how to load and save a document to a file:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Node.js-via-.NET.git.
let doc = new aw.Document(base.myDir + "Document.doc");
doc.save(base.artifactsDir + "BaseConversions.DocToDocx.docx");

Save a Document to a Stream

Pass a stream object to the save method. It’s necessary to specify the save format explicitly when saving to a stream.

The following code example shows how to load and save a document to a stream:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Node.js-via-.NET.git.
// Read only access is enough for Aspose.Words to load a document.
let stream = base.loadFileToBuffer(base.myDir + "Document.docx");
let doc = new aw.Document(stream);
// ... do something with the document.
// Convert the document to a different format and save to stream.
const dstFile = base.artifactsDir + "BaseConversions.DocxToRtf.rtf";
const dstStream = fs.createWriteStream(dstFile);
doc.save(dstStream, aw.SaveFormat.Rtf);
await new Promise(resolve => dstStream.on("finish", resolve));

You can download the template file of this example from Aspose.Words GitHub.

Save a Document to PCL

Aspose.Words supports saving a document into PCL (Printer Command Language). Aspose.Words can save documents into PCL 6 (PCL 6 Enhanced or PCL XL) format. The PclSaveOptions class can be used to specify additional options when saving a document into the PCL format.

The following code example shows how to save a document to PCL using save options:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Node.js-via-.NET.git.
let doc = new aw.Document(base.myDir + "Rendering.docx");
let saveOptions = new aw.Saving.PclSaveOptions();
saveOptions.saveFormat = aw.SaveFormat.Pcl;
saveOptions.rasterizeTransformedElements = false;
doc.save(base.artifactsDir + "WorkingWithPclSaveOptions.rasterizeTransformedElements.pcl", saveOptions);