指定保存选项
保存文档时,您可以设置一些高级属性。 Aspose.Words 为您提供了 SaveOptions 类,可以更精确地控制保存过程。存在接受 SaveOptions 对象的 Save 方法的重载 - 它应该是从 SaveOptions 类派生的类的对象。每种保存格式都有一个相应的类,用于保存该保存格式的保存选项,例如,用于保存为 PDF 格式的 PdfSaveOptions、用于保存为 Markdown 格式的 MarkdownSaveOptions 或用于保存为图像的 ImageSaveOptions。本文提供了使用从 SaveOptions 派生的一些选项类的示例。
以下代码示例演示如何在将文档保存为 HTML 之前设置保存选项:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadingAndSaving(); | |
string fileName = "TestFile RenderShape.docx"; | |
Document doc = new Document(dataDir + fileName); | |
// This is the directory we want the exported images to be saved to. | |
string imagesDir = Path.Combine(dataDir, "Images"); | |
// The folder specified needs to exist and should be empty. | |
if (Directory.Exists(imagesDir)) | |
Directory.Delete(imagesDir, true); | |
Directory.CreateDirectory(imagesDir); | |
// Set an option to export form fields as plain text, not as HTML input elements. | |
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html); | |
options.ExportTextInputFormFieldAsText = true; | |
options.ImagesFolder = imagesDir; | |
dataDir = dataDir + "Document.SaveWithOptions_out.html"; | |
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-.NET | |
Document doc = new Document(dataDir + "Document.docx"); | |
DocSaveOptions docSaveOptions = new DocSaveOptions(); | |
docSaveOptions.Password = "password"; | |
dataDir = dataDir + "Document.Password_out.docx"; | |
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-.NET | |
Document doc = new Document(dataDir + @"encrypted.odt", new 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-.NET | |
Document doc = new Document(MyDir + "Rendering.docx"); | |
PdfSaveOptions saveOptions = new PdfSaveOptions { UpdateLastPrintedProperty = true }; | |
doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.UpdateIfLastPrinted.pdf", saveOptions); |
更新上次保存的属性
Aspose.Words 提供了使用 UpdateLastSavedTimeProperty 属性来获取或设置一个值的功能,该值确定在保存之前是否更新 LastSavedTime 属性。
以下代码示例显示如何设置此属性并保存文档:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Document.docx"); | |
OoxmlSaveOptions ooxmlSaveOptions = new OoxmlSaveOptions(); | |
ooxmlSaveOptions.UpdateLastSavedTimeProperty = true; | |
dataDir = dataDir + "UpdateLastSavedTimeProperty_out.docx"; | |
// Save the document to disk. | |
doc.Save(dataDir, ooxmlSaveOptions); |
将文档保存为 HTML 或 SVG 时控制外部资源
要将 HTML 或 SVG 转换为 PDF,只需调用 Save 方法并指定带有".PDF"扩展名的文件名。如果要从外部源加载图像、CSS等,可以使用IResourceSavingCallback。
以下代码示例演示如何将 HTML 转换为 PDF 并从外部源加载图像:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public class ImageLoadingWithCredentialsHandler : IResourceLoadingCallback | |
{ | |
public ImageLoadingWithCredentialsHandler() | |
{ | |
mWebClient = new WebClient(); | |
} | |
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args) | |
{ | |
if (args.ResourceType == ResourceType.Image) | |
{ | |
Uri uri = new Uri(args.Uri); | |
if (uri.Host == "www.aspose.com") | |
mWebClient.Credentials = new NetworkCredential("User1", "akjdlsfkjs"); | |
else | |
mWebClient.Credentials = new NetworkCredential("SomeOtherUserID", "wiurlnlvs"); | |
// Download the bytes from the location referenced by the URI. | |
byte[] imageBytes = mWebClient.DownloadData(args.Uri); | |
args.SetData(imageBytes); | |
return ResourceLoadingAction.UserProvided; | |
} | |
else | |
{ | |
return ResourceLoadingAction.Default; | |
} | |
} | |
private WebClient mWebClient; | |
} |
以每像素一位格式保存黑白图像
为了控制图像保存选项,使用 ImageSaveOptions 类。例如,您可以使用 PixelFormat 属性来设置生成图像的像素格式。请注意,由于 GDI+ 的工作,输出图像的像素格式可能与设置值不同。
以下代码示例演示如何以每像素一位格式保存黑白图像:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.Png); | |
PageSet pageSet = new PageSet(new PageRange(1, 1)); | |
opt.PageSet = pageSet; | |
opt.ImageColorMode = ImageColorMode.BlackAndWhite; | |
opt.PixelFormat = ImagePixelFormat.Format1bppIndexed; | |
dataDir = dataDir + "Format1bppIndexed_Out.Png"; | |
doc.Save(dataDir, opt); |