Little known “feature” of Optional Content

When Adobe introduced Optional Content (aka “Layers”) into PDF 1.5, they specified a very unusual (and quite unexpected) implementation detail (Section 4.10.2):

Graphics state operations, such as setting the color, transformation matrix, and clipping, are still applied. In addition, graphics state side effects that arise from drawing operators are applied; in particular, the current text position is updated even for text wrapped in optional content. In other words, graphics state parameters that persist past the end of a marked-content section must be the same whether the optional content is visible or not. For example, hiding a section of optional content does not change the color of objects that do not belong to the same optional content group.

This enables a PDF viewer to more easily consume content and process OCGs - since it would do everything it would normally does, except actually draw/render.

However, it makes writing tools that manipulate PDF content (even via Acrobat plugins or the Adobe PDFLibrary) quite difficult since such state attributes need to be maintained along with related objects.

In addition, it also gives PDF creation tools a bit too much freedom in their PDF coding. Take the following piece of content stream that was generated by Adobe InDesign CS (3.0):

/OC /MC0 BDC
q
1 0 0 -1 135 81 cm 0 1 0 0 k 0 i
/GS0 gs
-52.18269 -5.19231 m -88.00961 -5.19231 l
-105.92308 26.48077 l -88.00961 58.15384 l
-52.18269 58.15384 l -34.26923 26.48077 l -52.18269 -5.19231 l
h f
EMC
/OC /MC1 BDC
1 0 0 1 143.65384 1.01373 cm
-52.18269 -5.19231 m -88.00961 -5.19231 l
-105.92308 26.48077 l -88.00961 58.15384 l
-52.18269 58.15384 l -34.26923 26.48077 l -52.18269 -5.19231 l
h f
EMC
Q

Although 100% valid, the poor “scoping” of the q/Q with respect to the BDC/EMC could potentially cause PDF processing solutions no end of trouble. So PLEASE don’t do it!

Just another wonderful “gotcha” in PDF…