Some test text!

menu
Insert pageskeyboard_arrow_down

Inserting a page into a PDF using JavaScript

Pages can be inserted into the current PDF document using the insertBlankPages and insertPages functions. Inserting blank pages is fairly straight forward.

linkInsert blank pages

WebViewer(...)
  .then(instance => {
    const docViewer = instance.docViewer;

    docViewer.on('documentLoaded', async () => {
      const doc = docViewer.getDocument();

      const width = 612;
      const height = 792;
      docViewer.getPageCount(); // 3

      // Insert blank pages
      await doc.insertBlankPages([2, 3], width, height);
      // The document now contains [page1, newBlankPage1, page2, newBlankPage2, page3]
      docViewer.getPageCount(); // 5
    });
  });

linkInsert a page from another document or the same document

More likely someone will want to insert pages from one PDF into another. To achieve this, instantiate a new Document object by passing the URL of the second PDF to CoreControls.createDocument function as a first parameter.

Currently there aren't any functions to duplicate pages in the current document directly. If you want to copy a page, you'll need to instantiate a new Document object and load the same PDF into it using CoreControls.createDocument and insert the page into the current document.
WebViewer({
  // options
}, document.getElementById('viewer'))
  .then(instance => {
    const { docViewer, CoreControls } = instance;

    docViewer.on('documentLoaded', async () => {
      const doc = docViewer.getDocument();
      const pagesToInsert = [4, 5, 6];
      const pageIndexToInsert = doc.getPageCount() + 1; 
      // in this example doc.getPageCount() returns 3
      
      
      // use createDocument to create CoreControls.Document instance
      const docToInsert = await CoreControls.createDocument('http://localhost/pdf/another.pdf', {});
        
      doc.insertPages(docToInsert, pagesToInsert, pageIndexToInsert).then(() => {
        // page 4, 5, and 6 from 'another.pdf' has been inserted into to the current pdf
        doc.getPageCount(); // 6
      });

    });
  });

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