Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

PDF teasers – how would you handle this stack problem?

59 sec read

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?

IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Enabling SVG Gzip Compression on Apache and NGINX

Gzip compression is a widely supported method of reducing the size of the content sent from a web server in order to improve the...
Leon Atherton
47 sec read

Leave a Reply

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

IDRsolutions Ltd 2020. All rights reserved.