Apache PDFBox - Rotate Pages

πŸ”§ Operation Name

Apache PDFBox - Rotate Pages rotatePages


🧾 Description

Rotates one or more pages in a PDF document clockwise by 90, 180, or 270 degrees. You can optionally limit the rotation to specific pages using a page range.


βœ… Inputs

Parameter
Type
Required
Description

PDF File [Binary]

InputStream (Binary)

βœ…

The PDF file to rotate.

Degrees

Integer

βœ…

The rotation angle: must be one of 90, 180, or 270.

Page Range

String

❌ (Optional)

A comma-separated list of pages and/or ranges to rotate (e.g., 1,3,5-6). If not provided, all pages will be rotated.


πŸ“€ Output

  • Payload: InputStream (Binary) A new binary PDF stream with the specified pages rotated.

  • Attributes: PdfBoxFileAttributes Includes original PDF metadata such as:

    • numberOfPages

    • title, author, etc.


πŸ§ͺ MuleSoft Flow Example

Here’s how to call this operation in a MuleSoft flow:

This example uses both Filter and Rotate as our test pdf is password protected so we use a filter to effectively make a copy


πŸ” Notes

  • Degrees must be one of: 90, 180, or 270. Other values will trigger a validation error.

  • If pageRange is omitted, all pages will be rotated.

  • Rotation is applied clockwise from the current orientation.

  • Useful for correcting scanned documents or preparing them for display.


Underlying Application Interface:

Pseudo Code
Methods used from the Apache PDFBox library
  • org.apache.pdfbox.Loader.loadPDF(byte[] input): Used to load the PDF document from a byte array (Step 3).

  • org.apache.pdfbox.pdmodel.PDDocument.getNumberOfPages(): Used to get the total number of pages in the loaded document (Step 5) and when creating attributes (Step 11).

  • org.apache.pdfbox.pdmodel.PDDocument.getPage(int pageIndex): Used to get a specific page object by its index (Step 8b).

  • org.apache.pdfbox.pdmodel.PDPage.setRotation(int rotation): Used to set the rotation angle for a specific page (Step 8c).

  • org.apache.pdfbox.pdmodel.PDDocument.save(OutputStream output): Used to save the modified document to an output stream (Step 9).

  • org.apache.pdfbox.pdmodel.PDDocument.close(): Used to close the loaded document and release resources (Step 14).

Last updated