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-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Document.doc")
doc.save(docs_base.artifacts_dir + "BaseConversions.doc_to_docx.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-Python-via-.NET
# Read only access is enough for Aspose.words to load a document.
stream = io.FileIO(docs_base.my_dir + "Document.docx")
doc = aw.Document(stream)
# You can close the stream now, it is no longer needed because the document is in memory.
stream.close()
# ... do something with the document.
# Convert the document to a different format and save to stream.
dstStream = io.FileIO(docs_base.artifacts_dir + "BaseConversions.docx_to_rtf.rtf", "wb")
doc.save(dstStream, aw.SaveFormat.RTF)
dstStream.close()

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-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Rendering.docx")
saveOptions = aw.saving.PclSaveOptions()
saveOptions.save_format = aw.SaveFormat.PCL
saveOptions.rasterize_transformed_elements = False
doc.save(docs_base.artifacts_dir + "WorkingWithPclSaveOptions.rasterize_transformed_elements.pcl", saveOptions)