在 Windows Azure 中轉換文檔
Aspose.Words是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
Windows Azure 上的 Aspose.Words 提供了加載、轉換和保存文檔的功能。為此,您可以創建一個應用程序,該應用程序:
- 實現一個簡單的 ASP.NET 表單,使用戶能夠加載文檔并指定所需的輸出格式。
- 調用 Aspose.Words 來轉換文檔并將其發送回瀏覽器。
本文中描述的應用程序作為WebRole實現,可以在開發結構中運行(在開發人員的機器上)或部署到Windows Azure。此應用程序是Aspose.Words如何在云中工作的可能示例之一。
先決條件
- Active Microsoft Azure 訂閱。如果您沒有免費帳戶,請在開始之前創建一個免費帳戶。
- 安裝了 Azure 開發的 Visual Studio 2019 或 Visual Studio 2017。
轉換文檔應用程序
本節討論一個不使用高級Aspose.Words功能或Windows Azure平臺的復雜服務的基本項目。
該項目演示了如何使用Aspose.Words輕松構建在云中運行的應用程序。
創建 Web 角色項目
要創建應用程序,您需要執行以下步驟:
- 在 Visual Studio 中創建新的云服務項目。
- 選擇云服務以具有一個 WebRole 項目。
- 將 NuGet 引用添加到 Aspose.Words。
- 將“文件上載”控件添加到“默認.aspx”窗體,使用戶能夠選擇要上載的文件。
- 將下拉列表控件添加到 Default.aspx 窗體,使用戶能夠選擇輸出格式。
- 為其添加“提交”按鈕和 Click 事件處理程序。
- 修改 ServiceDefinition.csdef 配置文件,以便應用程序可以在完全信任下在 Windows Azure 中運行。建議您啟用 NativeCodeExecution = “true”,以避免在使用 Aspose.Words 將文檔轉換為 PDF 或 XPS 時可能出現的任何權限問題。
使用 Aspose.Words 轉換文檔的實際代碼僅包含兩行,這些行創建新的 Document 對象以加載文檔,然后使用所需格式調用 Save 方法。下面的代碼示例演示如何在 Windows Azure 中轉換文檔:
using Aspose.Words; using Aspose.Words.Saving; using System.Web; using System; using System.IO; namespace WebRole { /// <summary> /// This demo shows how to use Aspose.Words for .NET inside a WebRole in a simple /// Windows Azure application. There is just one ASP.NET page that provides a user /// interface to convert a document from one format to another. /// </summary> public partial class _Default : System.Web.UI.Page { protected void SubmitButton_Click(object sender, EventArgs e) { HttpPostedFile postedFile = SrcFileUpload.PostedFile; if (postedFile.ContentLength == 0) throw new Exception("There was no document uploaded."); if (postedFile.ContentLength > 512 * 1024) throw new Exception("The uploaded document is too big. This demo limits the file size to 512Kb."); // Create a suitable file name for the converted document. string dstExtension = DstFormatDropDownList.SelectedValue; string dstFileName = Path.GetFileName(postedFile.FileName) + "_Converted." + dstExtension; SaveFormat dstFormat = FileFormatUtil.ExtensionToSaveFormat(dstExtension); // Convert the document and send to the browser. Document doc = new Document(postedFile.InputStream); doc.Save(Response, dstFileName, ContentDisposition.Inline, SaveOptions.CreateSaveOptions(dstFormat)); // Required. Otherwise DOCX cannot be opened on the client (probably not all data sent // or some extra data sent in the response). Response.End(); } static _Default() { // Uncomment this code and embed your license file as a resource in this project and this code // will find and activate the license. Aspose.Wods licensing needs to execute only once // before any Document instance is created and a static ctor is a good place. // // Aspose.Words.License l = new Aspose.Words.License(); // l.SetLicense("Aspose.Words.lic"); } } }
測試和運行 Web 角色項目
若要測試和運行示例項目,請執行以下步驟:
- 首先,檢查作為簡單 ASP.NET 應用程序運行的示例項目。使 WebRole 項目成為解決方案中的啟動項目并運行。Visual Studio將打開一個瀏覽器窗口并將表單加載到其中。您可以上傳示例文檔并驗證轉換是否正常工作。
- 然后,您應該測試項目是否在計算機上的開發結構中運行良好。Development Fabric 是 Windows Azure 計算和存儲服務的本地模擬。選擇“云服務”項目,使其成為解決方案的“啟動”項目并運行。這一次,構建應用程序可能需要更長的時間,因為Visual Studio啟動了Development Fabric并將項目部署到其中。瀏覽器窗口將再次打開表單,您將能夠測試應用程序。
- 最后,您可以在 Windows Azure 中部署和測試您的項目。為此,您需要有一個有效的 Microsoft Azure 訂閱。 在 Visual Studio 中,右鍵單擊云服務項目,然后選擇“發布”。如有必要,請使用您的 Microsoft Azure 帳戶登錄并選擇訂閱。 要進行測試,請選擇暫存環境,然后單擊發布。之后,在 Azure 活動日志中,你將收到已部署應用程序的 URL。
下圖顯示了在 Microsoft Azure 云中運行的 Web 角色項目: