> 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/web/layers-ocgs/extract-layer.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
