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.
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
}
]
}
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.
- C# (.NET)
- Java
- Node.js
- PHP
- GO
- Python
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);
}
static async Run() {
var apikey = "DP--api-key--";
var pdf = new Pdf();
pdf.apiKey = apikey;
var pdf2 = new Pdf();
pdf2.apiKey = apikey;
SplitPdf.Split(pdf, 1, 3, "./resources/split-pdf/split-one.pdf")
SplitPdf.Split(pdf, 6, 2, "./resources/split-pdf/split-two.pdf");
}
static async Split(pdf, startPage, pageCount, outFile) {
var pdfInput = pdf.addPdf(new PdfResource("./resources/split-pdf/pdfnumberedinput.pdf"));
pdfInput.startPage = startPage;
pdfInput.pageCount = pageCount;
var res = await pdf.process();
if (res.isSuccessful) {
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
} else {
console.log(res.errorJson);
}
}
public static void Run(String apiKey, String basePath){
SplitPdf.Split(apiKey, basePath, 1, 3, "split-one.pdf");
SplitPdf.Split(apiKey, basePath, 6, 2, "split-two.pdf");
}
private static void Split(String apiKey, String basePath, int startPage, int pageCount, String outputFile) {
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
PdfInput pdfInput = pdf.addPdf(new PdfResource(basePath + "pdfnumberedinput.pdf"));
pdfInput.setStartPage(startPage);
pdfInput.setPageCount(pageCount);
PdfResponse pdfResponse = pdf.process();
try {
FileUtils.writeByteArrayToFile(new File(DynamicPdfCloudApiExamples.OUTPUT_PATH + "/" + outputFile), pdfResponse.getContent());
} catch (IOException e) {
e.printStackTrace();
}
}
public static function Run(string $apikey, string $path, string $output_path)
{
$pdf = new Pdf();
$pdf->ApiKey = $apikey;
$pdf1 = new Pdf();
$pdf1->ApiKey = $apikey;
SplitPdf::Split($pdf, $path, $output_path, 1, 3, "splitpdf-one.pdf");
SplitPdf::Split($pdf1, $path, $output_path, 6, 2, "splitpdf-two.pdf");
}
public static function Split(Pdf $pdf, string $path, string $output_path, int $startPage, int $pageCount, $output_file)
{
$pdfInput = $pdf->AddPdf(new PdfResource($path . "pdfnumberedinput.pdf"));
$pdfInput->StartPage = $startPage;
$pdfInput->PageCount = $pageCount;
$response = $pdf->Process();
if($response->IsSuccessful)
{
file_put_contents($output_path . $output_file, $response->Content);
} else {
echo("Error: ");
echo($response->StatusCode);
echo($response->ErrorJson);
}
}
func main() {
pr := endpoint.NewPdf()
pr.Endpoint.ApiKey = apiKey
pdfResource := resource.NewPdfResourceWithResourcePath(basePath+"pdfnumberedinput.pdf", "pagenumberedinput1.pdf")
prInput := input.NewPdfWithResource(pdfResource)
prInput.StartPage = 1
prInput.PageCount = 3
pr.Inputs = append(pr.Inputs, prInput)
pdfResource2 := resource.NewPdfResourceWithResourcePath(basePath+"pdfnumberedinput.pdf", "pagenumberedinput2.pdf")
prInput2 := input.NewPdfWithResource(pdfResource2)
prInput2.StartPage = 6
prInput2.PageCount = 2
pr.Inputs = append(pr.Inputs, prInput2)
resp := pr.Process()
res := <-resp
if res.IsSuccessful() == false {
if res.ClientError() != nil {
fmt.Print("Failed with error: " + res.ClientError().Error())
} else {
fmt.Print("Failed with error: " + res.ErrorJson())
}
} else {
os.Remove(outputPath)
os.WriteFile(outputPath,
res.Content().Bytes(), os.ModeType)
}
}
def split_pdf(apikey, full_path):
pdf=Pdf()
pdf.api_key=apikey
split(pdf, full_path, 1, 3, "splitpdf-one.pdf")
split(pdf, full_path, 6, 2, "splitpdf-two.pdf")
response = pdf.process()
def split(pdf, full_path, startPage, pageCount, outputFile):
inputA = pdf.add_pdf(PdfResource(full_path + "pdfnumberedinput.pdf"))
inputA.start_page = startPage
inputA.page_count = pageCount
response = pdf.process()
if response.is_successful:
with open(output_path + outputFile, "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)
print(response.error_message)
print(response.error_json)
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.