Tuesday, February 5, 2013

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. 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);
Overview: Saaspose.Pdf
Saaspose.Pdf is a REST API to create, edit & manipulate PDF files. It also convert PDF file to DOC, DOCX, HTML, XPS, TIFF etc. You can create a new PDF either from scratch or from HTML, XML, template, database, XPS or an image. A PDF file can also be rendered to JPEG, PNG, GIF, BMP, TIFF and many other image formats. It works with any language like .NET, Java, PHP, Ruby, Python and many others. It is platform independent REST API & working with web, desktop, mobile or cloud applications alike.
More about Saaspose.Pdf
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

No comments:

Post a Comment