
您可能需要將巨大的 word 檔案分割成較小的檔案。 Aspose.Words Cloud API 可以增強您的應用程序,無需任何第三方應用程式即可分割 Microsoft Word 文件。當您免費註冊時,將獲得豐厚的試用配額。這使您可以在購買 API 之前全面評估 API 的功能。
Aspose.Words Cloud API 支援多種程式語言。您可以將不同的 SDK]2 整合到您的應用程式中,以測試不同程式語言的 API 功能。這裡我們將考慮一些使用 C# 語言的基本範例。讓我們探討以下用例:
- 將 Word 文件 DOC/DOCX 拆分為多個文件
- 拆分大型 Word(DOC/DOCX)文件的特定頁面
- 將 Word 檔案的特定頁面拆分為 PDF
在開始這些範例之前,讓我們先了解如何將檔案上傳到雲端儲存。以下程式碼片段包含上傳檔案的方法:
// 從 https://dashboard.aspose.cloud/ 取得 AppKey 和 AppSID
// 安裝 Nuget 套件 Aspose.Words-Cloud
// 完整範例和資料檔請前往https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet
///<summary>
/// 將檔案上傳到儲存。
///</summary>
///<param name="path">儲存中的路徑。</param>
///<param name="versionId"> API 版本。</param>
///<param name="storage">貯存。</param>
///<param name="fileContent">文件內容。</param>
protected void UploadFileToStorage(string path, string versionId, string storage, byte[] fileContent)
{
using (var ms = new MemoryStream(fileContent))
{
var request = new UploadFileRequest(ms, path);
this.WordsApi.UploadFile(request);
}
}
將 Word 文件 (DOC/DOCX) 拆分為多個文件
現在只需一個 API 呼叫即可將 Word 文件拆分為單獨的頁面。只需設定參數並執行操作即可。下面的程式碼演示了此功能,其中幾行程式碼可以實現這些要求:
// 從 https://dashboard.aspose.cloud/ 取得 AppKey 和 AppSID
// 安裝 Nuget 套件 Aspose.Words-Cloud
// 完整範例和資料檔請前往https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
var localName = "test_multi_pages.docx";
var remoteName = "TestSplitDocument.docx";
var fullName = Path.Combine(this.dataFolder, remoteName);
string format = "docx";
var destFileName = Path.Combine(BaseTestOutPath, Path.GetFileNameWithoutExtension(remoteName) + ".docx");
//正如我們上面討論過的上傳
this.UploadFileToStorage(fullName, null, null, File.ReadAllBytes(BaseTestContext.GetDataDir(BaseTestContext.CommonFolder) + localName));
var request = new SplitDocumentRequest(remoteName, this.dataFolder, format: format, destFileName: destFileName);
var actual = wordsApi.SplitDocument(request);
SplitDocumentRequest 支援設定一些可選參數,包括頁面範圍值、如果來源 word 檔案已加密則設定的密碼、是否需要將輸出檔案作為單一 ZIP 檔案的布林值,以及如果文件中使用了某些自訂字型則設定的字型位置。
拆分 Word 文件的特定頁面
我們上面討論的例子將整個文件分成不同的頁面,因為沒有用 to 和 from 參數指定頁面範圍。現在,讓我們繼續討論您只想選擇特定頁面的場景。它適用於您不想分享或列印某些機密資訊的場景。以下程式碼片段將頁碼分為 3 至 5 頁,並將輸出儲存為單一 ZIP 檔案。
// 從 https://dashboard.aspose.cloud/ 取得 AppKey 和 AppSID
// 安裝 Nuget 套件 Aspose.Words-Cloud
// 完整範例和資料檔請前往https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
var localName = "test_multi_pages.docx";
var remoteName = "TestSplitDocument.docx";
var fullName = Path.Combine(this.dataFolder, remoteName);
string format = "docx";
var destFileName = Path.Combine(BaseTestOutPath, Path.GetFileNameWithoutExtension(remoteName) + ".docx");
int from = 3;
int to = 5;
this.UploadFileToStorage(fullName, null, null, File.ReadAllBytes(BaseTestContext.GetDataDir(BaseTestContext.CommonFolder) + localName));
var request = new SplitDocumentRequest(remoteName, this.dataFolder, format: format, @from: from, to: to, destFileName: destFileName);
var actual = wordsApi.SplitDocument(request);
將 Word 文件的特定頁面拆分為 PDF
Aspose.Words Cloud API 支援將 Microsoft Word 檔案分割成不同的格式。該 API 支援直接將這些頁面儲存為多種文件格式,包括圖像、HTML、PDF 和其他幾種格式,而不需要先拆分一些頁面然後轉換為所需的格式。以下程式碼片段將頁碼拆分為 2 至 6,並將輸出儲存為 PDF 文件。
// 從 https://dashboard.aspose.cloud/ 取得 AppKey 和 AppSID
// 安裝 Nuget 套件 Aspose.Words-Cloud
// 完整範例和資料檔請前往https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet
WordsApi wordsApi = new WordsApi(MyAppKey, MyAppSid);
var localName = "test_multi_pages.docx";
var remoteName = "TestSplitDocument.docx";
var fullName = Path.Combine(this.dataFolder, remoteName);
string format = "pdf";
var destFileName = Path.Combine(BaseTestOutPath, Path.GetFileNameWithoutExtension(remoteName) + ".pdf");
int from = 2;
int to = 6;
this.UploadFileToStorage(fullName, null, null, File.ReadAllBytes(BaseTestContext.GetDataDir(BaseTestContext.CommonFolder) + localName));
var request = new SplitDocumentRequest(remoteName, this.dataFolder, format: format, @from: from, to: to, destFileName: destFileName);
var actual = wordsApi.SplitDocument(request);
此程式碼片段足夠高效,只需一次 API 呼叫即可拆分頁面並將其轉換為特定格式。這些令人興奮的功能使該 API 最適合您的所有文件處理需求。如果您對 API 有任何疑問,請透過免費支援論壇與我們聯絡。我們很樂意為您提供協助!
相關文章
我們強烈建議您閱讀以下文章以獲取更多資訊: