> 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/preloading.md).

# Preloading WebViewer and Documents

Preload PDFs, DOCX, XSLX, PPTX, images and more in a PDF viewer using this JavaScript sample (no servers or other external dependencies required). Preloading documents in a PDF Viewer gives users near

Preload PDFs, MS Office documents, images and more in a PDF viewer using this JavaScript sample (no servers or other external dependencies required). Preloading documents in a PDF Viewer gives users near instantaneous access to large and complex documents – helping improve their overall user experience in your application. This sample works on all browsers (including IE11) and mobile devices without using plug-ins.

Learn more about our [Web SDK](/web/get-started/guides.md).

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
const viewerElement = document.getElementById('viewer');
// eslint-disable-next-line no-undef
const WebViewerConstructor = isWebComponent() ? WebViewer.WebComponent : WebViewer;

WebViewerConstructor(
  {
    path: '../../../lib',
    // in this example we are using 'all' to preload resources for office files
    // when working with only pdf, set to 'pdf' to avoid downloading unneeded resources
    preloadWorker: 'all',
  },
  viewerElement
).then(instance => {
  samplesSetup(instance);
  const preLoadFiles = ['../../../samples/files/demo.pdf', '../../../samples/files/simple-excel_2007.xlsx', '../../../samples/files/simple-word_2007.docx'];

  const documentsDiv = document.getElementById('documents');

  preLoadFiles.forEach(file => {
    const div = document.createElement('div');
    div.innerHTML = file.split('/').slice(-1)[0];

    const button = document.createElement('button');
    button.innerHTML = 'Loading';

    const list = document.createElement('li');
    list.appendChild(div);
    list.appendChild(button);

    documentsDiv.appendChild(list);

    // using this instead of 'fetch' for IE11 support
    const xhr = new XMLHttpRequest();
    xhr.open('GET', file, true);
    xhr.responseType = 'blob';
    xhr.onload = function() {
      const doc = this.response;
      if (this.status === 200) {
        button.onclick = () => {
          viewerElement.style.visibility = 'visible';
          // file name is required for office files, 'loadDocument' will assume files are pdf if there isn't a filename
          instance.UI.loadDocument(doc, { filename: file.split('/').slice(-1)[0] });
        };
        button.innerHTML = 'Open';
      }
    };
    xhr.send();
  });

  document.getElementById('toggle').addEventListener('click', () => {
    if (viewerElement.style.visibility === 'hidden') {
      viewerElement.style.visibility = 'visible';
    } else {
      viewerElement.style.visibility = 'hidden';
    }
  });
});
```

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


---

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