Split PDF Document with C# REST API

This short tutorial guides how to split PDF document with C# REST API. You will learn how to split PDF file online with C# REST Interface using the .NET cloud SDK. It will explain all the parameters required to customize the splitting process of the PDF.

Prerequisite

Steps to Split PDF with C# Low Code API

  1. Configure the PdfApi class object using the client ID and secret
  2. Upload the source PDF file to the cloud by assigning a name for subsequent reference
  3. Invoke the PostSplitDocument method to split PDF files by pages within a specified range
  4. Iterate through the collection of split PDF documents and download each PDF file
  5. Save each PDF file as a separate file using the unique name assigned by the API

These steps summarize the development of the PDF splitter software with C# .NET-based API. First, load the source PDF file to the cloud to split it page-by-page, and call the PostSplitDocument() method in the PdfApi class to split it. Provide the necessary parameters, such as reference to the uploaded PDF file, format of the file, starting page number, ending page number, and a few optional parameters if required.

Code for Splitting of PDF Online with C# Low Code API

using System;
using System.IO;
using Aspose.Pdf.Cloud.Sdk.Api;
using Aspose.Pdf.Cloud.Sdk.Model;
using System.Collections.Generic;
namespace Aspose.PDF.Cloud.Examples.Kb
{
public class PdfTasks
{
public static void SplitPDFFiles()
{
PdfApi pdfApi = new PdfApi("API KEY", "API SID");// For splitting the PDF
String fileName = "sample.pdf";
String format = "pdf";
int from = 1;
int to = 2;
String storage = "";
String folder = "";
try
{
// Load the input PDF file into the cloud
pdfApi.UploadFile(fileName, new MemoryStream( System.IO.File.ReadAllBytes(fileName)));
// Split the PDF pages
SplitResultResponse apiResponse = pdfApi.PostSplitDocument(fileName, format, from, to, storage, folder);
if (apiResponse.Status.Equals("OK"))
{
// Download created pdf file
foreach(var item in apiResponse.Result.Documents)
{
Stream storageRes = pdfApi.DownloadFile(item.Href);
storageRes.Position = 0;
using (FileStream fileStream = new FileStream(item.Href, FileMode.Create, FileAccess.Write))
{
storageRes.CopyTo(fileStream);
}
}
Console.WriteLine("Split PDF Files, Done!");
Console.ReadKey();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
}
}
}

This code demonstrates how an online PDF splitter software with C# REST API works. The API returns a SplitResultResponse object that contains a list of documents with a unique name for each page. You may download all or selected pages based on your requirements.

This article has taught us how to split PDF document online with C# RESTful Service. To merge the PDF files into a single PDF, refer to the following article: Merge PDF with C# REST API.

 English