Example of inspecting PDF files
This page applies to Harlequin v13.1r0 and later; and to Harlequin Core but not Harlequin MultiRIP.
This code listing gives a simple example of how to use the PDF inspection operators to obtain a page count from a PDF file.
As customary in examples, there is limited error handling, and some assumptions are made. For example, /Count
might be indirect. This possibility is remote, but should be anticipated in production code, by checking the type of object returned.
% Example of use for the getPDFtrailer and getPDFobject
% routines in the RIP. Reports the total number of pages
% in the PDF file "example.pdf", without customary error checks.
2 dict begin
% Open the file
(example.pdf) (r) file /PDFFile exch def
% Create the PDF execution context
PDFFile << >> pdfopen /PDFContextID exch def
% Get the trailer object
PDFContextID 1183615869 internaldict /getPDFtrailer get exec
% Should have a /Root object
/Root get
PDFContextID exch 1183615869 internaldict /getPDFobject get exec
% Got the Catalog object, now look for /Pages
/Pages get
PDFContextID exch 1183615869 internaldict /getPDFobject get exec
% This top-level Pages object has the page count for
% the entire PDF file
/Count get
(PDF contains ) print 128 string cvs print ( pages) =
% Be sure to close the execution context and the physical file
PDFContextID pdfclose PDFFile closefile
currentdict end