Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

iOS / Guides / Extract layer


PDFTron is now Apryse, learn more here.

Extract layers from PDF on iOS

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

PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTPage *page = [doc GetPage: 1];

PTConfig *init_cfg = [doc GetOCGConfig];
PTContext *ctx = [[PTContext alloc] initWithConfig: init_cfg];

PTPDFDraw *pdfdraw = [[PTPDFDraw alloc] initWithDpi: 92];
[pdfdraw SetImageSize: 1000 height: 1000 preserve_aspect_ratio: YES];
[pdfdraw SetOCGContext: ctx]; // Render the page using the given OCG context.

// Disable drawing of content that is not optional (i.e. is not part of any layer).
[ctx SetNonOCDrawing: NO];

// Now render each layer in the input document to a separate image.
PTObj * ocgs = [doc GetOCGs]; // Get the array of all OCGs in the document.
unsigned long i, sz = [ocgs Size];
for (i=0; i<sz; ++i) {
    PTGroup *ocg = [[PTGroup alloc] initWithOcg: [ocgs GetAt: i]];
    [ctx ResetStates: NO];
    [ctx SetState: ocg state: YES];
    NSString* fname = [output_path stringByAppendingFormat: @"pdf_layers_%@.png", [ocg GetName]];
    [pdfdraw  page filename: fname format: @"PNG"];

// Now draw content that is not part of any layer...
[ctx SetNonOCDrawing: YES];
[ctx SetOCDrawMode: e_ptNoOC];
[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.

Get the answers you need: Support