7 Using Ghostscript to get page size

Is it possible to get the page size (from e.g. a PDF document page) using Ghostscript?

I have seen the bbox device, but it returns the bounding box (it differs per page), not the TrimBox (or CropBox) of the PDF pages. ( See Prepressure website for info about page boxes.) Any other possibility?

7.1 Answer 1

Unfortunately it doesn’t seem quite easy to get the (possibly different) page sizes (or *Boxes for that matter) inside a PDF with the help of Ghostscript.

But since you asked for other possibilities as well: a rather reliable way to determine the media sizes for each page (and even each one of the embedded {Trim,Media,Crop,Bleed}Boxes) is the commandline tool pdfinfo.exe. This utility is part of the XPDF tools from http://www.foolabs.com/xpdf/download.html. You can run the tool with the -box parameter and tell it with -f 3 to start at page 3 and with -l 8 to stop processing at page 8.

Example output

 1     C:\downloads>pdfinfo -box -f 1 -l 3 _IXUS_850IS_ADVCUG_EN.pdf
 2     Creator:        FrameMaker 6.0
 3     Producer:       Acrobat Distiller 5.0.5 (Windows)
 4     CreationDate:   08/17/06 16:43:06
 5     ModDate:        08/22/06 12:20:24
 6     Tagged:         no
 7     Pages:          146
 8     Encrypted:      no
 9     Page    1 size: 419.535 x 297.644 pts
10     Page    2 size: 297.646 x 419.524 pts
11     Page    3 size: 297.646 x 419.524 pts
12     Page    1 MediaBox:     0.00     0.00   595.00   842.00
13     Page    1 CropBox:     87.25   430.36   506.79   728.00
14     Page    1 BleedBox:    87.25   430.36   506.79   728.00
15     Page    1 TrimBox:     87.25   430.36   506.79   728.00
16     Page    1 ArtBox:      87.25   430.36   506.79   728.00
17     Page    2 MediaBox:     0.00     0.00   595.00   842.00
18     Page    2 CropBox:    148.17   210.76   445.81   630.28
19     Page    2 BleedBox:   148.17   210.76   445.81   630.28
20     Page    2 TrimBox:    148.17   210.76   445.81   630.28
21     Page    2 ArtBox:     148.17   210.76   445.81   630.28
22     Page    3 MediaBox:     0.00     0.00   595.00   842.00
23     Page    3 CropBox:    148.17   210.76   445.81   630.28
24     Page    3 BleedBox:   148.17   210.76   445.81   630.28
25     Page    3 TrimBox:    148.17   210.76   445.81   630.28
26     Page    3 ArtBox:     148.17   210.76   445.81   630.28
27     File size:      6888764 bytes
28     Optimized:      yes
29     PDF version:    1.4

7.2 Answer 2

Meanwhile I found a different method. This one uses Ghostscript only (just as you required). No need for additional third party utilities.

This method uses a little helper program, written in PostScript, shipping with the source code of Ghostscript. Look in the toolbin subdir for the pdf_info.ps file.

The included comments say you should run it like this in order to list fonts used, media sizes used

1     gswin32c -dNODISPLAY   ^
2        -q                  ^
3        -sFile=____.pdf     ^
4         [-dDumpMediaSizes] ^
5         [-dDumpFontsUsed [-dShowEmbeddedFonts]] ^
6         toolbin/pdf_info.ps

I did run it on a local example file, with commandline parameters that ask for the media sizes only (not the fonts used). Here is the result:

 1     C:\> gswin32c    ^
 2          -dNODISPLAY ^
 3          -q          ^
 4          -sFile=c:\downloads\_IXUS_850IS_ADVCUG_EN.pdf ^
 5          -dDumpMediaSizes ^
 6           C:/gs8.71/lib/pdf_info.ps
 7    
 8 
 9      c:\downloads\_IXUS_850IS_ADVCUG_EN.pdf has 146 pages.
10      Creator: FrameMaker 6.0
11      Producer: Acrobat Distiller 5.0.5 (Windows)
12      CreationDate: D:20060817164306Z
13      ModDate: D:20060822122024+02'00'
14      
15      Page 1 MediaBox: [ 595 842 ] CropBox: [ 419.535 297.644 ]
16      Page 2 MediaBox: [ 595 842 ] CropBox: [ 297.646 419.524 ]
17      Page 3 MediaBox: [ 595 842 ] CropBox: [ 297.646 419.524 ]
18      Page 4 MediaBox: [ 595 842 ] CropBox: [ 297.646 419.524 ]
19      [....]