Home > Code > C# > Split Specific or All Pages of PDF File & Save Each Page as a New PDF

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

by sher azam   on Feb 05, 2013   Category: C#   |  Views: 1340    |  Points: 25   |  Gold 


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. Saaspose.Pdf is a REST API to create, edit & manipulate PDF files. It also convert PDF file to DOC, DOCX, HTML, XPS, TIFF etc. 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<SplitPDFResponse>(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<SplitPDFResponse>(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<SplitPDFResponse>(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);
}}

More about Saaspose.Pdf

- Homepage of Saaspose.Pdf: http://saaspose.com/api/pdf

- More Technical Tips by Saaspose.Pdf about Working with Documents: http://saaspose.com/docs/display/pdf/1.2.1.1+-+Working+with+Document

- Ask technical questions/queries from Saaspose Support Team: http://saaspose.com/support/contact-us



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts