
You may need to split huge word files into smaller files. Aspose.Words Cloud API can enhance your applications with the ability to split Microsoft Word Documents without needing any 3rd party application. A generous trial quota is awarded when you sign up for free. This lets you evaluate the API in its full capacity before purchasing the API.
Aspose.Words Cloud API supports several programming languages. Different SDKs are available that you can integrate into your applications to test API features in different programming languages. Here we will be considering a few basic examples using C# language. Let us explore the following use cases:
- Split Word documents DOC/DOCX to multiple files
- Split Specific Pages of Large Word (DOC/DOCX) documents
- Split Specific Pages of Word File to PDF
Before we start these examples, let us learn how a file can be uploaded to Cloud Storage. Following code snippet contains the method which uploads a file:
// Get AppKey and AppSID from https://dashboard.aspose.cloud/ | |
// Install Nuget Package Aspose.Words-Cloud | |
// For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet | |
/// <summary> | |
/// Uploads file to storage. | |
/// </summary> | |
/// <param name="path">Path in storage.</param> | |
/// <param name="versionId">Api version.</param> | |
/// <param name="storage">Storage.</param> | |
/// <param name="fileContent">File content.</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); | |
} | |
} |
Split Word Documents (DOC/DOCX) to Multiple Files
Splitting a word document to separate pages is now possible with only one API call. Simply set the parameters and perform the operation. Below code demonstrates this feature where few lines of code can achieve these requirements:
// Get AppKey and AppSID from https://dashboard.aspose.cloud/ | |
// Install Nuget Package Aspose.Words-Cloud | |
// For complete examples and data files, please go to 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"); | |
//As we have discussed this uploading above | |
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); |
The SplitDocumentRequest supports setting some optional parameters including page range values, password if source word file is encrypted, a bool value to specify if you need output files as single ZIP file, as well as fonts location if some custom fonts are being used in the document.
Split Specific Pages of Word Documents
The example we have discussed above splits the whole document to separate pages because the page range is not specified with to and from parameters. Now, let us move forward with a scenario where you want to select specific pages only. It is suitable for scenarios when you do not want to share or print some confidential information. Following code snippet splits page number 3 to 5 and saves the output as a single ZIP file.
// Get AppKey and AppSID from https://dashboard.aspose.cloud/ | |
// Install Nuget Package Aspose.Words-Cloud | |
// For complete examples and data files, please go to 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); |
Split Specific Pages of Word File to PDF
Aspose.Words Cloud API supports splitting Microsoft Word files into different formats. Instead of first splitting some pages and then converting to the required format, the API supports direct saving of those pages to several file-formats including images, HTML, PDF, and several others. Following code snippet splits page numbers 2 to 6 and saves the output as PDF documents.
// Get AppKey and AppSID from https://dashboard.aspose.cloud/ | |
// Install Nuget Package Aspose.Words-Cloud | |
// For complete examples and data files, please go to 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); |
This code snippet is efficient enough to split as well as convert a page to a specific format with a single API call. Such exciting features make this API the best fit for all of your document processing needs. If you have any questions about the API, please reach out to us at Free Support Forums. We would love to assist you!
Related Article
We highly recommend visiting the following articles for more information on: