// ES6 Compliant Syntax
// GitHub Copilot v1.0 - GPT-4 Model - July 15, 2025
// File: index.js
import WebViewer from '@pdftron/webviewer';
const licenseKey = 'YOUR_WEBVIEWER_LICENSE_KEY';
// Customize the WebViewer UI
// Add custom buttons for flattening forms and downloading PDFs
const customizeUI = (instance) => {
const { UI } = instance;
// Flattening button
const flattenButton = new UI.Components.CustomButton({
dataElement: 'flattenButton',
className: 'custom-button-class',
label: 'Flattening',
onClick: () => flatten(instance), // Flatten the document
style: {
padding: '10px 20px',
backgroundColor: 'white',
color: 'blue',
border: '1px solid blue',
}
});
// Download button
const downloadButton = new UI.Components.CustomButton({
dataElement: 'downloadPdfButton',
className: 'custom-button-class',
label: 'Download',
onClick: () => download(instance), // Download with annotations
style: {
padding: '10px 20px',
backgroundColor: 'blue',
color: 'white',
}
});
const defaultHeader = UI.getModularHeader('default-top-header');
defaultHeader.setItems([...defaultHeader.items, flattenButton, downloadButton]);
};
// Flatten the form fields in the PDF document
const flatten = async (instance) => {
const { documentViewer, PDFNet, annotationManager } = instance.Core;
await PDFNet.initialize();
const doc = await documentViewer.getDocument().getPDFDoc();
const annotations = await annotationManager.exportAnnotations();
// Run PDFNet methods with memory management
await PDFNet.runWithCleanup(async () => {
// lock the document before a write operation
// runWithCleanup will auto unlock when complete
doc.lock();
// import annotations to PDFNet
const fdf_doc = await PDFNet.FDFDoc.createFromXFDF(annotations);
await doc.fdfUpdate(fdf_doc);
// Generate appearances in a way that supports non-standard rotation
const options = await PDFNet.PDFDoc.createRefreshOptions();
options.setUseNonStandardRotation(true);
await doc.refreshAnnotAppearances(options);
// flatten all annotations in the document
await doc.flattenAnnotations();
// clear the original annotations
annotationManager.deleteAnnotations(annotationManager.getAnnotationsList());
});
// clear the cache (rendered) data with the newly updated document
documentViewer.refreshAll();
// Update viewer to render with the new document
documentViewer.updateView();
// Refresh searchable and selectable text data with the new document
documentViewer.getDocument().refreshTextData();
};
// Download the PDF
const download = async (instance) => {
// Set the options for downloading the PDF
const options = {
flags: instance.Core.SaveOptions.LINEARIZED,
downloadType: 'pdf'
};
instance.UI.downloadPdf(options);
};
WebViewer(
{
path: '/lib',
initialDoc: 'https://apryse.s3.us-west-1.amazonaws.com/public/files/samples/form.pdf',
fullAPI: true,
enableFilePicker: true, // Enable file picker to open files. In WebViewer -> menu icon -> Open File
licenseKey: licenseKey,
},
document.getElementById('viewer')
).then((instance) => {
// customize WebViewer UI
customizeUI(instance);
console.log('✅ WebViewer loaded successfully.');
}).catch((error) => {
console.error('❌ Failed to initialize WebViewer:', error);
});