PDF teasers – how would you handle this stack problem?

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?

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>