After rotating, my code still reads the old page width and height. Why?
Because the tool stores the angle as the page's rotation value and does not rearrange the content, so the MediaBox recorded in the file keeps its original width and height. Viewers apply the rotation only when displaying. In your code, read the rotation value too, and swap width and height when it is 90 or 270.
What exactly changed in the file
Every PDF page carries a box that defines its size, called the MediaBox, and a separate value called Rotate. Rotate PDF pages touches only the second: it adds your angle to the existing Rotate value and stores the result between 0 and 359. The content and the box do not change. That is what keeps the page's quality intact, since nothing is redrawn, but it means anything reading the box alone sees the old dimensions.
A portrait A4 page: the MediaBox is about 595 wide and 842 high in points, with a rotation value of 0.
After 'Right 90°' the rotation value is 90, and the MediaBox is still 595 wide and 842 high.
A PDF reader displays it as a landscape page, 842 wide and 595 high.
Reading the displayed size in code
The rule: the displayed size is the box size, with width and height swapped when the rotation value is 90 or 270. In pdf-lib, for example:
const { width, height } = page.getSize();
const angle = page.getRotation().angle;
const turned = angle % 180 !== 0;
const shown = turned ? { width: height, height: width } : { width, height };
In pdf.js, getViewport applies the page rotation for you, so the width and height it returns are the displayed ones. The problem usually shows up when code reads the box directly, or when another library works out text positions from the raw box dimensions.
Common mistakes after rotating
- Placing a stamp or page number at the 'bottom right' using box coordinates: on a page with a value of 90 the stamp shows up on a different edge entirely when displayed. Work out the position after applying the rotation.
- Sorting pages into portrait and landscape by comparing width and height alone: a page with a portrait box and a rotation of 90 displays as landscape.
- Assuming the rotation is only 0 or 90: it can be 180 or 270, and the tool stores any multiple of ninety.
- Ignoring inherited rotation: some files set the rotation value higher up than the page itself. Good libraries resolve it for you, so use their functions rather than reading the field by hand.
If you really need a box with new dimensions
This tool does not do that; it is designed to change orientation without touching content. Some older systems do not respect the rotation value, and if yours is one of them, the answer is to handle rotation in code as in the example, not to expect the file to change its dimensions.
Common questions
Does the tool change the CropBox too?
No. The tool changes only the rotation value, and every page box stays as it was.
Why does the tool not redraw the page at new dimensions?
Because redrawing can affect content quality and whether text stays selectable. Changing the rotation value gives the same result on screen with no loss.
