Search code examples
pdforientationpdftk

How to find PDF page orientation with 'pdftk'?


I need to find PDF page orientations with pdftk.

I need to rotate landscape pages to portrait.

It will be good if I get to know to find page height and width so that I can rotate files.

  • Can I find this by using pdftk?
  • Or can I find this in the PDF source code?
  • Are there other methods to acquire this information?

Solution

  • First:

    • You cannot use pdftk to query PDF for its pages' orientation.
    • But you can use pdfinfo instead to find out which pages are landscape and which are portrait.
    • After you know, use pdftk to rotate that selection of pages which you want rotated into whichever direction you need.

    Second: Understanding pdfinfo

    1. Use -f N -l M to query for information about a certain page range, here pages N-M.
    2. Use -box to get all the different *Box dimensions which may or may not be explicitely defined for the PDF pages: /MediaBox (MUST be present), /CropBox (optional), /ArtBox (optional), /TrimBox (optional) and /BleedBox (optional). (pdfinfo assumes and reports identical values to the /MediaBox for all optional boxes if they are not explicitely defined.)
    3. Be aware that a /CropBox, if defined, limits the area of the complete page which is displayed in a PDF viewer or which is printed on paper. You can have a portrait A3 page (MediaBox) where the CropBox limits your view to a landscape-looking, A5-sized clipping only!
    4. Be aware that an internal page /Rotation key inserted into the PDF source code may tell the viewer to display the page differently from how it is defined.

    Third: Using pdfinfo

    Here is an example for running pdfinfo against a real-world PDF which uses different page sizes, *boxes and rotations, extracting the page size details about the range 3-5:

    $ pdfinfo -box -f 3 -l 5 sample.pdf
      Title:          sample.pdf
      Author:         SYSTEM
      Creator:        Adobe Acrobat 9.3.2
      Producer:       GPL Ghostscript 8.71
      CreationDate:   Sat Jun  5 00:55:42 2010
      ModDate:        Sat Jun  5 00:55:42 2010
      Tagged:         no
      UserProperties: no
      Suspects:       no
      Form:           none
      JavaScript:     no
      Pages:          8
      Encrypted:      no
      Page    3 size: 612 x 792 pts (letter)
      Page    3 rot:  90
      Page    4 size: 565 x 782 pts
      Page    4 rot:  0
      Page    5 size: 842 x 1191 pts (A3)
      Page    5 rot:  270
      Page    3 MediaBox:     0.00     0.00   612.00   792.00
      Page    3 CropBox:      0.00     0.00   612.00   792.00
      Page    3 BleedBox:     0.00     0.00   612.00   792.00
      Page    3 TrimBox:      0.00     0.00   612.00   792.00
      Page    3 ArtBox:       0.00     0.00   612.00   792.00
      Page    4 MediaBox:     0.00     0.00   595.00   842.00
      Page    4 CropBox:     10.00    20.00   575.00   802.00
      Page    4 BleedBox:    10.00    20.00   575.00   802.00
      Page    4 TrimBox:     10.00    20.00   575.00   802.00
      Page    4 ArtBox:      10.00    20.00   575.00   802.00
      Page    5 MediaBox:     0.00     0.00   842.00  1191.00
      Page    5 CropBox:      0.00     0.00   842.00  1191.00
      Page    5 BleedBox:     0.00     0.00   842.00  1191.00
      Page    5 TrimBox:      0.00     0.00   842.00  1191.00
      Page    5 ArtBox:       0.00     0.00   842.00  1191.00
      File size:      28947 bytes
      Optimized:      yes
      PDF version:    1.4
    

    I recommend you to study above output very closely. Don't miss the lines which state the rot: (page rotation) angle. See also the possible differences in what is given as size: and what is given for MediaBox.