Add a new PDF Layer in Android

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 the page
18
19// Add new content to the page and associate it with one of the layers.
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