Using GhostScript to get page size

前端 未结 3 1256
不知归路
不知归路 2020-12-01 12:48

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)

3条回答
  •  Happy的楠姐
    2020-12-01 13:34

    A solution in pure GhostScript PostScript, no additional scripts necessary:

    gs -dQUIET -sFileName=path/to/file.pdf -c "FileName (r) file runpdfbegin 1 1 pdfpagecount {pdfgetpage /MediaBox get {=print ( ) print} forall (\n) print} for quit"

    The command prints the MediaBox of each page in the PDF as four numbers per line. An example from a 3-page PDF:

    0 0 595 841
    0 0 595 841
    0 0 595 841
    

    Here's a breakdown of the command:

    FileName (r) file  % open file given by -sFileName
    runpdfbegin        % open file as pdf
    1 1 pdfpagecount { % for each page index
      pdfgetpage       % get pdf page properties (pushes a dict)
      /MediaBox get    % get MediaBox value from dict (pushes an array of numbers)
      {                % for every array element
        =print         % print element value
        ( ) print      % print single space
      } forall
      (\n) print       % print new line
    } for
    quit               % quit interpreter. Not necessary if you pass -dBATCH to gs
    

    Replace /MediaBox with /CropBox to get the crop box.

提交回复
热议问题