Some test text!

Hamburger Icon

Android / Guides / Extract layer

Extract layers from PDF in Android

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

// 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) {
    Group ocg = new Group(ocgs.getAt(i));
    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...
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: Chat with us