> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/layers-ocgs/extract-layer.md).

# Extract layers from PDF in Android

Learn how to extract PDF layers or OCG (optional content group) from a document with this comprehensive guide. Get full sample code for creating, extracting, and rendering PDF layers. The Apryse Andro

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

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
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 = (int) 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.e_NoOC);
pdfdraw.export(page, output_path + "pdf_layers_non_oc.png");
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc(filename)
val page = doc.getPage(1)

val init_cfg = doc.ocgConfig
val ctx = Context(init_cfg)

val 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).
ctx.setNonOCDrawing(false)

// Now render each layer in the input document to a separate image.
val ocgs = doc.ocGs // Get the array of all OCGs in the document.
var i: Int
val sz = ocgs.size().toInt()
i = 0
while (i < sz) {
    val ocg = Group(ocgs.getAt(i))
    ctx.resetStates(false)
    ctx.setState(ocg, true)
    val fname = "pdf_layers_" + ocg.name + ".png"
    pdfdraw.export(page, output_path + fname)
    ++i
}

// Now draw content that is not part of any layer...
ctx.setNonOCDrawing(true)
ctx.setOCDrawMode(Context.e_NoOC)
pdfdraw.export(page, output_path + "pdf_layers_non_oc.png")
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDF layers (OCG)](/android/get-started/samples.md#pdflayers) Full sample code which demonstrates how to create, extract, render PDF layers.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/layers-ocgs/extract-layer.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
