Move or Rearrange PDF Pages in Linux

PSPDFKit Processor has been deprecated and replaced by Document Engine. To migrate to Document Engine and unlock advanced document processing capabilities, refer to our migration guide. Learn more about these enhancements on our blog.

PSPDFKit Processor can move existing pages of a document. You can achieve this by sending a multipart request to the /build endpoint and attaching the input file(s) and the instructions JSON.

This guide shows examples of moving or rearranging PDF pages.

Before you get started, make sure Processor is up and running.

You can download and use either of the following sample documents for the examples in this guide:

You’ll be sending multipart POST requests(opens in a new tab) with instructions to Processor’s /build endpoint. To learn more about multipart requests, refer to our blog post on the topic, A Brief Tour of Multipart Requests.

Check out the API Reference to learn more about the /build endpoint and all the actions you can perform on PDFs with PSPDFKit Processor.

Reordering Pages of a File on Disk

You can rearrange pages of the same file, as well as pages from different files.

Reordering Pages of the Same File

To switch places of the third page with the sixth page, send a multipart request with these inputs to the /build endpoint:

Terminal window
curl -X POST http://localhost:5000/api/build \
-F document=@/path/to/document.pdf \
-F instructions='{
"parts": [
{
"file": "document",
"pages": {
"start": 0,
"end": 1
}
},
{
"file": "document",
"pages": {
"start": 5,
"end": 5
}
},
{
"file": "document",
"pages": {
"start": 3,
"end": 4
}
},
{
"file": "document",
"pages": {
"start": 2,
"end": 2
}
},
{
"file": "document",
"pages": {
"start": 6,
"end": 7
}
}
]
}' \
-o result.pdf

Reordering Pages of Different Files

Use the code example below to create a new PDF document with the following content from two other documents, each with eight pages:

  • First four pages of the first document
  • Last four pages of the second document
  • First four pages of the second document
Terminal window
curl -X POST http://localhost:5000/api/build \
-F document1=@/path/to/document1.pdf \
-F document2=@/path/to/document2.pdf \
-F instructions='{
"parts": [
{
"file": "document1",
"pages": {
"start": 0,
"end": 3
}
},
{
"file": "document2",
"pages": {
"start": 4,
"end": 7
}
},
{
"file": "document2",
"pages": {
"start": 0,
"end": 3
}
}
]
}' \
-o result.pdf

Reordering Pages of a File from a URL

To specify the file input with a URL, use the following code:

Terminal window
curl -X POST http://localhost:5000/api/build \
-F instructions='{
"parts": [
{
"file": {
"url": "http://pspdfkit.com/guides/processor/files/8-page-example-document.pdf"
},
"pages": {
"start": 0,
"end": 1
}
},
{
"file": {
"url": "http://pspdfkit.com/guides/processor/files/8-page-example-document.pdf"
},
"pages": {
"start": 5,
"end": 5
}
},
{
"file": {
"url": "http://pspdfkit.com/guides/processor/files/8-page-example-document.pdf"
},
"pages": {
"start": 3,
"end": 4
}
},
{
"file": {
"url": "http://pspdfkit.com/guides/processor/files/8-page-example-document.pdf"
},
"pages": {
"start": 2,
"end": 2
}
},
{
"file": {
"url": "http://pspdfkit.com/guides/processor/files/8-page-example-document.pdf"
},
"pages": {
"start": 6,
"end": 7
}
}
]
}' \
-o result.pdf