Sort, Delete & Rotate PDF Pages Via API

API
Image by http://bit.ly/2ejxaXS

Not only for developers: edit PDF files using the Online-Convert.com API.

Our API is versatile and reliable when it comes to file conversion, but there is so much more. Using the API, you can split & merge PDF documents as well as add password protection to a PDF. And yes, there is still more. In this article we’ll have a look at how you can edit a PDF document even further:

  • Delete single PDF pages
  • Rotate PDF pages
  • Sort a PDF’s pages

 

Edit PDF Pages Via API

Before you can start editing your PDF using the Online-Convert.com API, however, you need to be familiar with the basics. We have provided a tutorial on how to use the Online-Convert.com API using postman you can check out. There you will find an introduction to postman as well as a collection file you will need.

A video tutorial on how to use the online-convert.com API is also available.

 

If you have understood the basics, you can start editing your PDF with our API. Following, we will provide you with all necessary commands for postman in order to edit your PDF.

The basic command for a simple PDF to PDF conversion we’ll use is the following:

{
    "input": [{
         "type": "remote",
         "source": "https://cdn.online-convert.com/example-file/document/pdf/example.pdf"
     }],
     "conversion": [{
     "target": "pdf",
     "category": "document"
     }]
 }

This POST command will convert the remote example file into another PDF. With the addition of the following command, we can add the options for deleting, rotating or sorting the pages of a PDF:

"options": {
}

 

Rotate Pages Of A PDF

To rotate one or more pages of a PDF document, a command following this scheme has to be used:

"rotate": {
    "type": "array",
    "additionalProperties": false,
    "pages": {
        "type": "string",
        "pattern": "^(((\\d+|first)-?(\\d*|last))|(first)|(last)|(even)|(odd))(, ?(((\\d+|first)-?(\\d*|last))|(first)|(last)|(even)|(odd)))*$",
        "description": "The page numbers to be rotated. E.g. 5-8 or 5- or 5. It also accepts \"first\" and \"last\" aliases.",
        "default": "1-last"
    },
    "angle": {
        "type": "integer",
        "description": "The angle by which to rotate the pages",
        "enum": [
            0,
            90,
            180,
            270
        ],
        "required": true
    }
}

The rotate function needs, as an input, an array of values for “pages” and “angle”. The value for “angle” should be an integer of the list given below. The numbers given their define the degree of rotation (clockwise).

While the latter is quite easily explained, the values for pages require some additional explanation. The value is given in the form of numbers that indicate which pages should be rotated.

  • Single pages can be listed with a comma in between (e.g. 3,5,6)
  • A span of pages can be indicated by giving the first and last page separated by a hyphen (e.g. 3-6)
  • The last pages of a document can be rotated by giving the first page that should be rotated and a hyphen. (e.g. 3-)
  • One can also specify to rotate the first or last page (input first or last), or all even or odd numbered pages (input even or odd)

 

A complete command should look like the following example:

{
    "input": [{
        "type": "remote",
        "source": "https://cdn.online-convert.com/example-file/document/pdf/example_multipage.pdf"
    }],
    "conversion": [{
        "target": "pdf",
        "category": "document",
        "options": {
            "rotate": [{
                "pages": "3,5-",
                "angle": 180
            }]
        }
    }]
}

This API call creates a PDF file where the 3rd page as well as the 5th and all following pages are rotated by 180°.

 

Of course you could use several arrays for “rotate” in one call to rotate different pages by different degrees. A call for this could look like this:

{
    "input": [{
        "type": "remote",
        "source": "https://cdn.online-convert.com/example-file/document/pdf/example_multipage.pdf"
    }],
    "conversion": [{
        "target": "pdf",
        "category": "document",
        "options": {
            "rotate": [{
                "pages": "odd",
                "angle": 180
            }],
            "rotate": [{
                "pages": "even",
                "angle": 90
            }]
        }
    }]
}

With this call, all odd numbered pages of the PDF will be rotated by 180°, while all even numbered pages are rotated by 90°.

 

Sort The Pages Of A PDF

The pages of a PDF can easily be rearranged or sorted using the following scheme:

"rearrange": {
    "type": "string",
    "pattern": "^(((\\d+|first)-?(\\d*|last))|(first)|(last)|(even)|(odd))(, ?(((\\d+|first)-?(\\d*|last))|(first)|(last)|(even)|(odd)))*$",
    "description": "The page numbers that should be present in the converted file, in the desired order. This is also used to delete pages or extract a set of pages."
}

The “rearrange” option takes a string as an input which should consist of the page number in the order in which they should be sorted in the output PDF. This string can contain the following values:

  • The page numbers in the order in which the pages should appear, separated by comma (e.g. 2,3,4,1,5,6,7)

The other values mentioned in the scheme above will be ignored for now.

 

The following shows a complete POST command for rearranging PDF pages:

{
    "input": [{
        "type": "remote",
        "source": "https://cdn.online-convert.com/example-file/document/pdf/example_multipage.pdf"
    }],
    "conversion": [{
        "target": "pdf",
        "category": "document",
        "options": {
            "rearrange": "2,3,4,1,5,6,7"
        }
    }]
}

Basically, this call moves the first page of our document in between pages 4 and 5.

 

Delete Single Pages Of A PDF

Deleting certain pages in a PDF is easy and uses the exact same command as the rearrange function.

"rearrange": {
    "type": "string",
    "pattern": "^(((\\d+|first)-?(\\d*|last))|(first)|(last)|(even)|(odd))(, ?(((\\d+|first)-?(\\d*|last))|(first)|(last)|(even)|(odd)))*$",
    "description": "The page numbers that should be present in the converted file, in the desired order. This is also used to delete pages or extract a set of pages."
}

 

Again, you can specify a string of numbers, however they will serve an additional purpose other than rearranging the pages: all page numbers left out will be deleted from the resulting document.

Furthermore, you can use the following strings:

  • First: deletes all pages but the first page of the document
  • Last: deletes all pages but the last page of the document
  • Odd: will preserve all odd numbered pages
  • Even: will preserve all even numbered pages

 

With this in mind, a complete API call for the deletion of certain pages in a PDF could look like this:

{
    "input": [{
        "type": "remote",
        "source": "https://cdn.online-convert.com/example-file/document/pdf/example_multipage.pdf"
    }],
    "conversion": [{
        "target": "pdf",
        "category": "document",
        "options": {
            "rearrange": "1,2,3,5,7"
        }
    }]
}

The PDF created using this call will only contain the odd pages and page number 2 of the original document.

 

Sort & Delete PDF Pages In One Step

Due to the nature of the rearrange function, incorporating the delete function, it’s easy to sort and delete pages in a PDF with just one command. Take the following example:

{
    "input": [{
        "type": "remote",
        "source": "https://cdn.online-convert.com/example-file/document/pdf/example_multipage.pdf"
    }],
    "conversion": [{
        "target": "pdf",
        "category": "document",
        "options": {
            "rearrange": "7,6,5,4,1"
        }
    }]
}

The API call above will sort the pages of the PDF according to the array of numbers, as well as delete the missing pages (3 and 2).

 

Got any questions?

If you still have questions about how to use Online-Convert.com’s API to edit your PDFs, drop us a message. A friendly and competent support team will make sure that you will be able to do your conversions just like you wanted to.

 

Further Information