指定保存选项

保存文档时,可以设置一些高级属性。 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-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SpecifySaveOption.class);
String fileName = "TestFile RenderShape.docx";
// Load the document.
Document doc = new Document(dataDir + fileName);
// This is the directory we want the exported images to be saved to.
File imagesDir = new File(dataDir, "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());
dataDir = dataDir + Utils.GetOutputFilePath(fileName);
doc.save(dataDir, 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-Java
Document doc = new Document(dataDir + "Document.doc");
DocSaveOptions docSaveOptions = new DocSaveOptions();
docSaveOptions.setPassword("password");
dataDir = dataDir + "Document.Password_out.doc";
doc.save(dataDir, docSaveOptions);

将文档保存为ODT格式时,请使用OdtSaveOptions类的Password属性。

下面的代码示例演示如何加载和保存使用密码加密的OpenDocument:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "encrypted.odt", new com.aspose.words.LoadOptions("password"));
doc.save(dataDir + "out.odt", new OdtSaveOptions("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-Java
// For complete examples and data files, please go to //
// https://github.com/aspose-words/Aspose.Words-for-Java
// Open a document
Document doc = new Document(dataDir + "Rendering.doc");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setUpdateLastPrintedProperty(false);
doc.save(dataDir + "PdfSaveOptions.UpdateIfLastPrinted.pdf", saveOptions);

更新上次保存的属性

Aspose.Words提供了使用UpdateLastSavedTimeProperty属性获取或设置确定LastSavedTime属性是否在保存之前更新的值的功能。

下面的代码示例演示如何设置此属性并保存文档:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Document.doc");
OoxmlSaveOptions ooxmlSaveOptions = new OoxmlSaveOptions();
ooxmlSaveOptions.setUpdateLastSavedTimeProperty(true);
dataDir = dataDir + "UpdateLastSavedTimeProperty_out.docx";
doc.save(dataDir, ooxmlSaveOptions);

以每像素一位格式保存黑白图像

要控制图像保存选项,使用ImageSaveOptions类。 例如,您可以使用PixelFormat属性为生成的图像设置像素格式。 请注意,由于GDI+的工作,输出图像的像素格式可能与设定值不同。

下面的代码示例演示如何以每像素一位格式保存黑白图像:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
opt.setPageSet(new PageSet(1));
opt.setImageColorMode(ImageColorMode.BLACK_AND_WHITE);
opt.setPixelFormat(ImagePixelFormat.FORMAT_1_BPP_INDEXED);
dataDir = dataDir + "Format1bppIndexed_Out.Png";
doc.save(dataDir, opt);