> 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/core/layer-ocg/extract-layer.md).

# Extract layers from PDF on Server/Desktop

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

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

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

```csharp
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 = 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.OCDrawMode.e_NoOC);
pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png");
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);
Page page = doc.GetPage(1);

OCG::Config init_cfg = doc.GetOCGConfig();
OCG::Context ctx(init_cfg);

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.
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) {
    OCG::Group ocg(ocgs.GetAt(i));
    ctx.ResetStates(false);
    ctx.SetState(ocg, true);
    std::string fname("pdf_layers_");
    fname += ocg.GetName().ConvertToAscii();
    fname += ".png";
    pdfdraw.Export(page, (output_path + fname).c_str());
}

// Now draw content that is not part of any layer...
ctx.SetNonOCDrawing(true);
ctx.SetOCDrawMode(OCG::Context::e_NoOC);
pdfdraw.Export(page, (output_path + "pdf_layers_non_oc.png").c_str());
```

{% endcode %}
{% endtab %}

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

```go
doc := NewPDFDoc(filename)
page = doc.GetPage(1)

init_cfg := doc.GetOCGConfig()
ctx := NewContext(init_cfg)

pdfdraw := NewPDFDraw()
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.
ocgs := doc.GetOCGs()    // Get the array of all OCGs in the document.
if ocgs != nil{
    sz := ocgs.Size()
    i := int64(0)
    for i < sz{
        ocg := NewGroup(ocgs.GetAt(i))
        ctx.ResetStates(false)
        ctx.SetState(ocg, true)
        fname := "pdf_layers_" + ocg.GetName() + ".png"
        fmt.Println(fname)
        pdfdraw.Export(page, outputPath + fname)
        i = i + 1
    }
}

// Now draw content that is not part of any layer...
ctx.SetNonOCDrawing(true)
ctx.SetOCDrawMode(ContextE_NoOC)
pdfdraw.Export(page, outputPath + "pdf_layers_non_oc.png")
```

{% endcode %}
{% endtab %}

{% 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="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
    const doc = await PDFNet.PDFDoc.createFromURL(filename);
    const page = await doc.getPage(1);

    const initCfg = await doc.getOCGConfig();
    const ctx = await PDFNet.OCGContext.createFromConfig(initCfg);

    const pdfdraw = await PDFNet.PDFDraw.create();
    pdfdraw.setImageSize(1000, 1000);
    pdfdraw.setOCGContext(ctx);

    // 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.
    const ocgs = await doc.getOCGs(); // Get the array of all OCGs in the document.
    let i;
    const sz = await ocgs.size();
    for (i = 0; i < sz; ++i) {
        const ocg = await PDFNet.OCG.createFromObj(await ocgs.getAt(i));
        ctx.resetStates(false);
        ctx.setState(ocg, true);
        let fname = 'pdf_layers_';
        fname += await ocg.getName();
        fname += '.png';
        const pageBuffer = await pdfdraw.exportStream(page);
        const imageData = new Blob([pageBuffer], {type: 'image/png'}));
    }

    // Now draw content that is not part of any layer...
    ctx.setNonOCDrawing(true);
    ctx.setOCDrawMode(PDFNet.OCGContext.OCDrawMode.e_NoOC);
    const nonLayerBuffer = await pdfdraw.exportStream(page);
    const data = new Blob([nonLayerBuffer], {type: 'image/png'}));
}
PDFNet.runWithCleanup(main);
```

{% 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 %}

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

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTPage *page = [doc GetPage: 1];

PTConfig *init_cfg = [doc GetOCGConfig];
PTContext *ctx = [[PTContext alloc] initWithConfig: init_cfg];

PTPDFDraw *pdfdraw = [[PTPDFDraw alloc] initWithDpi: 92];
[pdfdraw SetImageSize: 1000 height: 1000 preserve_aspect_ratio: YES];
[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: NO];

// Now render each layer in the input document to a separate image.
PTObj * ocgs = [doc GetOCGs]; // Get the array of all OCGs in the document.
unsigned long i, sz = [ocgs Size];
for (i=0; i<sz; ++i) {
    PTGroup *ocg = [[PTGroup alloc] initWithOcg: [ocgs GetAt: i]];
    [ctx ResetStates: NO];
    [ctx SetState: ocg state: YES];
    NSString* fname = [output_path stringByAppendingFormat: @"pdf_layers_%@.png", [ocg GetName]];
    [pdfdraw  page filename: fname format: @"PNG"];
}

// Now draw content that is not part of any layer...
[ctx SetNonOCDrawing: YES];
[ctx SetOCDrawMode: e_ptNoOC];
[pdfdraw  page filename: [output_path stringByAppendingString: @"pdf_layers_non_oc.png"] format: @"PNG" ];
```

