保存文書
你需要用 Aspose.Words 執行的任務大多都是儲存文件。 要儲存一個文件Aspose.Words,提供Save方法的Document類別。 有過載,可將文件儲存於檔案、串流或 ASP.NET HttpResponse 物件中傳送至客戶端瀏覽器。 該文件可儲存在 Aspose.Words 支援的任何儲存格式。 所有支援的儲存格式列表,請參閱 SaveFormat 列舉。
儲存到檔案
只需透過一個檔案名稱使用 Save 這個方法。 Aspose.Words 會從你指定的檔案擴展名來決定儲存格式。
以下範例示範如何在檔案中載入與儲存文書:
// 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.doc"); | |
doc.Save(ArtifactsDir + "BaseConversions.DocToDocx.docx"); |
儲存到流
將流物件傳給 Save 方法。 在儲存到流時,必須明顯指定儲存格式。
以下範例示出了如何將文件載入及儲存到串流:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
// Read only access is enough for Aspose.Words to load a document. | |
Document doc; | |
using (Stream stream = File.OpenRead(MyDir + "Document.docx")) | |
doc = new Document(stream); | |
// ... do something with the document. | |
// Convert the document to a different format and save to stream. | |
using (MemoryStream dstStream = new MemoryStream()) | |
{ | |
doc.Save(dstStream, SaveFormat.Rtf); | |
// Rewind the stream position back to zero so it is ready for the next reader. | |
dstStream.Position = 0; | |
File.WriteAllBytes(ArtifactsDir + "BaseConversions.DocxToRtf.rtf", dstStream.ToArray()); | |
} |
傳送文件到客戶端瀏覽器
在傳送一個檔案到客戶端瀏覽器時,請使用一個特別的超載來傳入四個參數:檔案名稱、儲存格式、儲存類型,及一個 ASP.NET HttpResponse 物件。 該文件會如何呈現給使用者由 ContentDisposition 列舉表示,決定了送交到瀏覽器的文件會提供一個直接在瀏覽器中打開或在與檔案拡張名相關聯的應用程式中打開選項。
接下來的程式碼範例示範了如何從 ASP.NET 程式碼傳送文件到客戶端瀏覽器。
// 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.doc"); | |
doc.Save(ArtifactsDir + "BaseConversions.DocToDocx.docx"); |
在使用 .NET Client Profile DLL 時,此方法的 Save
過載是不可用的。 這個 DLL 位於 net3.5_ClientProfile 資料夾中。 “.NET Client Profile 排除類別 כגון System.Web, 因此,HttpResponse 不可用。” 這是設計本意。
這可能 manifeste 出錯誤:
方法"Save"的負載數為4個參數。
若您需要在 Aspose.Words 應用程式中使用 ASP.NET,推薦使用有正確過載的 .NET 2.0 DLL,如本文所述。
儲存到 PCL
Aspose.Words支援將文件儲存成PCL (Printer Command Language)。 Aspose.Words可以將文件儲存成PCL 6 (PCL 6 Enhanced 或 PCL XL) 格式。 PclSaveOptions
類別可用於在儲存文件到 PCL 格式時指定額外的選項。
以下範例示範了如何以選項儲存文件到PCL:
// 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"); | |
PclSaveOptions saveOptions = new PclSaveOptions | |
{ | |
SaveFormat = SaveFormat.Pcl, RasterizeTransformedElements = false | |
}; | |
doc.Save(ArtifactsDir + "WorkingWithPclSaveOptions.RasterizeTransformedElements.pcl", saveOptions); |
見也
關於一 ASP.NET 個 HttpResponse 的資訊