Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Customize Panels ​

Panels are dynamic components in WebViewer UI, designed to enhance user interaction by displaying content either on the left or right side. This guide provides detailed instructions and examples for incorporating both prebuilt and custom panels into your UI, offering a seamless integration process.

Prebuilt Panels ​

The UI includes a comprehensive selection of preconfigured panels designed to address common functionality requirements, such as thumbnail manipulation and document outline navigation. Through the use of the provided APIs, you can seamlessly integrate these panels into your UI. Additionally, you have the flexibility to customize various aspects of their behavior, such as the side of the UI from which they open.

List of Prebuilt Panels ​

  • tabPanel
  • formFieldPanel
  • pageModePanel
  • rubberStampPanel
  • linkPanel
  • textEditingPanel
  • stylePanel
  • colorSeparationPanel

The left panel tabPanel is a tab panel. Its tabs are the prebuilt panels thumbnailsPanel, outlinesPanel, notesPanel, layersPanel, signaturePanel and searchPanel.

Integrating Prebuilt Panels ​

To incorporate a prebuilt panel, utilize the addPanel API, specifying necessary properties such as dataElement, location and render. For prebuilt panels, render corresponds to a reserved identifier, while for custom panels, it involves a function returning the panel element.

javascript
// Menu Panel
instance.UI.addPanel({
  dataElement: 'tabPanel',
  location: 'left',
  render: 'tabPanel'
});

// Page Mode Panel
instance.UI.addPanel({
  dataElement: 'pageModePanel',
  location: 'right',
  render: 'pageModePanel'
});

To open the panels, you can use the API openElement with the dataElement of the panel. You can also add a toggle button to your UI and use the selector of the panel as the toggleElement of the button.

javascript
// Using the openElement API to open the Page Mode panel
instance.UI.openElement('pageModePanel');

// Using the toggle button to open the Page Mode panel
const newToggleButton = new instance.UI.Components.ToggleElementButton({
  toggleElement: 'pageModePanel',
  dataElement: 'pageModePanelButton',
  label: 'Page Mode'
});
const header = instance.UI.getModularHeader('default-top-header');
header.setItems([...header.getItems(), newToggleButton]);

Adding Custom Panels ​

If you want to customize your UI even more, the Modular UI framework lets you build panels exactly the way you want. This means you can create special components that fit perfectly with what your app needs. You still use the same properties when creating panels with the prebuilt options. However, instead of passing a panel element identifier directly to the render property, you pass a function that returns the panel element. This element can be a DOM element built with JavaScript, giving you the flexibility to tailor your UI components to your specific requirements.

javascript
// Adding a panel built with JS and DOM elements
instance.UI.addPanel({
  dataElement: 'customPanel',
  location: 'left',
  render: () => {
    const div = document.createElement('div');
    div.innerHTML = 'My custom panel';
    return div;
  }
});

// You can then add a toggleElement button for this panel
const customPanelToggle = new instance.UI.Components.ToggleElementButton({
  dataElement: 'customPanelToggle',
  toggleElement: 'customPanel',
  label: 'Custom Panel'
});
const header = instance.UI.getModularHeader('default-top-header');
header.setItems([...header.getItems(), customPanelToggle]);

Working with Panels ​

addPanel ​

To incorporate panels into your UI, begin by registering them with the addPanel method. This step makes the panels ready for use within your interface. For detailed guidance on utilizing this method, including all available options, refer to the earlier sections of this documentation.

javascript
instance.UI.addPanel({
  dataElement: 'customPanel',
  location: 'left',
  render: () => {
    const div = document.createElement('div');
    div.innerHTML = 'My custom panel';
    return div;
  }
});

getPanels ​

This API fetches a comprehensive list of all the modular panels that can be utilized in the current UI. Once you have this list, you can select any specific panel and modify its properties using the APIs described below.

javascript
const panelsList = instance.UI.getPanels();

setPanels ​

If you want to modify the list of panels, you can make the changes in the list using JavaScript methods to modify arrays and then you should call the setPanels API with the modified list as a parameter to update the panels list.

javascript
const panelsList = instance.UI.getPanels();

panelsList.pop();

instance.UI.setPanels(panelsList);

delete ​

This API deletes a panel from the UI, rendering it inaccessible for future usage. The deleted panel will remain in the panels' list but it will not be rendered in the UI.

javascript
const panelsList = instance.UI.getPanels();

// Deleting the first panel from the UI
panelsList[0].delete();

setLocation ​

This method allows you to change the location of a panel within the Modular UI. It receives a string indicating the new location where the panel should be positioned. The available values for the location property are right and left.

javascript
const panelsList = instance.UI.getPanels();

// Find the panel you want to change the location of ie. pageModePanel
const panel = panelsList.find(panel => panel.dataElement === 'pageModePanel');

// Setting its location to the left
panel.setLocation('left');

// You can also set the location via the property
panel.location = 'left';

Open/Close the panel ​

You can use the openElement/closeElements method to open/close the specified panel. Use the isElementOpen method to determine whether the specified panel is in an open state.

javascript
// Open the left panel
instance.UI.openElement('tabPanel');
instance.UI.openElements(['tabPanel']);

// Close the left panel
instance.UI.closeElements(['tabPanel']);

// Is the left panel open
instance.UI.isElementOpen('tabPanel');

Switch tags ​

If you want to switch between tabs in a tab panel, you need to first determine the parameters of the panel and the tabs, and then use the setActiveTabInPanel to switch the tabs.

For example, switch the left panel to the annotation tag:

  1. Click the arrow of the left panel button in the header to open the tab dropdown, then find the DOM element of the annotation tab in the DOM viewer of the browser. Each tab's data-element is its panel name followed by Tab; left panel dom

  2. Confirm that the annotation tab is notesPanelTab, so its panel is notesPanel;

  3. Use the setActiveTabInPanel method to pass in the specified tab panel (tabPanel) and tab (notesPanel) parameters.

javascript
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";

const viewer = document.getElementById('webviewer');

ComPDFKitViewer({
  path: '/',
  initialDoc: 'Your PDF Url',
  licenseKey: 'Input your license here'
}, viewer)
  .then((instance) => {
    const { documentViewer } = instance.Core;
    const { UI } = instance;

    documentViewer.addEventListener('documentLoaded', async () => {
      console.log('document loaded');
      // Open the left panel
      UI.openElement('tabPanel');

      // Switch to the annotation tag in the left panel
      UI.setActiveTabInPanel({ tabPanel: 'tabPanel', tabName: 'notesPanel' });
    });
  });