Split PDF C# csharp

Splitting a PDF document is a quite famous use case in file format manipulation. For instance, the finance management system of some organization creates a report containing many salary slips of their employees, then they need to split the PDF file to multiple files and email to the respective department for further processing. Aspose.PDF Cloud API is the perfect solution for PDF file manipulations.

A unique competitive edge for Aspose.PDF Cloud API is that it has been designed keeping in view the user-perspective. Since the skill set of all users is not the same, thus several SDKs are available in different languages including .NET, Java, PHP, Python, Ruby, and other programming languages. This leverages you to focus on what matters the most, because minor details like handling of requests and responses are managed by the SDK. However, in this article, we will be checking out the following use cases using C# language.

  • Split whole PDF file into separate PDF files
  • Split specific pages of a PDF document

One can also term this feature as cutting PDF documents or PDF cutter feature using C# language. However, before we proceed to splitting PDF files, it is important to note that you can configure several 3rd party Cloud Storages. Let us now upload a sample PDF document to Cloud Storage using following simple code snippet:

// Get AppKey and AppSID from https://dashboard.aspose.cloud/
// Install Nuget Package Aspose.Pdf-Cloud
// For complete examples and data files, please go to https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-dotnet
string FileName = "4pages.pdf";
PdfApi pdfApi = new PdfApi(AppKey, AppSid);
using (var file = System.IO.File.OpenRead(Path.Combine(LocalFolder, FileName)))
{
var response = PdfApi.UploadFile(Path.Combine(StorageFolder, FileName), file);
}
view raw UploadPDF.cs hosted with ❤ by GitHub

After the file is uploaded to Cloud Storage, let us move on to the code that works as PDF cutter or PDF splitter.

Split whole PDF file to multiple files

You may need to split all pages into separate PDF documents. For instance, n number of PDF documents will be generated if the original document contains n Pages. Follow the below steps in order to split a complete PDF file with single API call:

  1. Upload PDF file to Cloud Storage.
  2. Call PostSplitDocument API while setting necessary parameters.
  3. Output PDF files will be saved in configured cloud storage.

Below C# code snippet is a demonstration of how to split whole PDF document to multiple files.

// Get AppKey and AppSID from https://dashboard.aspose.cloud/
// Install Nuget Package Aspose.Pdf-Cloud
// For complete examples and data files, please go to https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-dotnet
const string FileName = "4pages.pdf";
PdfApi pdfApi = new PdfApi(AppKey, AppSid);
//Split whole PDF document page by page, into separate PDF documents
var response = PdfApi.PostSplitDocument(FileName, folder: "TempPdfCloud");

Split Specific Pages of PDF to Separate Files

You can also split specific pages of a PDF document to separate files as per your requirements. Below C# code can be used to process specific pages of the PDF document.

// Get AppKey and AppSID from https://dashboard.aspose.cloud/
// Install Nuget Package Aspose.Pdf-Cloud
// For complete examples and data files, please go to https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-dotnet
const string FileName = "4pages.pdf";
PdfApi pdfApi = new PdfApi(AppKey, AppSid);
//Split Page number 2,3 and 4 to separate PDF documents
var response = PdfApi.PostSplitDocument(FileName, from: 2, to: 4 , folder: "TempPdfCloud");

This code snippet creates separate PDF documents for page number 2,3, and 4 because of the values specified in API request. Those output PDF documents will be saved in the respective folder at Cloud Drive.

It elaborates on the simple yet efficient API features of Aspose.PDF Cloud API, where you only need to sign up for free and evaluate robust features with simple API calls.

Related Article

Use the following link to learn more about: