This article arose as a result of debugging a customer file which was not displaying properly. There are many PDF files out there which do not actually meet the spec so we spend a lot of time tweaking our library to allow for all these ‘interesting’ cases.
The PDF file format has a stack system so that you can save the current graphics Status, make some changes and restore it later. In the PDF stream you will see this with the Q/q command. Here is an example
q //save stack 1 0 0 1 130.32 117.601 cm /X7 Do //draw an image or execute some commands Q //restore stack
This code saves the stack, makes a change to the co-ordinates, does something and then restores original values.
It can even nest calls so you can have
q //save orig state //something q //save new state //something Q //restore new state Q //restore orig state
It is a very powerful feature.
The Do command can also call some code commands including saving and restoring the stack like this
q //save stack 1 0 0 1 130.32 117.601 cm /X7 Do //execute these commands q //save state 7.92 0 0 7.92 0 -0.001 cm //move position 0 0 0 rg //set color BI /W 34 /IM true /D [1 0] /BPC 1 /H 34 ID //draw image //end of stream of commands (we pushed value onto stack but did not use) Q //oh dear!!!
What do I do? Do I use value pushed by subroutine or value pushed before the sub-routine
The answer really is whether there is a single, global stack or whether the sub-routine has its own stack….
Update: The answer is to that the sub-routine effectively has its own stack so any values left should be ignored. Did you get it right?
Are you a Developer working with PDF files?
Our developers guide contains a large number of technical posts to help you understand the PDF file Format.
Find out more about our software for Developers
|Convert PDF to HTML5 or SVG|
|Convert AcroForms and XFA to HTML5|
|Java PDF SDK for working with PDF files|