Extract layers from PDF on iOS

To extract PDF layers or OCG (optional content group) from a document.

1PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
2PTPage *page = [doc GetPage: 1];
3
4PTConfig *init_cfg = [doc GetOCGConfig];
5PTContext *ctx = [[PTContext alloc] initWithConfig: init_cfg];
6
7PTPDFDraw *pdfdraw = [[PTPDFDraw alloc] initWithDpi: 92];
8[pdfdraw SetImageSize: 1000 height: 1000 preserve_aspect_ratio: YES];
9[pdfdraw SetOCGContext: ctx]; // Render the page using the given OCG context.
10
11// Disable drawing of content that is not optional (i.e. is not part of any layer).
12[ctx SetNonOCDrawing: NO];
13
14// Now render each layer in the input document to a separate image.
15PTObj * ocgs = [doc GetOCGs]; // Get the array of all OCGs in the document.
16unsigned long i, sz = [ocgs Size];
17for (i=0; i<sz; ++i) {
18 PTGroup *ocg = [[PTGroup alloc] initWithOcg: [ocgs GetAt: i]];
19 [ctx ResetStates: NO];
20 [ctx SetState: ocg state: YES];
21 NSString* fname = [output_path stringByAppendingFormat: @"pdf_layers_%@.png", [ocg GetName]];
22 [pdfdraw page filename: fname format: @"PNG"];
23}
24
25// Now draw content that is not part of any layer...
26[ctx SetNonOCDrawing: YES];
27[ctx SetOCDrawMode: e_ptNoOC];
28[pdfdraw page filename: [output_path stringByAppendingString: @"pdf_layers_non_oc.png"] format: @"PNG" ];

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