> 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/get-started/samples/sdftest.md).

# Extract SDF Document From PDF

Sample JavaScript code to edit an existing PDF document at the object level by using the Apryse SDK Cos/SDF low-level API.

{% hint style="info" %}
**Requirements**

*These packages are required to use these features in production. Trial keys have unlimited access to all features*

<a href="/web/get-started/readme.md" class="button primary">Web SDK</a><a href="/web/full-api/full-api-overview.md" class="button primary">Full API</a>
{% endhint %}

Sample JavaScript code for editing an existing PDF document at the object level by using the Apryse SDK Cos/SDF low-level API.

Learn more about our [Web SDK](/web/get-started/guides.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

### **Implementation steps**

Step 1: Follow [get started in your preferred web stack for WebViewer](/web/get-started/readme.md) Step 2: [Enable the full API ](/web/full-api/full-api-overview.md)by passing the `fullAPI` option into the WebViewer constructor Step 3: Add the sample code provided in this guide

This full sample is one of many included in the [manual download of WebViewer.](/web/get-started/manually.md#1-download-webviewer)

<pre class="language-js" data-line-numbers><code class="lang-js">//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

(exports => {
  exports.runSDFTest = () => {
    const PDFNet = exports.Core.PDFNet;

    const main = async () => {
      console.log('Beginning SDF Test.');
      const inputURL = '../TestFiles/';

      try {
        // Here we create a SDF/Cos document directly from PDF file. In case you have
        // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
        const docorig = await PDFNet.PDFDoc.createFromURL(inputURL + 'fish.pdf');
        const doc = await docorig.getSDFDoc();
        doc.initSecurityHandler();
        doc.lock();
        console.log('Modifying into dictionary, adding custom properties, embedding a stream...');

        const trailer = await doc.getTrailer(); // Get the trailer

        // Now we will change PDF document information properties using SDF API

        // Get the Info dictionary.

        let itr = await trailer.find('Info');
        let info;
        if (await itr.hasNext()) {
          info = await itr.value();
          // Modify 'Producer' entry.
          info.putString('Producer', 'PDFTron PDFNet');

          // read title entry if it is present
          itr = await info.find('Author');
          if (await itr.hasNext()) {
            const itrval = await itr.value();
            const oldstr = await itrval.getAsPDFText();
            info.putText('Author', oldstr + ' - Modified');
          } else {
            info.putString('Author', 'Me, myself, and I');
          }
        } else {
          // Info dict is missing.
          info = await trailer.putDict('Info');
          info.putString('Producer', 'PDFTron PDFNet');
          info.putString('Title', 'My document');
        }

        // Create a custom inline dictionary within Infor dictionary
        const customDict = await info.putDict('My Direct Dict');
        customDict.putNumber('My Number', 100); // Add some key/value pairs
        customDict.putArray('My Array');

        // Create a custom indirect array within Info dictionary
        const customArray = await doc.createIndirectArray();
        info.put('My Indirect Array', customArray); // Add some entries

        // create indirect link to root
        const trailerRoot = await trailer.get('Root');
        customArray.pushBack(await trailerRoot.value());

        // Embed a custom stream (file mystream.txt).
        const embedFile = await PDFNet.Filter.createURLFilter(inputURL + 'my_stream.txt');
        const mystm = await PDFNet.FilterReader.create(embedFile);
        const indStream = await doc.createIndirectStreamFromFilter(mystm);
        customArray.pushBack(indStream);

        const docbuf = await doc.saveMemory(0, '%PDF-1.4'); // PDFNet.SDFDoc.SaveOptions.e_remove_unused
        saveBufferAsPDFDoc(docbuf, 'sdftest_out.pdf');
        console.log('Done.');
      } catch (err) {
        console.log(err);
      }
    };
    // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, '<code class="expression">visitor.claims.wvKey || "YOUR_LICENSE_KEY"</code>')
    PDFNet.runWithCleanup(main);
  };
})(window);
// eslint-disable-next-line spaced-comment
//# sourceURL=SDFTest.js
</code></pre>


---

# 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/get-started/samples/sdftest.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.
