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. 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 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-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(LoadAndSave.class);
Document doc = new Document(dataDir+ "Test File (doc).doc");
// Save the finished document to disk.
doc.save(dataDir + "Test File (doc)_out.doc", SaveFormat.PNG);

Save 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-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(LoadAndSaveToStream.class);
String inputFile = "Test File (doc).doc";
String outputFile = "output.png";
InputStream in = new FileInputStream(dataDir + inputFile);
OutputStream out = new FileOutputStream(dataDir + outputFile);
Document doc = new Document(in);
// Save the finished document to disk.
doc.save(out, SaveFormat.PNG);

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

Save 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-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ConvertDocumentToPCL.class);
// Load the document from disk.
Document doc = new Document(dataDir + "Document.doc");
PclSaveOptions saveOptions = new PclSaveOptions();
saveOptions.setSaveFormat(SaveFormat.PCL);
saveOptions.setRasterizeTransformedElements(false);
// Export the document as an PCL file.
doc.save(dataDir + "Document.PclConversion_out.pcl", saveOptions);