# Rotate PDF pages permanently

**Rotate Left** and **Rotate Right** in SumatraPDF turn the pages on screen only. To save rotated pages in the PDF file, so every viewer shows them turned, run a short script with `sumatrapdf-tool run`.

**Available in [pre-release 3.7](https://www.sumatrapdfreader.org/prerelease)**

**To fix sideways pages for good, run `rotate-pages.js` (below) on the PDF.** At a glance:

- **Rotate the view:** `[` / `]`, **View → Rotate Left / Rotate Right**. The file is not changed.
- **Rotate all pages in the file:** `sumatrapdf-tool run rotate-pages.js in.pdf out.pdf 90`
- **Rotate some pages:** `sumatrapdf-tool run rotate-pages.js in.pdf out.pdf -90 2,5-7`
- **Turn upside-down back sides of a scan:** `sumatrapdf-tool run rotate-pages.js in.pdf out.pdf 180 even`

## Rotate the view in SumatraPDF

Use any of these:

- **View → Rotate Left** or **View → Rotate Right**
- the **Rotate Left** / **Rotate Right** toolbar buttons
- `[` rotates left (counterclockwise), `]` rotates right (clockwise); `Ctrl + Shift + -` and `Ctrl + Shift + +` do the same, and so do `/` and `*` on the numeric keypad
- `Ctrl + K`, `Rotate Left` or `Rotate Right` in [Command Palette](Command-Palette.md) (`CmdRotateLeft`, `CmdRotateRight`)

Each press turns all pages of the document by 90 degrees. This only changes how SumatraPDF shows the document:

- The PDF file is not changed. Other PDF viewers, and other people you send the file to, see the original orientation.
- SumatraPDF remembers the rotation for that file and uses it next time you open it. It is saved as `Rotation` in `FileStates` in the [settings](Advanced-options-settings.md), when `RememberStatePerDocument` is `true` (the default).
- Printing pages doesn't use it. To turn the printout, use **Rotate printout** in the print dialog, see [Printing](Printing.md).

## Rotate pages in the PDF file

1. Save the script below as `rotate-pages.js`.
2. Run it with the angle and the pages to rotate:

```
sumatrapdf-tool run rotate-pages.js in.pdf out.pdf 90
```

`out.pdf` is a copy of `in.pdf` with the pages turned. [SumatraPDF.exe run](Tools.md) with the same arguments works the same.

```js
// Rotate pages of a PDF permanently.
// Usage: sumatrapdf-tool run rotate-pages.js in.pdf out.pdf [angle] [pages]
// e.g.   sumatrapdf-tool run rotate-pages.js in.pdf out.pdf 90 1,3-5,N

// ---- Settings (used when not given on the command line) ----
var ANGLE = 90      // clockwise: 90, 180 or 270; -90 turns left
var PAGES = "1-N"   // e.g. "2", "1,3-5", "4-N" (N is the last page), "odd", "even"
// ------------------

if (scriptArgs.length < 2) {
	print("usage: sumatrapdf-tool run rotate-pages.js in.pdf out.pdf [angle] [pages]")
	quit(1)
}
if (scriptArgs.length > 2) ANGLE = parseInt(scriptArgs[2])
if (scriptArgs.length > 3) PAGES = scriptArgs[3]

var doc = Document.openDocument(scriptArgs[0])
var n = doc.countPages()

// turn "1,3-5,N" into [1, 3, 4, 5, n]
function parsePages(spec) {
	var pages = []
	spec.split(",").forEach(function (part) {
		if (part == "odd" || part == "even") {
			for (var p = part == "odd" ? 1 : 2; p <= n; p += 2) pages.push(p)
			return
		}
		var r = part.replace(/N/g, n).split("-")
		for (var p = parseInt(r[0]); p <= parseInt(r[r.length - 1]); p++) pages.push(p)
	})
	return pages
}

parsePages(PAGES).forEach(function (p) {
	if (p < 1 || p > n) return
	var pageObj = doc.loadPage(p - 1).getObject()
	var old = pageObj.getInheritable("Rotate")
	var rotate = (old ? old.valueOf() : 0) + ANGLE
	pageObj.put("Rotate", ((rotate % 360) + 360) % 360)
})

doc.save(scriptArgs[1], "garbage,compress")
```

The arguments after `out.pdf` are optional:

- **angle:** `90` turns clockwise (right), `-90` or `270` counterclockwise (left), `180` upside down. Use a multiple of 90; PDF doesn't allow other angles.
- **pages:** page numbers and ranges, separated by commas, with no spaces: `3`, `1,4-6`, `10-N` (`N` is the last page), `odd` or `even`. Leave it out to rotate every page.

Without arguments, the script uses `ANGLE` and `PAGES` from the top of the script.

Examples:

| You want                                  | Command                                                         |
| ----------------------------------------- | --------------------------------------------------------------- |
| Every page to the right                   | `sumatrapdf-tool run rotate-pages.js in.pdf out.pdf 90`         |
| Page 3 to the left                        | `sumatrapdf-tool run rotate-pages.js in.pdf out.pdf -90 3`      |
| Pages 2, 5 to 8 and the last page         | `sumatrapdf-tool run rotate-pages.js in.pdf out.pdf 90 2,5-8,N` |
| Upside-down back sides of a two-sided scan | `sumatrapdf-tool run rotate-pages.js in.pdf out.pdf 180 even`   |

The script sets the rotation (`/Rotate`) of each page, adding to the rotation the page already has. The page content is not touched: text, links, bookmarks, annotations and form fields stay as they are, and the file size stays about the same.

## Rotate with sumatrapdf-tool draw

`sumatrapdf-tool draw` can also rotate, without a script:

```
sumatrapdf-tool draw -R 90 -o out.pdf in.pdf
```

It draws the pages again into a new PDF and rotates every page it writes; with a page list (`... in.pdf 2-4`) it writes only those pages. Bookmarks and links are lost, and the file can get much bigger. Use `rotate-pages.js` instead when you can. See [sumatrapdf-tool draw](Tool-draw.md).

Note: `merge` and `clean` have no rotate option.

## Tips

- Write to a new file. Saving over the input file fails with `cannot remove file`.
- Rotating pages changes the file, so it breaks existing digital signatures. Rotate before you sign.
- To rotate many files, run the script in a loop: see [Batch process PDFs](Batch-process-PDFs.md).

## See also

- [Reorder PDF pages](Reorder-PDF-pages.md) — change the order of pages
- [Crop PDF margins](Crop-PDF-margins.md) — another page-level fix for scans
- [Keyboard shortcuts](Keyboard-shortcuts.md)
- [sumatrapdf-tool run](Tool-run.md) — how scripts run
- [All cmd-line tools](Tools.md)
