Some test text!

Hamburger Icon

Cpp / Guides / Extract layer

Extract layers from PDF in C++

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

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

OCG::Config init_cfg = doc.GetOCGConfig();
OCG::Context ctx(init_cfg);

PDFDraw 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).

// 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 = int(ocgs.Size());
for (i=0; i<sz; ++i) {
    OCG::Group ocg(ocgs.GetAt(i));
    ctx.SetState(ocg, true);
    std::string fname("pdf_layers_");
    fname += ocg.GetName().ConvertToAscii();
    fname += ".png";
    pdfdraw.Export(page, (output_path + fname).c_str());

// Now draw content that is not part of any layer...
pdfdraw.Export(page, (output_path + "pdf_layers_non_oc.png").c_str());

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

Get the answers you need: Chat with us