Aspose.Words

Save a Document

Use the Document.Save method for saving a document. 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.

Saving to a File

Simply use the Document.Save(String) method with a file name. Aspose.Words will infer the save format from the file extension that you specify.

Example SaveToFile

Saves a document to a file.

[Java]

 

doc.save(getMyDir() + "Document.OpenFromFile Out.doc");

 

 

Saving to a Stream

You pass a stream object to the Document.Save(Stream, SaveFormat) method. When you save to a stream, you must specify the save format explicitly.

Example SaveToStream

Shows how to save a document to a stream.

[Java]

 

Document doc = new Document(getMyDir() + "Document.doc");

 

ByteArrayOutputStream dstStream = new ByteArrayOutputStream();

doc.save(dstStream, SaveFormat.DOCX);

 

// In you want to read the result into a Document object again, in Java you need to get the

// data bytes and wrap into an input stream.

ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());

 

 

Specifying Save Options

There are Document.Save method overloads that accept a SaveOptions object. This should be an object of a class derived from the SaveOptions class. Each save format has a corresponding class that holds save options for that save format, for example there is PdfSaveOptions for the SaveFormat.Pdf save format.

More info about using save options is coming soon.

Example SaveWithOptions

Shows how to set save options before saving a document to HTML.

[Java]

 

Document doc = new Document(getMyDir() + "Rendering.doc");

 

// This is the directory we want the exported images to be saved to.

File imagesDir = new File(getMyDir(), "Images");

 

// The folder specified needs to exist and should be empty.

if(imagesDir.exists())

    imagesDir.delete();

 

imagesDir.mkdir();

 

// Set an option to export form fields as plain text, not as HTML input elements.

HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);

options.setExportTextInputFormFieldAsText(true);

options.setImagesFolder(imagesDir.getPath());

 

doc.save(getMyDir() + "Document.SaveWithOptions Out.html", options);