First, be aware that not every single page from a multi-page PostScript file will show the exact same "bounding box" values (in fact, this is rather rare). So you probably want to find out the common denominator across all possible bounding boxes (which would include them all).
Second, what you see in the console window when you run gs -sDEVICE=bbox
is a mix of stdin and stdout output channels. However, the info you're after is going to stderr. If you redirect the command output to a file, you're capturing stdout, not stderr! To suppress some of the version and debugging info going to stderr add -q
to the commandline.
So in order to get a 'clean* output of the bounding boxes for all pages, you have to re-direct the stderr channel first, which you then capture in file info.txt. So run a command like this (or similar):
gs \
-dBATCH \
-dNOPAUSE \
-q \
-sDEVICE=bbox \
example.ps \
2>&1 \
| tee info.txt
or even this, should you not need the info about the HiResBoundingBox
:
gs \
-dBATCH \
-dNOPAUSE \
-q \
-sDEVICE=bbox \
example.ps \
2>&1 \
| grep ^%%Bound \
| tee info.txt
Also, BTW, note that can determine the bounding boxes of PostScript as well as PDF input files.
This should give you output like the following, where each line represents a page of the input file, starting with page 1 on the first line:
%%BoundingBox: 36 18 553 802
%%BoundingBox: 37 18 553 804
%%BoundingBox: 36 18 553 802
%%BoundingBox: 37 668 552 803
%%BoundingBox: 40 68 532 757
Lastly, you might want to read up in the following answers for some background info about Ghostscript's bbox
device. You'll also find some alternative PostScript code for the cropping job there: