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

# Add a new PDF Layer in Xamarin

Learn how to add a PDF layer or OCG (optional content group) to a new document with full sample code for creating, extracting, and rendering PDF layers. Master PDF optimization now! The Apryse Xamarin

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

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
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 this page

// Add new content to the page and associate it with the layer.
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 %}
{% endtabs %}

[PDF layers (OCG)](/xamarin/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/xamarin/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.
