Overview

PDF Layers are called Optional Content within the PDF specifications. This is a feature that was added as of PDF-1.5 (Acrobat 6). A layer is identified by an identifier and a title. Only the layer’s title is visible to the end-user of the PDF document. Layers can be structured as a tree or a hierarchy of layers.

Here is a sample Optional Content object in a PDF:

 

9 0 obj <</Type/OCG/Name(Blue Layer)>> endobj

 

Each page using this layer should add the layer to its list of resources, for example:

 

6 0 obj <</Type /Page /Parent 3 0 R /MediaBox [0 0 612 792 ] /Contents [7 0 R ]
/Resources << /ProcSet [/PDF /Text] /Font <</F10 10 0 R >>
/Properties <</OC9 9 0 R >>
>> >> endobj

 

OC9 is the layer ID,(Blue Layer) is the layer title (strings in PDF are surrounded by parenthesis.)

To indicate that various objects within the page content belong to specific layers, the following syntax is used:

 

% start the Blue Layer that contains the text: Page 1
/OC /OC9 BDC
BT /F10 5.76  Tf 1 0 0 1 24 745.56 Tm 0.026  Tc -0.067  Tw(Page 1)Tj ET
% start a sub-layer that will contain a filled rectangle
/OC /OC11 BDC
n 1 g 96 326.96 35.76 -6.6 re f*
% end of sub-layer 1
EMC
% start another sub-layer that will contain another filled rectangle
/OC /OC13 BDC
n 1 g 96 526.96 35.76 -6.6 re f*
% end of sub-layer 2
EMC
%end of blue layer
EMC

 

Although the code shows one parent layer and two nested layers contained within the parent layer, this is not sufficient to define the hierarchy of layers. The Order string is used to determine the hierarchy of layers. The Order string is part of the document Catalog. The Order string for the above PDF sample should look like this:

/Order [ 9 0 R [11 0 R 13 0 R] ]

The object IDs are needed to create a proper order string. The order string can also contain a parent node that is not a layer but only a container for other layers. In the example above we could have all the layers below to one parent layer called(Layered PDF)by using:

/Order [(Layered PDF) 9 0 R [11 0 R 13 0 R]  ]

It is important that the Order string matches the order of layers as they appear in the page content, otherwise the PDF viewer will not be able to properly show and hide layers with their sub-layers.

 

There is an inconsistency in the PDF specifications related to defining nodes that are parents of other layers. To have (Layered PDF) As the parent of layer 9, we do not use [(Layered PDF) [9 0 R] ] as one might expect but [(Layered PDF) 9 0 R] as if the 2 nodes were on the same level.