> 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/web/layers-ocgs/extract-layer.md).

# Extract layers from PDF using JavaScript

Learn how to extract PDF layers or OCG (optional content group) from a document with full sample code. Create, extract, and render PDF layers effortlessly. The Apryse Web SDK streamlines secure, serve

{% hint style="warning" %}
Make sure you have [Full API enabled in WebViewer.](/web/what-is-webviewer/full-api.md)
{% endhint %}

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

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
<html>
  <script src="../lib/core/webviewer-core.min.js"></script>
  <script src="../lib/core/pdf/PDFNet.js"></script>
  <script>
    (async function() {
      Core.setWorkerPath('../lib/core');
      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);

          //optionally save the blob to a file or upload to a server
          const layerBlob = 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);

      // optionally save the blob to a file or upload to a server
      const nonLayerBlob = new Blob([nonLayerBuffer], { type: 'image/png' });
    })()
  </script>
</html>
```

{% endcode %}

[PDFNet.PDFDoc.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) [PDFNet.PDFDoc.getPage](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getPage__anchor) [PDFNet.PDFDoc.getOCGConfig](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getOCGConfig__anchor) [PDFNet.PDFDoc.getOCGs](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getOCGs__anchor) [PDFNet.OCGContext.createFromConfig](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#.createFromConfig__anchor) [PDFNet.OCGContext.setNonOCDrawing](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#setNonOCDrawing__anchor) [PDFNet.OCGContext.resetStates](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#resetStates__anchor) [PDFNet.OCGContext.setState](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#setState__anchor) [PDFNet.OCGContext.setOCDrawMode](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#setOCDrawMode__anchor) [PDFNet.OCG.createFromObj](https://sdk.apryse.com/api/web/Core.PDFNet.OCG.html#.createFromObj__anchor) [PDFNet.PDFDraw.create](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDraw.html#.create__anchor) [PDFNet.PDFDraw.setImageSize](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDraw.html#setImageSize__anchor) [PDFNet.PDFDraw.setOCGContext](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDraw.html#setOCGContext__anchor) [Core.setWorkerPath](https://sdk.apryse.com/api/web/Core.html#.setWorkerPath__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
<html>
  <script src="../lib/core/CoreControls.js"></script>
  <script src="../lib/core/pdf/PDFNet.js"></script>
  <script>
    (async function() {
      CoreControls.setWorkerPath('../lib/core');
      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);

          //optionally save the blob to a file or upload to a server
          const layerBlob = 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);

      // optionally save the blob to a file or upload to a server
      const nonLayerBlob = new Blob([nonLayerBuffer], { type: 'image/png' });
    })()
  </script>
</html>
```

{% endcode %}

[PDFNet.PDFDoc.createFromURL](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#.createFromURL__anchor) [PDFNet.PDFDoc.getPage](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getPage__anchor) [PDFNet.PDFDoc.getOCGConfig](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getOCGConfig__anchor) [PDFNet.PDFDoc.getOCGs](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDoc.html#getOCGs__anchor) [PDFNet.OCGContext.createFromConfig](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#.createFromConfig__anchor) [PDFNet.OCGContext.setNonOCDrawing](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#setNonOCDrawing__anchor) [PDFNet.OCGContext.resetStates](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#resetStates__anchor) [PDFNet.OCGContext.setState](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#setState__anchor) [PDFNet.OCGContext.setOCDrawMode](https://sdk.apryse.com/api/web/Core.PDFNet.OCGContext.html#setOCDrawMode__anchor) [PDFNet.OCG.createFromObj](https://sdk.apryse.com/api/web/Core.PDFNet.OCG.html#.createFromObj__anchor) [PDFNet.PDFDraw.create](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDraw.html#.create__anchor) [PDFNet.PDFDraw.setImageSize](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDraw.html#setImageSize__anchor) [PDFNet.PDFDraw.setOCGContext](https://sdk.apryse.com/api/web/Core.PDFNet.PDFDraw.html#setOCGContext__anchor) [CoreControls.setWorkerPath](https://sdk.apryse.com/api/web/Core.html#.setWorkerPath__anchor)
{% endtab %}
{% endtabs %}

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