Specify Save Options

When saving a document, you can set some advanced properties. Aspose.Words provides you with the SaveOptions class, which allows more precise control of the save process. There are overloads of the Save method that accept a SaveOptions object – it should be an object of a class derived from the SaveOptions class. Each save format has a corresponding class that holds save options for this save format, for example, there is PdfSaveOptions for saving to PDF format, MarkdownSaveOptions for saving to Markdown format, or ImageSaveOptions for saving to an image. This article provides examples of working with some options classes derived from SaveOptions.

The following code example shows how to set the save options before saving the document into HTML:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_LoadingAndSaving();
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile RenderShape.docx");
// This is the directory we want the exported images to be saved to.
System::String imagesDir = System::IO::Path::Combine(outputDataDir, u"SpecifySaveOption.Images");
// The folder specified needs to exist and should be empty.
if (System::IO::Directory::Exists(imagesDir))
{
System::IO::Directory::Delete(imagesDir, true);
}
System::IO::Directory::CreateDirectory_(imagesDir);
// Set an option to export form fields as plain text, not as HTML input elements.
System::SharedPtr<HtmlSaveOptions> options = System::MakeObject<HtmlSaveOptions>(SaveFormat::Html);
options->set_ExportTextInputFormFieldAsText(true);
options->set_ImagesFolder(imagesDir);
System::String outputPath = outputDataDir + u"SpecifySaveOption.html";
doc->Save(outputPath, options);

The article describes a few properties you can control when saving a document.

Encrypt a Document With a Password

Use the Password property to get or set a password for an encrypted document. Use the Password property of the corresponding class to work with the selected document format.

For example, when saving a document to DOC or DOT format, use the Password property of the DocSaveOptions class.

The following code example shows how to set a password to encrypt a document using the RC4 encryption method:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.doc");
System::SharedPtr<DocSaveOptions> docSaveOptions = System::MakeObject<DocSaveOptions>();
docSaveOptions->set_Password(u"password");
System::String outputPath = outputDataDir + u"WorkingWithDoc.EncryptDocumentWithPassword.doc";
doc->Save(outputPath, docSaveOptions);

When saving a document to ODT format, use the Password property of the OdtSaveOptions class.

The following code example shows how to load and save OpenDocument encrypted with a password:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"encrypted.odt", System::MakeObject<LoadOptions>(u"password"));
System::String outputPath = outputDataDir + u"Load_Options.LoadAndSaveEncryptedODT.odt";
doc->Save(outputPath, System::MakeObject<OdtSaveOptions>(u"newpassword"));

Not all formats support encryption and the use of Password property.

Show Document Saving Progress Notifications

Aspose.Words provides the ability to use the ProgressCallback property to get notifications about the progress of document saving.

It is now available when saving to DOCX, FlatOpc, DOCM, DOTM, DOTX, HTML, MHTML, EPUB, XamlFlow, XamlFlowPack, or TXT formats.

Update the Document Creation Time

Aspose.Words provides an ability to use the CreatedTime property to get or set the document creation date in UTC. You can also update this value before saving using the UpdateCreatedTimeProperty option.

The following code example shows how to update the document creation time:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// Open a document
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.docx");
System::SharedPtr<SaveOptions> options = System::MakeObject<PdfSaveOptions>();
options->set_UpdateLastPrintedProperty(false);
System::String outputPath = outputDataDir + u"WorkingWithPdfSaveOptions.UpdateIfLastPrinted.pdf";
doc->Save(outputPath, options);

Update Last Saved Property

Aspose.Words provides an ability to use the UpdateLastSavedTimeProperty property to gets or sets a value determining whether the LastSavedTime property is updated before saving.

The following code example shows how to set this property and save the document:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.doc");
System::SharedPtr<OoxmlSaveOptions> ooxmlSaveOptions = System::MakeObject<OoxmlSaveOptions>();
ooxmlSaveOptions->set_UpdateLastSavedTimeProperty(true);
System::String outputPath = outputDataDir + u"WorkingWithOoxml.UpdateLastSavedTimeProperty.docx";
// Save the document to disk.
doc->Save(outputPath, ooxmlSaveOptions);

Save Black and White Image with One Bit Per Pixel Format

To control image saving options, the ImageSaveOptions class is used. For example, you can use the PixelFormat property to set the pixel format for the generated images. Please note that the pixel format of the output image may differ from the set value because of the work of GDI+.

The following code example shows how to save a black and white image with one bit per pixel format:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<ImageSaveOptions> opt = System::MakeObject<ImageSaveOptions>(SaveFormat::Png);
auto pageSet = System::MakeObject<PageSet>(System::MakeArray<System::SharedPtr<PageRange>>({System::MakeObject<PageRange>(1,1)}));
opt->set_PageSet(pageSet);
opt->set_ImageColorMode(ImageColorMode::BlackAndWhite);
opt->set_PixelFormat(ImagePixelFormat::Format1bppIndexed);
System::String outputPath = outputDataDir + u"ImageColorFilters.SaveImageToOnebitPerPixel.png";
doc->Save(outputPath, opt);