Add a new PDF Layer on Server/Desktop

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

1doc = PDFDoc.new
2builder = ElementBuilder.new # ElementBuilder is used to build new Element objects
3writer = ElementWriter.new # ElementWriter is used to write Elements to the page
4
5# Create a layer
6image_layer = Group.Create(doc, "Image Layer")
7cfg = PDFNetRuby::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.
11layer_order_array = doc.CreateIndirectArray
12cfg.SetOrder(layer_order_array)
13layer_order_array.PushBack(image_layer.GetSDFObj)
14
15# Start a new page
16page = doc.PageCreate
17writer.Begin(page) # Begin writting to the page
18
19# Add new content to the page and associate it with one of the layers.
20img = Image.Create(doc.GetSDFDoc, imagename)
21img.GetSDFObj().Put("OC", image_layer)
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