Some test text!
Web / Guides / PDF Overlay
Make sure you have Full API enabled in WebViewer.
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.
window.addEventListener('viewerLoaded', async () => {
// initialize PDFNet
await PDFNet.initialize('Insert commercial license key here after purchase');
const getDocument = async (url) => {
const newDoc = await Core.createDocument(url);
return await newDoc.getPDFDoc();
};
const [doc1, doc2] = await Promise.all([
getDocument('https://s3.amazonaws.com/pdftron/pdftron/example/test_doc_1.pdf'),
getDocument('https://s3.amazonaws.com/pdftron/pdftron/example/test_doc_2.pdf')
])
});
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(); itr.next()) {
const page = await itr.current();
arr.push(page);
}
return arr;
}
const [doc1Pages, doc2Pages] = await Promise.all([
getPageArray(doc1),
getPageArray(doc2)
]);
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();
newDoc.lock();
// we'll loop over the doc with the most pages
const biggestLength = Math.max(doc1Pages.length, doc2Pages.length)
for(let i = 0; i < biggestLength; i++) {
let page1 = doc1Pages[i];
let page2 = doc2Pages[i];
// handle the case where one document has more pages than the other
if (!page1) {
page1 = await doc1.pageCreate(); // create a blank page
}
if (!page2) {
page2 = await doc2.pageCreate(); // create a blank page
}
await newDoc.appendVisualDiff(page1, page2)
}
newDoc.unlock();
// display the document!
// instance is a global variable thats automatically defined inside the config file.
instance.UI.loadDocument(newDoc);
The full code sample should look like this:
Webviewer({
fullAPI: true,
path: '/lib',
}, document.getElementById('viewer')).then(instance => {
const { PDFNet } = instance.Core;
instance.UI.addEventListener('viewerLoaded', async () => {
// initialize PDFNet
await PDFNet.initialize();
const getDocument = async (url) => {
const newDoc = await instance.Core.createDocument(url);
return await newDoc.getPDFDoc();
};
const [doc1, doc2] = await Promise.all([
getDocument('https://s3.amazonaws.com/pdftron/pdftron/example/test_doc_1.pdf'),
getDocument('https://s3.amazonaws.com/pdftron/pdftron/example/test_doc_2.pdf')
])
// inside `viewerLoaded`
const getPageArray = async (doc) => {
const arr = [];
const itr = await doc.getPageIterator(1);
for (itr; await itr.hasNext(); itr.next()) {
const page = await itr.current();
arr.push(page);
}
return arr;
}
const [doc1Pages, doc2Pages] = await Promise.all([
getPageArray(doc1),
getPageArray(doc2)
]);
console.log(doc1Pages, doc2Pages);
const newDoc = await PDFNet.PDFDoc.create();
newDoc.lock();
// we'll loop over the doc with the most pages
const biggestLength = Math.max(doc1Pages.length, doc2Pages.length)
for(let i = 0; i < biggestLength; i++) {
let page1 = doc1Pages[i];
let page2 = doc2Pages[i];
// handle the case where one document has more pages than the other
if (!page1) {
page1 = await doc1.pageCreate(); // create a blank page
}
if (!page2) {
page2 = await doc2.pageCreate(); // create a blank page
}
await newDoc.appendVisualDiff(page1, page2)
}
newDoc.unlock();
// display the document!
// instance is a global variable thats automatically defined inside the config file.
instance.UI.loadDocument(newDoc);
});
});
WebViewer should now display the diffed document, like the image below.
In this example:
Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales