[Tex/LaTex] Remove only top and bottom margins using pdfcrop

pdfcrop

I usually use pdfcrop to remove white margins of pdf illustrations in LaTeX documents. Using it by command line:

pdfcrop myfigure.pdf

it removes all margins but to avoid me to resize the figure (if it has left and right white margins) I need a way to remove only top and bottom margins. I mean someting like

pdfcrop --margins '- 0 - 0' input.pdf

(where the - should be a way to make pdf crop keep the original margin) to set top and bottom margins to 0 keeping original left and right margins.

Perhaps, if there is a tool to measure the white margins of a (single page) .pdf file, I could write a script to achieve my purpose.

Best Answer

pdfcrop reports the bounding boxes if option --verbose is given, e.g.:

pdfcrop --verbose test.pdf

It reports:

PDFCROP 1.38, 2012/11/02 - Copyright (c) 2002-2012 by Heiko Oberdiek.
[...]
* Running ghostscript for BoundingBox calculation ...
[...]
Page 1
%%BoundingBox: 133 89 308 668
* Page 1: 133 89 308 668
%%HiResBoundingBox: 133.767980 89.369997 307.295991 667.205980
[...]

In this case, the left margin is 133. The right margin can be calculated via the width. The size of the PDF file is reported by pdfinfo (assuming the pages have all the same size), e.g.:

pdfinfo test.pdf

It reports:

[...]
Page size:      595.276 x 841.89 pts (A4) (rotated 0 degrees)
[...]

Then the missing values for --margins can be calculated and added:

pdfcrop --margins '133 0 288.276 0' test.pdf

Alternatively the bounding box option can be used:

pdfcrop --bbox '0 89 595.276 668' test.pdf