Some test text!

menu

Using thumbnail controls to perform page manipulation

Sample JavaScript code to use thumbnail controls to perform page manipulation in the browser (no servers or other external dependencies required). This sample lets you rotate a PDF page and delete, move, or insert a new page into your document. There are two instances of WebViewer, so you can move pages between them. In the case of MS Office files (DOCX, XLSX, PPTX), documents are converted to PDF files where modifications are saved. This sample works on all browsers (including IE10) and mobile devices without using plug-ins. For an example view our Thumbnail Controls demo. Learn more about our JavaScript PDF Library.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

JavaScript

HTML

const viewers = [
  {
    initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf',
    domElement: 'leftPanel',
  },
  {
    initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/report.docx',
    domElement: 'rightPanel',
  },
];
let workerTransportPromise;

CoreControls.setWorkerPath('../../../lib/core');
CoreControls.getDefaultBackendType().then(pdfType => {
  workerTransportPromise = CoreControls.initPDFWorkerTransports(pdfType, {});

  initializeWebViewer(viewers[0]);
  initializeWebViewer(viewers[1]);
});

const initializeWebViewer = viewer => {
  WebViewer(
    {
      path: '../../../lib',
      // since there are two instance of WebViewer, use "workerTransportPromise" so viewers can share resources
      workerTransportPromise: {
        pdf: workerTransportPromise,
      },

      // set "useDownloader" to false to the either file is loaded
      useDownloader: false,

      // can load a office documents as PDFDocument by setting "loadAsPDF"
      loadAsPDF: true,

      initialDoc: viewer.initialDoc,
    },
    document.getElementById(`${viewer.domElement}`)
  ).then(instance => {
    const { docViewer } = instance;
    instance.enableFeatures(['ThumbnailMultiselect', 'MultipleViewerMerging']);
    instance.enableElements(['documentControl']);

    docViewer.on('documentLoaded', () => {
      instance.openElements(['thumbnailsPanel']);

      // select some pages
      instance.selectThumbnailPages([1]);
    });

    document.getElementById(`${viewer.domElement}`).addEventListener('documentMerged', data => {
      // can use "documentMerged" event to track what is being merged into a document
      console.log(`From: ${data.detail.filename} pages: ${data.detail.pages}`);
    });

    // set up controls on the left side bar
    document.getElementById(`${viewer.domElement}-select`).onchange = e => {
      instance.loadDocument(e.target.value);
    };

    document.getElementById(`${viewer.domElement}-file-picker`).onchange = e => {
      const file = e.target.files[0];

      if (file) {
        instance.loadDocument(file);
      }
    };

    document.getElementById(`${viewer.domElement}-url-form`).onsubmit = e => {
      e.preventDefault();
      instance.loadDocument(document.getElementById(`${viewer.domElement}-url`).value);
    };
  });
};
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