// Class with static UI elements and related functions for the OCR demo
class UIElements {
static languages = {
eng: 'English',
fra: 'French',
spa: 'Spanish',
ita: 'Italian',
deu: 'German',
rus: 'Russian',
};
static demoFilesPath = "https://apryse.s3.us-west-1.amazonaws.com/public/files/samples/";
static ocrdemofiles = {
eng: 'scanned_sublease_agreement.pdf',
fra: 'scanned_sublease_agreement_french.pdf',
spa: 'scanned_sublease_agreement_spanish.pdf',
ita: 'scanned_sublease_agreement_italian.pdf',
deu: 'scanned_sublease_agreement_german.pdf',
rus: 'scanned_sublease_agreement_russian.pdf',
};
static loadingFromList = true; // initially true because we load the first document from the list
static loadedFromPicker = false; // to differentiate if the document is loaded from the list or from file picker
static jsonData = null;
static loadLabelText = ' Select language to load sample';
static ocrText = null; // element to show OCR result text
static selectedLanguage = 'eng'; // Default language
static fileLabel = null;
static filesArray = []; // Files array for gallery picker
// The list of registered panels in the main webviewer
static viewerPanels = null;
// The tab panel, representing the webviewer left panel
static tabPanel = {
handle: null,
dataElement: 'tabPanel'
};
// The custom form sub-panel to be registered
static formPanel = {
handle: null,
dataElement: 'formPanel',
render: null,
};
// Function to set WebViewer 'loading' state
static setLoading(inst, isLoading) {
if (isLoading) {
inst.UI.openElements(['loadingModal']);
} else {
inst.UI.closeElements(['loadingModal']);
}
}
static setButtonStyle(button) {
button.style.margin = '10px';
button.style.padding = '5px 10px';
button.style.border = '1px solid #ccc';
button.style.borderRadius = '4px';
button.style.cursor = 'pointer';
button.style.fontSize = '14px';
button.style.fontWeight = 'bold';
button.style.transition = 'all 0.2s ease';
button.style.boxShadow = '0 2px 4px rgba(0,0,0,0.1)';
button.style.color = 'white';
button.style.backgroundColor = '#007bff';
}
// Customize the main webviewer left panel after the load completion
static customizeUI(instance, performOCR) {
const { UI } = instance;
// close the tab panel (if it's open) for refreshment.
UI.closeElements([this.tabPanel.dataElement]);
// Get the list of registered panels in the main webviewer
this.viewerPanels = UI.getPanels();
// Find the Tab Panel to modify. The form sub-panel will be added to this Tab panel.
this.tabPanel.handle = this.viewerPanels.find((panel) => panel.dataElement === this.tabPanel.dataElement);
// Register the custom form sub-panel
this.RegisterFormPanel(instance);
const languageLabel = document.createElement('h3');
languageLabel.textContent = "Select Language:";
this.formPanel.render.appendChild(languageLabel);
const languageSelect = document.createElement('select');
languageSelect.id = 'languageSelect';
for (const [langCode, langName] of Object.entries(this.languages)) {
const option = document.createElement('option');
option.value = langCode;
option.textContent = langName;
languageSelect.appendChild(option);
}
languageSelect.value = this.selectedLanguage;
languageSelect.onchange = () => {
this.selectedLanguage = languageSelect.value;
if (this.loadedFromPicker) // if the document is loaded from file picker once in the past, do not load using list again
return;
this.loadingFromList = true;
instance.UI.loadDocument(this.demoFilesPath + this.ocrdemofiles[this.selectedLanguage]);
};
this.formPanel.render.appendChild(languageSelect);
const loadLabel = document.createElement('label');
loadLabel.textContent = this.loadLabelText;
this.formPanel.render.appendChild(loadLabel);
this.fileLabel = document.createElement('h4');
this.fileLabel.textContent = `Current file: ${instance.Core.documentViewer.getDocument().getFilename()}`;
this.formPanel.render.appendChild(this.fileLabel);
// OCR button
const recognizeButton = document.createElement('button');
recognizeButton.textContent = 'Recognize Text';
recognizeButton.id = 'recognizeTextButton';
this.setButtonStyle(recognizeButton);
recognizeButton.onclick = async () => {
this.ocrText.textContent = "Performing OCR, please wait...";
this.setLoading(instance, true);
this.formPanel.render.style.cursor = "wait";
await performOCR(instance); // Perform text recognition
this.formPanel.render.style.cursor = "default";
}
this.formPanel.render.appendChild(recognizeButton);
let jsonDiv = document.createElement('div');
jsonDiv.id = 'json';
const jsonTitle = document.createElement('h3');
jsonTitle.textContent = "Exportable text";
jsonDiv.appendChild(jsonTitle);
jsonDiv.appendChild(document.createElement('p'));
this.jsonData = null;
this.ocrText = document.createElement('textarea');
this.ocrText.style.width = "350px";
this.ocrText.style.height = "350px";
this.ocrText.style.whiteSpace = "nowrap";
this.ocrText.readOnly = true;
this.ocrText.style.width = "350px";
this.ocrText.style.height = "350px";
this.ocrText.style.whiteSpace = "nowrap";
this.ocrText.readOnly = true;
this.ocrText.textContent = "Try applying OCR to this document\nby pressing the button above!";
jsonDiv.appendChild(this.ocrText);
this.formPanel.render.appendChild(jsonDiv);
// Add the new custom form sub-panel to list of sub-panels under the Tab Panel
this.formPanel.handle = { render: this.formPanel.dataElement };
this.tabPanel.handle.panelsList = [this.formPanel.handle, ...this.tabPanel.handle.panelsList];
UI.openElements([this.tabPanel.dataElement]);
UI.setPanelWidth(this.tabPanel.dataElement, 400);
};
// Register the custom form sub-panel
static RegisterFormPanel(instance) {
this.formPanel.render = this.createFormPanelElements(instance);
instance.UI.addPanel({
dataElement: this.formPanel.dataElement,
location: 'left',
icon: '<svg width="18px" height="18px" viewBox="0 0 24 24" id="圖層_1" data-name="圖層 1" xmlns="http://www.w3.org/2000/svg"><defs><style>.cls-1{fill:#080808;}</style></defs><title>form</title><path class="cls-1" d="M21,.5H3a2,2,0,0,0-2,2V22a2,2,0,0,0,2,2H21a2,2,0,0,0,2-2V2.5A2,2,0,0,0,21,.5Zm0,2v2H3v-2ZM3,22V6.5H21V22Z"/><path class="cls-1" d="M12.5,4H20a.5.5,0,0,0,0-1H12.5a.5.5,0,0,0,0,1Z"/><path class="cls-1" d="M4.5,4a.43.43,0,0,0,.19,0,.35.35,0,0,0,.16-.11A.47.47,0,0,0,5,3.5a.43.43,0,0,0,0-.19.36.36,0,0,0-.11-.16.5.5,0,0,0-.7,0A.35.35,0,0,0,4,3.31.43.43,0,0,0,4,3.5a.51.51,0,0,0,.5.5Z"/><path class="cls-1" d="M5.65,3.85A.36.36,0,0,0,5.81,4,.44.44,0,0,0,6,4a.47.47,0,0,0,.35-.15.36.36,0,0,0,.11-.16.6.6,0,0,0,0-.19.51.51,0,0,0-.15-.35A.49.49,0,0,0,5.81,3a.36.36,0,0,0-.16.11.47.47,0,0,0-.15.35.4.4,0,0,0,0,.19A.35.35,0,0,0,5.65,3.85Z"/><path class="cls-1" d="M8,8H4.5a1,1,0,0,0,0,2H8A1,1,0,0,0,8,8Z"/><path class="cls-1" d="M8,11.67H4.5a1,1,0,0,0,0,2H8a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M8,15.33H4.5a1,1,0,0,0,0,2H8a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M8,19H4.5a1,1,0,0,0,0,2H8a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M14,8H10.5a1,1,0,0,0,0,2H14a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M14,11.67H10.5a1,1,0,0,0,0,2H14a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M14,15.33H10.5a1,1,0,0,0,0,2H14a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M14,19H10.5a1,1,0,0,0,0,2H14a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M19.5,8h-3a1,1,0,0,0,0,2h3a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M19.5,11.67h-3a1,1,0,0,0,0,2h3a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M19.5,15.33h-3a1,1,0,0,0,0,2h3a1,1,0,0,0,0-2Z"/><path class="cls-1" d="M19.5,19h-3a1,1,0,0,0,0,2h3a1,1,0,0,0,0-2Z"/></svg>',
title: 'Form',
render: () => this.formPanel.render,
});
};
// Create the form panel elements.
static createFormPanelElements() {
let panelDiv = document.createElement('div');
panelDiv.id = 'form';
let paragraph = document.createTextNode('A demo of Apryse SDK OCR. Take a PDF and find text. (Note: This demo only processes the current page).');
panelDiv.appendChild(paragraph);
let dividerDiv = document.createElement('div');
dividerDiv.style.borderTop = '1px solid #ccc';
dividerDiv.style.margin = '10px 0';
panelDiv.appendChild(dividerDiv);
panelDiv.appendChild(document.createElement('p'));
return panelDiv;
}
}