Add a new PDF Layer on Server/Desktop

To add a PDF layer or OCG (optional content group) to a new document.

1$doc = new PDFDoc();
2$builder = new ElementBuilder(); // ElementBuilder is used to build new Element objects
3$writer = new ElementWriter; // ElementWriter is used to write Elements to the page
4
5// Create a layer
6$image_layer = Group::Create($doc, "Image Layer");
7$cfg = Config::Create($doc, true);
8$cfg->SetName("Default");
9
10// Add the new OCG to the list of layers that should appear in PDF viewer GUI.
11$layer_order_array = $doc->CreateIndirectArray();
12$cfg->SetOrder($layer_order_array);
13$layer_order_array->PushBack($image_layer->GetSDFObj());
14
15// Start a new page
16$page = $doc->PageCreate();
17$writer->Begin($page); // Begin writing to the page
18
19// Add new content to the page and associate it with one of the layers.
20$img = Image::Create($doc->GetSDFDoc(), $imagename);
21$img->GetSDFObj()->Put("OC", $image_layer);
22$writer->WritePlacedElement($builder->CreateImage($img, 300.0, 600.0, 200.0, -150.0));
23$writer->End(); // save changes to the current page
24$doc->PagePushBack($page);

PDF layers (OCG)
Full sample code which demonstrates how to create, extract, render PDF layers.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales