Amyuni in SD Times Top 100

Jun 19
2009

Amyuni Developer Tools are among a handful of components to win in the Components and Libraries category of the 2009 SD Times 100.

The SD Times 100 recognizes the top innovators and leaders in multiple software development industry areas.

2009sdt100_logo

Learn more about Amyuni Developer Tools

Alex Furness is the Marketing Director for Amyuni Technologies
www.amyuni.com

Creating Layered PDFs with Escape Calls to PDF Converter Printer

Jun 01
2009

Background Information

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 title is visible to the end-user of the PDF document. Layers can be structured as a tree or as 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) and 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 enough 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.

Note: There is an inconsistency in the PDF specifications related to defining nodes that are parents of other layers. To have a 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.

Creating Groups of Layers

The PDF format allows creating groups of mutually exclusive layers where showing one layer will automatically hide the other. These are called radio-button groups and defined using the RBGroups PDF keyword. One can also define which layers are visible by default and which are hidden, for example:

/RBGroups [[ 11 0 R 13 0 R]] /ON[ 11 0 R] /OFF [13 0 R] indicates that layers 11 and 13 are grouped and that layer 11 is visible by default whereas 13 is invisible.

Dynamically Creating Layers with the Amyuni PDF Converter

External developers need to be aware of layer titles only. The layers IDs and object numbers are not significant to the developer, so the implemented API will only use layer titles to insert layers and define the layer hierarchy.

Layers can be inserted within a PDF file while the document is being printed by calling Escape sequences. Escape sequences are GDI artifacts that enable developers to send custom data to a printer driver.

Checking the Printer for PDF Layer Support

The first step to start adding layers is to check that the printer has support for the escape sequences that are defined by Amyuni. The following calls are needed include:

// check if PDF printer supports layers
#define ESCAPE_SETLAYER 248

CHAR technology[4];
int escape = ESCAPE_SETLAYER;
ExtEscape( hDC, GETTECHNOLOGY, 0, NULL, sizeof(technology), (LPSTR)technology
);

// the technology should be PDF
if ( lstrcmp(technology, “PDF”) )
{
MessageBox( 0, “Not an Amyuni PDF driver”, “Error”, MB_ICONERROR );
}

// and support the SETLAYER escape
if ( !ExtEscape( hDC, QUERYESCSUPPORT, sizeof(escape), (LPCSTR)&escape, 0,
NULL ) )
{
MessageBox( 0, “Not an Amyuni PDF driver”, “Error”, MB_ICONERROR );
}

GETTECHNOLOGY and QUERYESCSUPPORT are escape sequences that are predefined by Windows GDI. ESCAPE_SETLAYER is a custom escape that is processed by the Amyuni printer.

The SETLAYER escape takes one parameter which is a Unicode string. The escape can be wrapped into a helper function such as:

void SetLayer( HDC hDC, LPWSTR LayerInfo )
{
switch ( ExtEscape( hDC, ESCAPE_SETLAYER, (int)((wcslen(LayerInfo) + 1)
* sizeof(LayerInfo[0])), (LPCSTR)LayerInfo, 0, NULL ) )
{
default:
// positive return indicates success
case 0:
// no error
return;

case -1:
// error occured, closing a layer when none was open
break;

case -2:
// memory allocation error (low memory or invalid string)
break;
}
}

The same escape call is used to start a layer or sub-layer, end a layer or define the hierarchy of layers.

Setting the Hierarchy of Layers

The order or hierarchy of layers is sent to the PDF printer using the SETLAYER escape before the call to StartDoc. It can actually be called anytime during the printing as long as the call is made outside of a StartPage/EndPage block but before the EndDoc is called. The advantage of setting the hierarchy before StartDoc is that it allows the PDF printer to expect layer support and switch the file header to PDF-1.5.

If the hierarchy of layers is not known before the call the StartDoc, the developer can call SetLayer( L) with an empty string before the call to StartDoc simply to instruct the PDF printer to output PDF-1.5 header. The order string should contain the titles of all the layers and their hierarchy, all in Unicode format. For example:

// set the order and hierrachy of layers
SetLayer( hDC, L“/Order[(Blue Layer)[(Blue Layer – 1)(Blue Layer – 2)](Red
Layer)(Green Layer)]”
);

This will produce a layer structure like this:

layer-structure

To group layers within radio-button groups, the /RBGroups, /ON and /OFF entries should be added to the call above. This code for example will make the red and green layers as part of a group:

// set the order and radio/button groups of layers
SetLayer( hDC, L“/Order[(Blue Layer)[(Blue Layer – 1)(Blue Layer – 2)](Red
Layer)(Green Layer)]”
\
L“/RBGroups[[(Red Layer)(Green Layer)]]/OFF[(Red Layer)]/ON[(Green Layer)]
);

Drawing Objects within Layers

Within a StartPage/EndPage sequence of calls, calling SetLayer with a valid layer title will start a new layer and place all subsequent drawing instructions within that layer. Calling SetLayer with an empty string will end the last layer.

If multiple layers are nested, multiple calls to SetLayer with an empty string are needed to close all layers. If all layers are not closed at the call to EndPage, the PDF printer will automatically close all open layers, for example:

// start parent layer
SetLayer( hDC, L“Blue Layer” );
SetTextColor( hDC, RGB(0, 0, 255) );
TextOut( hDC, 200, yPos, buf, lstrlen(buf) );
yPos += 200;

// start blue sub-layer 1
SetLayer( hDC, L“Blue Layer – 1” );
SetTextColor( hDC, RGB(0, 0, 128) );
TextOut( hDC, 800, yPos, “Blue Layer – 1”, lstrlen(“Blue Layer – 1”) );
yPos += 200;
SetLayer( hDC, L);    // close blue sub-layer 1
SetLayer( hDC, L);    // close blue parent layer

The sequence of opening and closing layers should be consistent with the order string for the PDF viewer to show or hide layers with their sub-layers in a consistent way.

The contents for this post were provided by Dany Amiouny using the Amyuni PDF Converter.

To learn more about Amyuni Developer Pro tools please visit www.amyuni.com.

Dany Amiouny is the CTO for Amyuni Technologies
www.amyuni.com