Move or Rearrange PDF Pages

Document Engine can move existing pages of a document. You can achieve this by sending a multipart request to the /api/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 Document Engine is up and running.

You’ll be sending multipart POST requests with instructions to Document Engine’s /api/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 /api/build endpoint and all the actions you can perform on PDFs with Document Engine.

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 /api/build endpoint:

curl -X POST http://localhost:5000/api/build \
  -H "Authorization: Token token=<API token>" \
  -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 /api/build HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--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 \
  -H "Authorization: Token token=<API token>" \
  -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 /api/build HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

--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 \
  -H "Authorization: Token token=<API token>" \
  -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 /api/build HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary
Authorization: Token token=<API token>

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