Some test text!

menu
Extract layerkeyboard_arrow_down

Extract layers from PDF on Linux

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

PDFDoc doc = new PDFDoc(filename);
Page page = doc.GetPage(1);

Config init_cfg = doc.GetOCGConfig();
Context ctx = new Context(init_cfg);

PDFDraw pdfdraw = new PDFDraw();
pdfdraw.SetImageSize(1000, 1000);
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(false);

// Now render each layer in the input document to a separate image.
Obj ocgs = doc.GetOCGs(); // Get the array of all OCGs in the document.
int i, sz = ocgs.Size();
for (i=0; i<sz; ++i) 
{
    Group ocg = new Group(ocgs.GetAt(i));
    ctx.ResetStates(false);
    ctx.SetState(ocg, true);
    string fname = "pdf_layers_" + ocg.GetName() + ".png";
    pdfdraw.Export(page, output_path + fname);
}

// Now draw content that is not part of any layer...
ctx.SetNonOCDrawing(true);
ctx.SetOCDrawMode(Context.OCDrawMode.e_NoOC);
pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png");

PDF layers (OCG)
Full sample code which demonstrates how to create, extract, render PDF layers.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close