> 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/add-layer.md).

# Add a new PDF Layer in Android

Learn how to add PDF layers (OCG) to your documents with our comprehensive guide. Get full sample code for creating, extracting, and rendering PDF layers. The Apryse Android SDK streamlines secure doc

To add a PDF layer or OCG (optional content group) to a new document.

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

```java
PDFDoc doc = new PDFDoc();
ElementBuilder builder = new ElementBuilder(); // ElementBuilder is used to build new Element objects
ElementWriter writer = new ElementWriter(); // ElementWriter is used to write Elements to the page

// Create a layer
Group image_layer = Group.create(doc, "Image Layer");
Config cfg = Config.create(doc, true);
cfg.setName("Default");

// Add the new OCG to the list of layers that should appear in PDF viewer GUI.
Obj layer_order_array = doc.createIndirectArray();
cfg.setOrder(layer_order_array);
layer_order_array.pushBack(image_layer.getSDFObj());

// Start a new page
Page page = doc.pageCreate();
writer.begin(page); // Begin writing to the page

// Add new content to the page and associate it with one of the layers.
Image img = Image.create(doc.getSDFDoc(), imagename);
img.getSDFObj().put("OC", image_layer.getSDFObj());
writer.writePlacedElement(builder.createImage(img, 300, 600, 200, -150));
writer.end();    // save changes to the current page
doc.pagePushBack(page);
```

{% endcode %}
{% endtab %}

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

```kotlin
val doc = PDFDoc()
val builder = ElementBuilder() // ElementBuilder is used to build new Element objects
val writer = ElementWriter() // ElementWriter is used to write Elements to the page

// Create a layer
val image_layer = Group.create(doc, "Image Layer")
var cfg: Config? = Config.create(doc, true)
cfg!!.name = "Default"

// Add the new OCG to the list of layers that should appear in PDF viewer GUI.
var layer_order_array: Obj? = doc.createIndirectArray()
cfg.order = layer_order_array!!
layer_order_array.pushBack(image_layer.sdfObj)

// Start a new page
val page = doc.pageCreate()
writer.begin(page) // Begin writing to the page

// Add new content to the page and associate it with one of the layers.
val img = Image.create(doc.sdfDoc, imagename)
img.getSDFObj().put("OC", image_layer)
writer.writePlacedElement(builder.createImage(img, 300.0, 600.0, 200.0, -150.0))
writer.end()   // save changes to the current page
doc.pagePushBack(page)
```

{% 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/add-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.
