Move or Rearrange PDF Pages in Linux

Information

PSPDFKit Processor has been deprecated and replaced by Document Engine. To start using Document Engine, refer to the migration guide. With Document Engine, you’ll have access to robust new capabilities (read the blog for more information).

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 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:

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
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="document"; filename="document.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json

{
  "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
      }
    }
  ]
}
--customboundary--

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

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
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="document1"; filename="document1.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="document2"; filename="document2.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json

{
  "parts": [
    {
      "file": "document1",
      "pages": {
        "start": 0,
        "end": 3
      }
    },
    {
      "file": "document2",
      "pages": {
        "start": 4,
        "end": 7
      }
    },
    {
      "file": "document2",
      "pages": {
        "start": 0,
        "end": 3
      }
    }
  ]
}
--customboundary--

Reordering Pages of a File from a URL

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

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
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json

{
  "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
      }
    }
  ]
}
--customboundary--