Add a new PDF Layer on iOS

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

1PTPDFDoc *doc = [[PTPDFDoc alloc] init];
2PTElementBuilder *builder = [[PTElementBuilder alloc] init]; // ElementBuilder is used to build new Element objects
3PTElementWriter *writer= [[PTElementWriter alloc] init]; // ElementWriter is used to write Elements to the page
4
5// Create a layer
6PTGroup *image_layer = [PTGroup Create: doc name: @"Image Layer"];
7PTConfig *cfg = [PTConfig Create: doc default_config: YES];
8[cfg SetName: @"Default"];
9
10// Add the new OCG to the list of layers that should appear in PDF viewer GUI.
11PTObj *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
16PTPage *page = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2:792]];
17[writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES]; // Begin writing to the page
18
19// Add new content to the page and associate it with one of the layers.
20PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: imagename];
21[[img GetSDFObj] Put: @"OC" obj: image_layer];
22[writer WritePlacedElement: [builder CreateImageWithCornerAndScale: img x: 300 y: 600 hscale: 200 vscale: -150]];
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