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

# Add a new PDF Layer on iOS

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 layer manipulation now! The Apryse i

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

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

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] init];
PTElementBuilder *builder = [[PTElementBuilder alloc] init];	// ElementBuilder is used to build new Element objects
PTElementWriter *writer= [[PTElementWriter alloc] init];	// ElementWriter is used to write Elements to the page

// Create a layer
PTGroup *image_layer = [PTGroup Create: doc name: @"Image Layer"];
PTConfig *cfg = [PTConfig Create: doc default_config: YES];
[cfg SetName: @"Default"];

// Add the new OCG to the list of layers that should appear in PDF viewer GUI.
PTObj *layer_order_array = [doc CreateIndirectArray];
[cfg SetOrder: layer_order_array];
[layer_order_array PushBack: [image_layer GetSDFObj]];

// Start a new page
PTPage *page = [doc PageCreate: [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2:792]];
[writer WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES]; // Begin writing to the page

// Add new content to the page and associate it with one of the layers.
PTImage *img = [PTImage Create: [doc GetSDFDoc] filename: imagename];
[[img GetSDFObj] Put: @"OC" obj: image_layer];
[writer WritePlacedElement: [builder CreateImageWithCornerAndScale: img x: 300 y: 600 hscale: 200 vscale: -150]];
[writer End];   // save changes to the current page
[doc PagePushBack: page];
```

{% endcode %}
{% endtab %}

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

```swift
let doc: PTPDFDoc = PTPDFDoc()
let builder: PTElementBuilder = PTElementBuilder()    // ElementBuilder is used to build new Element objects
let writer: PTElementWriter = PTElementWriter()      // ElementWriter is used to write Elements to the page

// Create a layer
let image_layer: PTGroup = PTGroup.create(doc, name: "Image Layer")
let cfg: PTConfig = PTConfig.create(doc, default_config: true)
cfg.setName("Default")

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

// Start a new page
let page: PTPage = doc.pageCreate(PTPDFRect(x1: 0, y1: 0, x2: 612, y2: 792))
writer.writerBegin(with: page, placement: e_ptoverlay, page_coord_sys: true, compress: true) // Begin writing to the page

// Add new content to the page and associate it with one of the layers.
let img: PTImage = PTImage.create(doc.getSDFDoc(), filename: imagename)
img.getSDFObj().put("OC", obj: image_layer)
writer.writePlacedElement(builder.createImage(withCornerAndScale: img, x: 300, y: 600, hscale: 200, vscale: -150))             writer.end()   // save changes to the current page
doc.pagePushBack(page)
```

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

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