Some test text!

menu
User bookmarkskeyboard_arrow_down

Creating user-defined bookmarks in WebViewer

Welcome to WebViewer User Bookmarks. WebViewer allows users to create bookmarks for each page. When clicked, these bookmarks will navigate to the saved page. In order to persist these bookmarks, WebViewer can easily export them. They can then be saved to a server.

user-bookmarks

You can enable bookmarks by enabling the following elements. The rest of this guide will demonstrate a ready to go sample and how you can export them.

WebViewer(...)
  .then(instance => {
    instance.enableElements(['bookmarksPanel', 'bookmarksPanelButton']);
  });

linkPrerequisites

linkInitial setup

  1. Extract webviewer-user-bookmarks-nodejs-sample.zip. The directory should look something like this:
webviewer-user-bookmarks-nodejs-sample
├── ...
├── package.json
├── README.md
├── client
│   ├── lib
│   │    ├── ...
│   ├── index.html
│   ├── index.js
│   └── webviewer-demo.pdf
└── server
    ├── bookmarks
    │    ├── ...
    ├── bookmarksHandler.js
    └── serve.js

linkHow to run

  1. Navigate to the extracted sample folder and install the required dependencies to run the samples by executing:
npm install
  1. Next run the sample by executing:
npm start

You should see a message that reads:

...
Server is listening at http://localhost:3000/client/index.html
  1. Navigate to http://localhost:3000/client/index.html and you will see the sample WebViewer. Open the left panel and navigate to the last tab to see bookmarks. You should see the same bookmarks as the image above.

linkImport and Exporting Bookmarks

The webviewer instance contains the functions exportBookmarks and importBookmarks. You can use these along with AJAX requests to save and load the user bookmark data from a server, and setup the server to write and read XFDF files.

To save user bookmarks, the sample, uses POST and GET requests. In the sample you will find fetch request that are similar to the following:

WebViewer(...)
  .then(function(instance) {
    // load the user bookmarks data for id 'doc123'
    fetch('/server/bookmarksHandler.js?documentId=doc123', {
      method: 'GET'
    }).then(function(response) {
      if (response.status === 200) {
        response.text().then(function(bookmarksString) {
          // {"0":"Bookmark 1","2":"Bookmark 2"}
          const bookmarks = JSON.parse(bookmarksString);
          instance.importBookmarks(bookmarks);
        });
      }
    });

    // ...
    // later save the annotation data for doc123
    const bookmarks = instance.exportBookmarks();
    const bookmarksString = JSON.stringify(bookmarks);
    fetch('/server/bookmarksHandler.js?documentId=doc123', {
      method: 'POST',
      body: bookmarksString // written into a json file on server
    });
  });

To automatically save user bookmark changes you are able to listen to the `userBookmarksChanged' event. This event fires whenever there is any change to user bookmarks.

instance.iframeWindow.addEventListener('userBookmarksChanged', e => {
  const bookmarks = e.detail;
  const bookmarksString = JSON.stringify(bookmarks);
  // ...save string to server
});

The sample uses Node.js as a backend for fulling the POST and GET requests, but other backends can also be used.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close