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

sherazam77

Well-known member
Joined
Apr 29, 2011
Messages
300
Location
Lane Cove, New South Wales
What is new in this release?

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....ges.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..../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....t?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..../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....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..../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
- More Technical Tips by Saaspose.Pdf about Working with Documents
- More Technical Tips by Saaspose.Pdf
- Ask technical questions/queries from Saaspose Support Team

Contact Information

Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
Saaspose - Your File Format Experts 2.0
sales@aspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465
 
Back
Top