Skip to main content

Split a PDF into Multiple PDFs


Split a PDF document into multiple PDFs using the pdf endpoint.

Use the pdf endpoint to split a PDF into multiple PDFs. In this example you start with a nine-page document and then extract pages 1-3 to form a new PDF and then extract pages 6-7 to form a second PDF. To accomplish splitting a PDF, call the PDF endpoint twice using the same document but different JSON instructions. The first call to the endpoint creates a new PDF from pages 1-3 while the second call to the endpoint creates a new PDF from pages 6-7. The following example illustrates.

tip

Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.

Calling Endpoint Directly

The following JSON instructions documents illustrate splitting a PDF into two PDFs. It calls the pdf endpoint twice. The first call uses the following instructions JSON and selects pages 1, 2, and 3.

{
"author": "CeteSoftware",
"creator": "DynamicPDF Cloud Api",
"inputs": [
{
"type": "pdf",
"resourceName": "pdfnumberedinput.pdf",
"startPage": 1,
"pageCount": 3
}
]
}

The second pdf call selects pages 6 and 7 when creating the new PDF.

{
"author": "CeteSoftware",
"creator": "DynamicPDF Cloud Api",
"inputs": [
{
"type": "pdf",
"resourceName": "pdfnumberedinput.pdf",
"startPage": 6,
"pageCount": 2
}
]
}

Run In Postman

curl --location 'https://api.dpdf.io/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--
--form 'Instructions=@C:/temp/solutions/delete-pages/instructions-one.json'
--form 'Resource=@C:/temp/solutions/delete-pages/pagenumberedinput.pdf'

curl --location 'https://api.dpdf.io/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--
--form 'Instructions=@C:/temp/solutions/delete-pages/instructions-two.json'
--form 'Resource=@C:/temp/solutions/delete-pages/pagenumberedinput.pdf'

Calling Endpoint Using Client Library

The following JSON instructions document illustrates deleting pages from a PDF using the six different client libraries.

public static void Run(string apiKey, string basePath, string outputPath)
{
SplitPdfs.Split(apiKey, basePath, outputPath, 1, 3, "split-one.pdf");
SplitPdfs.Split(apiKey, basePath, outputPath, 6, 2, "split-two.pdf");
}

static void Split(string apiKey, string basePath, string outputPath, int startPage, int pageCount, string outputFile)
{
Pdf pdf = new Pdf();
PdfInput input = pdf.AddPdf(new PdfResource(basePath + "pdfnumberedinput.pdf"));
input.StartPage = startPage;
input.PageCount = pageCount;

pdf.ApiKey = apiKey;
PdfResponse response = pdf.Process();

File.WriteAllBytes(outputPath + "/" + outputFile, response.Content);
}
Source: SplitPdfs.cs
info

The pdf endpoint takes a JSON instructions document that provides instructions for creating, transforming, merging, and formatting inputs into a combined PDF. Refer to documentation on the instructions schema for information on how to use the pdf endpoint.

   Follow us on social media for latest news!