> 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/webviewer-annotations-document.md).

# Save and Load PDF Annotations with Node.js

Save and load annotations using files and Node.js backend.

In addition to showing how to add the WebViewer component to a Node.js backend project, this sample expands on annotation functionality by enabling saving and loading the annotation objects into a PDF document.

The saving is done through POST HTTP message that is handled by an `annotationHandler` in the project.

Follow these steps:

1. Create annotations with annotations tools in the header.
2. Save annotations with the save button in the header.
3. Load annotations by refreshing the app.
4. Locate the original PDF document in server folder where the new annotations are saved into.

WebViewer provides a slick out-of-the-box responsive UI that enables you to view, annotate and manipulate PDFs and other document types inside any web project.

Click the button below to view the full project in GitHub.

{% tabs %}
{% tab title="index.js" %}
{% code title="index.js" lineNumbers="true" %}

```js
const viewerElement = document.getElementById('viewer');
WebViewer({
  path: 'lib',
  initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/demo.pdf',
}, viewerElement).then(instance => {
  const {documentViewer, annotationManager} = instance.Core;

  // Add a save button on header
  const topHeader = instance.UI.getModularHeader('default-top-header');
  const items = topHeader.getItems();

  const saveButton = {
    type: 'customButton',
    img: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M17 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V7l-4-4zm-5 16c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm3-10H5V5h10v4z"/></svg>',
    title: 'Save Annotations',
    onClick: function() {
      // Update the document when button is clicked
      saveDocument('demo.pdf').then(function() {
        alert('Annotations saved to the document.');
      });
    }
  };

  items.push(saveButton);
  topHeader.setItems(items);

  // Make a POST request with blob data for a PDF with new annotations
  const saveDocument = function(filename) {
    return new Promise(function(resolve) {
      annotationManager.exportAnnotations().then(function(xfdfString) {
        documentViewer.getDocument().getFileData({ xfdfString }).then(function(data) {
          const arr = new Uint8Array(data);
          const blob = new Blob([ arr ], { type: 'application/pdf' });
          // FormData is used to send blob data through fetch
          const formData = new FormData();
          formData.append('blob', blob);
          fetch(`/server/annotationHandler.js?filename=${filename}`, {
            method: 'POST',
            body: formData
          }).then(function(res) {
            if (res.status === 200) {
              resolve();
            }
          });
        });
      });
    });
  };
});

```

{% endcode %}
{% endtab %}

{% tab title="annotationHandler.js" %}
{% code title="annotationHandler.js" lineNumbers="true" %}

```js
const fs = require('fs');
const multer = require('multer');

const upload = multer(); // To handle blob in the server

module.exports = (app) => {  
  // Handle POST request sent to '/server/annotationHandler.js'
  app.post('/server/annotationHandler.js', upload.any(), (req, res) => {
    const filename = req.query.filename;
    try {
      // Write the blob into a PDF file
      res.status(200).send(fs.writeFileSync(`server/${req.query.filename}`, req.files[0].buffer));
    } catch(e) {
      res.status(500).send(`Error writing file data to ${filename}`);
    }
    res.end();
  });
}

```

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

[View the full sample on GitHub](https://github.com/ApryseSDK/webviewer-samples/tree/main/webviewer-annotations-document)


---

# 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/webviewer-annotations-document.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.
