指定儲存選項
當儲存文件時,您可以設定一些進階屬性。 Aspose.Words 提供給您類別 SaveOptions,允許更精確的儲存過程控制。 有過載的 Save 方法可以接受 SaveOptions 物件,他應該是從 SaveOptions 類別派生的物件。 每個儲存格式都有對應的類別,該類別會儲存儲存格式的選項。例如,有 PdfSaveOptions 用於儲存到 PDF 格式,MarkdownSaveOptions 用於儲存到 Markdown 格式,或 ImageSaveOptions 用於儲存到圖像。 本文示例說明如何處理一些從 SaveOptions 導出的選項類別。
接下來這個程式碼範例示範了如何在儲存HTML文件前設定儲存選項:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Rendering.docx"); | |
string imagesDir = Path.Combine(ArtifactsDir, "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 saveOptions = new HtmlSaveOptions(SaveFormat.Html) | |
{ | |
ExportTextInputFormFieldAsText = true, ImagesFolder = imagesDir | |
}; | |
doc.Save(ArtifactsDir + "WorkingWithHtmlSaveOptions.ExportTextInputFormFieldAsText.html", saveOptions); |
該文章描述了一些當您儲存文件時可以控制的屬性。
用密碼加密一個檔案
使用 Password 屬性取得或設定加密文件的密碼。 使用對應類別的 Password 屬性來處理所選的文件格式。
例如,當儲存一個文件為DOC或DOT格式的時候,請使用Password屬性的DocSaveOptions類別。
以下範例會示範如何設定密碼來加密文件,使用 RC4 加密法:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Write("Hello world!"); | |
DocSaveOptions saveOptions = new DocSaveOptions { Password = "password" }; | |
doc.Save(ArtifactsDir + "WorkingWithDocSaveOptions.EncryptDocumentWithPassword.docx", saveOptions); |
儲存以 Odt 格式的文件時,請使用 Password 屬性的 OdtSaveOptions 類別。
以下程式碼示範了如何以密碼加密的OpenDocument方式載入並儲存:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword")); | |
doc.Save(ArtifactsDir + "WorkingWithLoadOptions.LoadSaveEncryptedDocument.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.git. | |
Document doc = new Document(MyDir + "Rendering.docx"); | |
PdfSaveOptions saveOptions = new PdfSaveOptions { UpdateLastPrintedProperty = true }; | |
doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.UpdateLastPrinted.pdf", saveOptions); |
更新上次儲存的屬性
Aspose.Words提供了一個能使用UpdateLastSavedTimeProperty屬性來獲取或設定一個值的能力,以決定是否在儲存前更新LastSavedTime屬性。
以下範例示範如何設定此屬性並儲存文件:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Document.docx"); | |
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions { UpdateLastSavedTimeProperty = true }; | |
doc.Save(ArtifactsDir + "WorkingWithOoxmlSaveOptions.UpdateLastSavedTime.docx", saveOptions); |
在將檔案儲存為 HTML 或 SVG 時,控制外部資源
若要將 HTML 或 SVG 轉換成 PDF,只要啟動 Save 方法並指定包含 .pdf 附檔名的檔案即可。 如果您想要從外部來源加載圖片、CSS 等檔案,您可以使用 IResourceSavingCallback。
以一比特每像素格式儲存黑白圖像
要控制圖片的儲存選項,則使用 ImageSaveOptions 類別。 例如,您可以使用 PixelFormat 屬性來設定所生成圖像的像素格式。 請注意,由於 GDI+的工作,輸出影像的像素格式可能與設定值不同。
以下範例顯示如何保存一張以一比特每像素格式的黑色與白色圖像:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Rendering.docx"); | |
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.Png) | |
{ | |
PageSet = new PageSet(1), | |
ImageColorMode = ImageColorMode.BlackAndWhite, | |
PixelFormat = ImagePixelFormat.Format1bppIndexed | |
}; | |
doc.Save(ArtifactsDir + "WorkingWithImageSaveOptions.Format1BppIndexed.Png", saveOptions); |