指定保存选项
保存文档时,可以设置一些高级属性。 Aspose.Words为您提供SaveOptions类,它允许更精确地控制保存过程。 有接受SaveOptions对象的Save方法的重载-它应该是从SaveOptions类派生的类的对象。 每个保存格式都有一个相应的类,用于保存此保存格式的保存选项,例如,有PdfSaveOptions用于保存为PDF格式,MarkdownSaveOptions用于保存为Markdown格式,或ImageSaveOptions用于保存为图像。 本文提供了使用从SaveOptions派生的一些options类的示例。
下面的代码示例演示如何在将文档保存到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); |
本文介绍了保存文档时可以控制的一些属性。
使用密码加密文档
使用Password属性获取或设置加密文档的密码。 使用相应类的Password属性来处理选定的文档格式。
例如,将文档保存为DOC或DOT格式时,请使用DocSaveOptions类的Password属性。
下面的代码示例演示如何设置密码以使用RC4加密方法加密文档:
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); |
将文档保存为ODT格式时,请使用OdtSaveOptions类的Password属性。
下面的代码示例演示如何加载和保存使用密码加密的OpenDocument:
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")); |
并非所有格式都支持加密和使用Password属性。
显示文档保存进度通知
Aspose.Words提供了使用ProgressCallback属性获取有关文档保存进度的通知的功能。
现在可以在保存到DOCX, FlatOpc, DOCM, DOTM, DOTX, HTML, MHTML, EPUB, XamlFlow, XamlFlowPack, 或TXT格式。
更新文档创建时间
Aspose.Words提供了使用CreatedTime属性在UTC中获取或设置文档创建日期的功能。 您也可以在使用UpdateCreatedTimeProperty选项保存之前更新此值。
下面的代码示例演示如何更新文档创建时间:
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); |
更新上次保存的属性
Aspose.Words提供了使用UpdateLastSavedTimeProperty属性获取或设置确定LastSavedTime属性是否在保存之前更新的值的功能。
下面的代码示例演示如何设置此属性并保存文档:
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); |
以每像素一位格式保存黑白图像
要控制图像保存选项,使用ImageSaveOptions类。 例如,您可以使用PixelFormat属性为生成的图像设置像素格式。 请注意,由于GDI+的工作,输出图像的像素格式可能与设定值不同。
下面的代码示例演示如何以每像素一位格式保存黑白图像:
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); |