Platinum Partner

Split Specific or All Pages of PDF File & Save Each Page as a New PDF

This technical tip allows developers to split all or specific pages of a PDF file and save each page as a new PDF or any supported format using Saaspose.Pdf REST API in your .NET applications. Input PDF file needs to be uploaded in root folder of Saaspose Storage before running this code. It is platform independent REST API & working with web, desktop, mobile or cloud applications alike. Some important steps for performing this task are to build URI to split PDF pages, sign URI, further process JSON response, Parse the json string to JObject, build URI to download split pages and save split PDF pages as PDF.
//Sample Code for Splitting all pages to new PDFs

SaasposeApp.AppSID  = "77***********************************";
SaasposeApp.AppKey = "9a*******************************";
string outputPath = "C:\\TempFiles\\";

//build URI to split PDF pages
string strURI = "http://api.saaspose.com/v1.0/pdf/4pages.pdf/split";
//sign URI
string signedURI = Utils.Sign(strURI);
StreamReader reader = new StreamReader(Utils.ProcessCommand(signedURI, "POST"));
//further process JSON response
string strJSON = reader.ReadToEnd();
//Parse the json string to JObject
JObject parsedJSON = JObject.Parse(strJSON);
SplitPDFResponse responseStream = JsonConvert.DeserializeObject(parsedJSON.ToString());

foreach (LinkResponse splitPage in responseStream.Result.Documents) 
{
    string splitFileName = System.IO.Path.GetFileName(splitPage.Href);
    //build URI to download split pages 
    strURI = "http://api.saaspose.com/v1.0/storage/file/" + splitFileName;
    //sign URI
    signedURI = Utils.Sign(strURI);
    //save split PDF pages as PDF
    using (Stream fileStream = System.IO.File.OpenWrite(outputPath + splitFileName))
    {
        Utils.CopyStream(Utils.ProcessCommand(signedURI, "GET"), fileStream);
    }
}
// class definitions
public class SplitPDFResponse : Saaspose.Common.BaseResponse
{
    public SplitPdfResult Result { get; set; }
}
public class SplitPdfResult
{
    public LinkResponse[] Documents { get; set; }
}

//Split specific pages to new PDFs

SaasposeApp.AppSID  = "77***********************************";
SaasposeApp.AppKey = "9a*******************************";
string outputPath = "C:\\TempFiles\\";

//build URI to split PDF pages
string strURI = "http://api.saaspose.com/v1.0/pdf/4pages.pdf/split?from=2&to=3";
//sign URI
string signedURI = Utils.Sign(strURI);
StreamReader reader = new StreamReader(Utils.ProcessCommand(signedURI, "POST"));
//further process JSON response
string strJSON = reader.ReadToEnd();
//Parse the json string to JObject
JObject parsedJSON = JObject.Parse(strJSON);
SplitPDFResponse responseStream = JsonConvert.DeserializeObject(parsedJSON.ToString());

foreach (LinkResponse splitPage in responseStream.Result.Documents) 
{
    string splitFileName = System.IO.Path.GetFileName(splitPage.Href);
    //build URI to download split pages 
    strURI = "http://api.saaspose.com/v1.0/storage/file/" + splitFileName;
    //sign URI
    signedURI = Utils.Sign(strURI);
    //save split PDF pages as PDF
    using (Stream fileStream = System.IO.File.OpenWrite(outputPath + splitFileName))
    {
        Utils.CopyStream(Utils.ProcessCommand(signedURI, "GET"), fileStream);
    }
}

//Split PDF pages to any supported format

SaasposeApp.AppSID  = "77***********************************";
SaasposeApp.AppKey = "9a*******************************";
string outputPath = "C:\\TempFiles\\";

//build URI to split PDF pages
string strURI = "http://api.saaspose.com/v1.0/pdf/4pages.pdf/split?from=2&to=3&format=tiff";
//sign URI
string signedURI = Utils.Sign(strURI);
StreamReader reader = new StreamReader(Utils.ProcessCommand(signedURI, "POST"));
//further process JSON response
string strJSON = reader.ReadToEnd();
//Parse the json string to JObject
JObject parsedJSON = JObject.Parse(strJSON);
SplitPDFResponse responseStream = JsonConvert.DeserializeObject(parsedJSON.ToString());

foreach (LinkResponse splitPage in responseStream.Result.Documents) 
{
    string splitFileName = System.IO.Path.GetFileName(splitPage.Href);
    //build URI to download split pages 
    strURI = "http://api.saaspose.com/v1.0/storage/file/" + splitFileName;
    //sign URI
    signedURI = Utils.Sign(strURI);
    //save split PDF pages as PDF
    using (Stream fileStream = System.IO.File.OpenWrite(outputPath + splitFileName))
    {
        Utils.CopyStream(Utils.ProcessCommand(signedURI, "GET"), fileStream);
    }}
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}