> 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-document-merge.md).

# Merge Documents and Reorganize Pages Using a Drop Zone

Merge and rearrange pages from multiple documents using a drop zone. Note that this is only enabled for client-side PDF and Office documents.  Sample is using React and Apryse WebViewer.

This client-side React sample is showing how to use a drop zone to do merge and rearrange of pages among multiple documents.

The sample is demonstrating two WebViewer instances. The user will drag and drop pages from a document to hold in the drop zone, then drag and drop them within the thumbnail of another document.

In the case of MS Office files (DOCX, XLSX, PPTX), documents will be loaded as PDFs, where modifications are saved.

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="Viewer.js" %}
{% code title="Viewer.js" lineNumbers="true" %}

```js
import React, { useRef, useEffect } from 'react';
import WebViewer from '@pdftron/webviewer';

const Viewer = ({ docToLoad }) => {
  const viewer = useRef(null);

  useEffect(() => {
    WebViewer.Iframe(
      {
        path: '/lib/webviewer',
        initialDoc: docToLoad,
        ui: 'legacy',
        loadAsPDF: true,
        enableFilePicker: true,
      },
      viewer.current,
    ).then((instance) => {
      if (!window.instance) {
        window.instance = instance;
      }
      instance.UI.enableFeatures([
        'ThumbnailMultiselect',
        'MultipleViewerMerging',
      ]);
      instance.UI.enableElements(['documentControl']);
      instance.UI.openElements(['leftPanel']);
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  return <div className="webviewer" ref={viewer}></div>;
};

export default Viewer;

```

{% endcode %}
{% endtab %}

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

```js
import React, { useEffect, useRef, useState } from 'react';
import './styles.css';

const Dropzone = () => {
  const dropRef = useRef(null);
  const [docs, addDocument] = useState([]);
  const [thumbArray, addThumbToArray] = useState([]);

  useEffect(() => {
    if (docs.length >= 1) {
      const Core = window.instance.Core;
      const loadDocumentAndThumb = async () => {
        const doc = await Core.createDocument(docs[docs.length - 1]);
        doc.loadThumbnail(1, (thumbnail) => {
          addThumbToArray([...thumbArray, thumbnail]);
        });
      }
      loadDocumentAndThumb();
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [docs]);

  const mergeDocuments = async () => {
    const Core = window.instance.Core;
    if (docs.length > 0) {
      const doc = await Core.createDocument(docs[0]);
      let i;
      for (i = 1; i < docs.length; i++) {
        let doc2 = await Core.createDocument(docs[i]);
        await doc.insertPages(doc2);
      }

      const data = await doc.getFileData();
      const arr = new Uint8Array(data);
      const blob = new Blob([arr], { type: 'application/pdf' });
      downloadBlob(blob);
    }
    addDocument([]);
    addThumbToArray([]);
  };

  const downloadBlob = (blob) => {
    const a = document.createElement('a');
    document.body.appendChild(a);
    const url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = 'merged-file.pdf';
    a.click();
    setTimeout(() => {
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
    }, 0);
  };

  const onDropEvent = (ev) => {
    ev.preventDefault();
    const viewerID = ev.dataTransfer.getData('dataTransferWebViewerFrame');
    const otherWebViewerIframe = window.parent.document.querySelector(
      `#${viewerID}`,
    );
    if (!otherWebViewerIframe) {
      console.warn('Could not find other instance of WebViewer');
    }

    const extractedDataPromise =
      otherWebViewerIframe.contentWindow.extractedDataPromise;
    if (!extractedDataPromise) {
      console.warn('Could not retrieve data from other instance of WebViewer');
    }

    extractedDataPromise.then(docToMerge => {
      addDocument([...docs, docToMerge]);
    });
  };

  return (
    <div>
      <div
        className="dropDiv"
        ref={dropRef}
        onDrop={ev => {
          onDropEvent(ev);
        }}
        onDragOver={(ev) => {
          ev.preventDefault();
          ev.dataTransfer.dropEffect = 'move';
        }}
      >
        <p>Drop the thumbs from the viewers here</p>
        <button onClick={mergeDocuments}>Download</button>
      </div>
      <div className="list">
        {thumbArray.map((thumb, i) => {
          return <img key={i} src={thumb.toDataURL()} alt={i} />
        })}
      </div>
    </div>
  );
};

export default Dropzone;

```

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

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


---

# 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-document-merge.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.
