Some test text!

PDF Overlaykeyboard_arrow_down

Compare PDFs by overlaying PDFs and creating comparison PDF

Only available with the Full API

WebViewer can take two PDF files and output the visual difference between them by overlaying the PDFs and generating a new PDF out of it. The generated PDF will preserve text and searchability. This can be useful in situations where you want to visually see the difference between two versions of a document (a blueprint for example). Check out the demo.

In our config.js file (see this guide for more information on config files), we start by waiting for WebViewer to fully initialize by waiting for the viewerLoaded event to fire. Once this is done, we can initialize the full API and get the documents into memory.

We'll start by writing a function that takes a URL and resolves with a document, and then use that function to load two sample documents.

The following code snippets are written using ES6+ syntax, which will only work in modern browsers. You may, however, transpile this code down to ES5 to ensure proper browser support. See this guide for more details.
window.addEventListener('viewerLoaded', async () => {
  // initialize PDFNet
  await PDFNet.initialize('Insert commercial license key here after purchase');

  const getDocument = async (url) => {
    const newDoc = await CoreControls.createDocument(url);
    return await newDoc.getPDFDoc();

  const [doc1, doc2] = await Promise.all([

Now we need to get the pages that we want to diff. In this example, we will diff all pages. We'll write a helper function to help us get the pages into an array, and then use that function to get the pages for both our documents.

// inside `viewerLoaded`
const getPageArray = async (doc) => {
  const arr = [];
  const itr = await doc.getPageIterator(1);

  for (itr; await itr.hasNext(); {
    const page = await itr.current();

  return arr;

const [doc1Pages, doc2Pages] = await Promise.all([

Now we can create a new blank document, and fill it with the diffed images from our two documents. Once that is done, we can tell WebViewer to display this new diffed document.

// inside `viewerLoaded`
const newDoc = await PDFNet.PDFDoc.create();

// we'll loop over the doc with the most pages
const biggestLength = Math.max(doc1Pages.length, doc2Pages.length)

// we need to do the pages in order, so lets create a Promise chain
const chain = Promise.resolve();

for(let i = 0; i < biggestLength; i++) {
  chain.then(async () => {
    const page1 = doc1Pages[i];
    const page2 = doc2Pages[i];

    // handle the case where one document has more pages than the other
    if (!page1) {
      page1 = new PDFNet.Page(0); // create a blank page
    if (!page2) {
      page2 = new PDFNet.Page(0); // create a blank page
    return newDoc.appendVisualDiff(page1, page2, null)

await chain; // wait for our chain to resolve

// display the document!
// readerControl is a global variable thats automatically defined inside the config file.

WebViewer should now display the diffed document, like the image below.

In this example, the color blue represents content that is in document one and not document two, and red represents content in document two that is not in document one. Black represents overlap.

Behind the scenes, WebViewer blends the two documents using the Porter/Duff 'darken' operator and displays the output.

Get the answers you need: Support


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.