Add a new PDF Layer on Server/Desktop

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

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