Deskew Scanned PDF
Operator: Abdulla Al Fahad. How tools are built and checked
A sheet that goes through a feeder at a slight angle, or a page photographed by hand, reads fine but looks careless and breaks OCR, which expects text lines to run level. Rotating pages one at a time in a PDF editor is tedious when a 60-page scan has a different tilt on every sheet.
This page measures the tilt of every page from its text lines, turns it back to level, stands sideways and upside-down pages upright, and shows each page before and after so you can overrule the measurement.
Key features
- Skew search within 5 to 15 degrees either side (default 15), measured in 0.05 degree steps, with the angle shown for every page
- Orientation detection that turns sideways pages by 90 or 270 degrees and upside-down pages by 180, and says so when it cannot tell
- Optional dark-border crop, removal of small specks, contrast stretch, and colour, grayscale or black-and-white output
- Output at 150, 200 (default) or 300 dpi with a JPEG quality slider from 40 to 95 (default 80), with each page keeping its original page size (trimmed by the same fraction when borders are cropped)
How skew and orientation are measured
Each page is drawn with pdf.js at the chosen dpi, but the measurement runs on a copy whose long side is 1,200 pixels. The copy is turned into black ink on white with an adaptive threshold, and the ink points are projected onto the vertical axis at trial angles. When the text lines are level the projection has sharp peaks and deep valleys, so the angle whose projection has the largest sum of squares wins; a coarse sweep is followed by two finer passes down to 0.05 degree.
A measurement counts as found only when its peak is clearly higher than the average over all angles and the winner is not at the edge of the search range, because an edge result usually means the true tilt is larger than the range. Pages with too little text, such as photos or blank sheets, are left unrotated and marked 'not enough text to measure'.
Orientation uses two tests. First the page is compared with itself turned 90 degrees to see which direction the lines run. Then lines vote on up or down: in Latin text most letters sit on the baseline and many reach up (b, d, f, h, k, l, t, capitals) while few reach below, so a line has more ink above its dense band than below it. A verdict needs at least 6 voting lines and a 2 to 1 majority.
How it works
Rendering, analysis and the pixel clean-up run in a worker, so the page stays usable and Cancel works between pages. Each result keeps a small before and after thumbnail for the review grid and the corrected page as a JPEG for the final file.
Building the PDF places each corrected image on a page of the original size. When a dark border was cropped, the page is shortened by the same fraction as the image, so the picture is never stretched. A manual angle or turn you enter on a page re-runs just that page.
Page images from a scan are already raster, so the output is image-only: text in it cannot be selected until you run it through Searchable PDF.
Common use cases
- A school secretary scans 150 pupil forms through a sheet feeder and straightens the batch in one run before filing (the limit is 300 pages).
- A solicitor photographs a 12-page deed with a phone on a desk, and every page comes back level with its dark desk borders cropped. Straight lines that were converging in the photo, because the phone was held at an angle, stay converging; see the limits below.
How to use this tool
- Drop the scanned PDF; password-protected files need Unlock PDF first.
- Choose the resolution, colour mode, JPEG quality and the clean-up options, and keep orientation detection on for mixed stacks.
- Press Straighten all pages and watch the progress; Cancel stops after the page in hand.
- Review each page, type a different angle or turn where needed and press Apply, then press Download PDF.
Example
An illustrative 3-page contract scan, generated for this page by rotating clean pages by known amounts: page 1 by 2.5 degrees, page 2 by -4 degrees, page 3 upside down and rotated 1.5 degrees
200 dpi, grayscale, orientation detection on, search range 15 degrees, Crop dark scanner borders off, then Straighten all pagesWhat the page reports for each page
Page 1: 2.50 degrees. Page 2: -4.00 degrees. Page 3: 1.50 degrees, turned 180 ('The page is upside down: turned 180 degrees'). The tilt was injected exactly here, so the measurements match it; on a real scan the angle is an estimate from the text lines, and the review grid is where you check it. The Download button gives scan-deskewed.pdf of 3 pages.Tips for best results
- Switch on 'Crop dark scanner borders' only for scans with a black margin; on white scans it has nothing to remove.
- Use black and white for text-only pages and grayscale for pages with stamps or pencil notes, which a hard black-and-white threshold can lose.
- Narrow the search range to 5 degrees when you know the sheets are only slightly off; it rules out a far-off wrong match on pages with few lines, at the cost of missing a sheet tilted more than that.
Common pitfalls
Rotation cannot fix keystone distortion, the trapezoid look of a page photographed from an angle with a phone. The text lines come out level on average, but the page edges stay slanted and one side of the page stays larger than the other. For photos, hold the phone square above the page, or use a perspective tool first.
Pages that are mostly a photo, a drawing or a table without text lines may not measure; they are left as they are, and you can type the angle yourself.
Orientation detection assumes Latin-style text. A page in Arabic, Chinese or Hebrew may be left alone or turned wrongly, so check it in the review grid.
The limits are 50 MB and 300 pages. A page rendered at 300 dpi is large in memory, so a very large sheet such as an A0 plan is better at 150 dpi.
Common errors and how to fix them
- "Upright or upside down could not be told, so the page was left as it is."
- There were too few text lines to vote. Set the Turn box to 180 (or 90 or 270) on that page and press Apply if it is wrong.
- "The text direction could not be told from the page."
- The page has no clear line structure, for example a photograph. Leave it, or enter the angle by hand.
Reading the angle sign
A positive angle means the text ran downhill to the right and the page was turned counter-clockwise to level it. To override, type the angle you want removed in the Angle box and press Apply; the thumbnail updates before you save anything.
What to do after saving
The saved PDF holds page images only. Add a text layer with Searchable PDF, or use Rotate PDF if whole pages only need a quarter turn.
Frequently asked questions
Does the corrected PDF keep my page sizes?
Yes, unless you crop dark borders. Without the crop each page keeps its original size. With it, the page is made smaller by the same fraction as the cropped image, so the result keeps its proportions and nothing is stretched, but the sheet is no longer a full Letter or A4 page.
Why is a page marked 'not enough text to measure'?
The page had too few text lines for the peaks to stand out, so the page was left level as scanned. Type an angle in the Angle box if you can see it is tilted.
Does deskewing change the text?
The page is rotated by resampling, which softens fine edges slightly. At 200 dpi and above ordinary 10-point text stays readable and OCR-friendly.