{% endcode %}
{% endtab %}

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

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
let page: PTPage = doc.getPage(1)

let init_cfg: PTConfig = doc.getOCGConfig()
let ctx: PTContext = PTContext(config: init_cfg)

let pdfdraw: PTPDFDraw = PTPDFDraw(dpi: 92)
pdfdraw.setImageSize(1000, height: 1000, preserve_aspect_ratio: true)
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.
let ocgs: PTObj = doc.getOCGs() {    // Get the array of all OCGs in the document.
var _: UInt = 0
let sz: UInt = ocgs.size()
for i in 0..<sz {
    let ocg: PTGroup = PTGroup(ocg: ocgs.getAt(i))
    ctx.resetStates(false)
    ctx.setState(ocg, state: true)
    let fname: String = output_path + "pdf_layers_\(ocg.getName()).png")
    pdfdraw.export(page, filename: fname, format: "PNG")
}

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

{% endcode %}
{% endtab %}

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

```php
$doc = new PDFDoc($filename);
$page = $doc->GetPage(1);

$init_cfg = $doc->GetOCGConfig();
$ctx = new Context($init_cfg);

$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.
$ocgs = $doc->GetOCGs(); // Get the array of all OCGs in the document.
$sz = $ocgs->Size();
for ($i=0; $i<$sz; ++$i) {
    $ocg = new Group($ocgs->GetAt($i));
    $ctx->ResetStates(false);
    $ctx->SetState($ocg, true);
    $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="Python" %}
{% code lineNumbers="true" %}

```python
doc = PDFDoc(filename)
page = doc.GetPage(1)

init_cfg = doc.GetOCGConfig()
ctx = Context(init_cfg)

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.
ocgs = doc.GetOCGs()    # Get the array of all OCGs in the document.
sz = ocgs.Size()
i = 0
while i < sz:
    ocg = Group(ocgs.GetAt(i))
    ctx.ResetStates(False)
    ctx.SetState(ocg, True)
    fname = "pdf_layers_" + ocg.GetName() + ".png"
    pdfdraw.Export(page, output_path + fname)
    i = i + 1

# 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="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc = PDFDoc.new($filename)
page = doc.GetPage(1)

init_cfg = doc.GetOCGConfig
ctx = Context.new(init_cfg)

pdfdraw = PDFDraw.new
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.
ocgs = doc.GetOCGs	# Get the array of all OCGs in the document.
sz = ocgs.Size
i = 0
while i < sz do
    ocg = Group.new(ocgs.GetAt(i))
    ctx.ResetStates(false)
    ctx.SetState(ocg, true)
    fname = "pdf_layers_" + ocg.GetName + ".png"
    pdfdraw.Export(page, $output_path + fname)
    i = i + 1
end

# 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="VB" %}
{% code lineNumbers="true" %}

```vb
Dim doc As PDFDoc = New PDFDoc(filename)
Dim page As Page = doc.GetPage(1)

Dim init_cfg As Config = doc.GetOCGConfig()
Dim ctx As Context = New Context(init_cfg)

Dim pdfdraw As 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.
Dim ocgs As Obj = doc.GetOCGs()			 ' Get the array of all OCGs in the document.
Dim i As Integer = 0
Dim sz As Integer = ocgs.Size()
While (i < sz)
    Dim ocg As Group = New Group(ocgs.GetAt(i))
    ctx.ResetStates(False)
    ctx.SetState(ocg, True)
    Dim fname As String =  "pdf_layers_" + ocg.GetName() + ".png"
    pdfdraw.Export(page, output_path + fname)
    i = i + 1
End While

' Now draw content that is not part of any layer...
ctx.SetNonOCDrawing(True)
ctx.SetOCDrawMode(Context.OCDrawMode.e_NoOC)
pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png")
```

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

[PDF layers (OCG)](/core/get-started/samples/pdflayerstest.md) Full sample code which demonstrates how to create, extract, render PDF layers. Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.


---

# 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/core/layer-ocg/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.
