Skip to content

PDF Generation Template Editor

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

View on GitHub

Header & Toolbar ​

The header and the toolbar are built from modular components. This guide explains their structure and the APIs that change them.

UI APIs ​

UI.getModularHeader(dataElement) ​

Returns the header object. Its items can be read with getItems() and replaced with setItems(items).

javascript
/**
 * @method UI.getModularHeader
 * @param {string} dataElement The header, such as 'default-top-header' or 'tools-header'.
 * @returns {UI.Components.ModularHeader}
 */
const header = UI.getModularHeader('default-top-header');
console.log(header.getItems().map(item => item.dataElement));

The header object also provides getGroupedItems(), setGap(gap), setJustifyContent(value), setMaxWidth(width), setMaxHeight(height) and setStyle(style). UI.getModularHeaderList() returns all headers.

Headers and components ​

APIPurpose
UI.getModularHeader(id) / UI.getModularHeaderList()Get one / all headers.
UI.addModularHeaders(headers)Add headers created with UI.Components.ModularHeader (or TopHeader, BottomHeader, LeftHeader, RightHeader).
UI.setModularHeaders(headers)Replace all headers.
UI.getGroupedItems(id)Get a grouped items component; change it with setItems(items).
UI.getRibbonGroup(id)Get the ribbon group; change it with setItems(items).
UI.exportModularComponents() / UI.importModularComponents(data, functionMap)Export / import the whole modular UI as data.

UI.setToolbarGroup(group) ​

Switch the active toolbar group (the ribbon item that is selected, and the tools shown in tools-header).

javascript
/**
 * @method UI.setToolbarGroup
 * @param {string} group A value of UI.ToolbarGroup, such as 'toolbarGroup-Annotate'.
 */
UI.setToolbarGroup('toolbarGroup-Annotate');

Valid groups: toolbarGroup-View, toolbarGroup-Annotate, toolbarGroup-Forms, toolbarGroup-Measure, toolbarGroup-FillAndSign, toolbarGroup-Security, toolbarGroup-Redact, toolbarGroup-Compare, toolbarGroup-EditText, toolbarGroup-Document, toolbarGroup-Separation, plus the custom groups toolbarGroup-Shapes, toolbarGroup-Insert and toolbarGroup-Edit. UI.Events.TOOLBAR_GROUP_CHANGED fires when the group changes.

Toolbar structure ​

Headers ​

HeaderContents
default-top-headerleftPanelButton, fullscreenButton, selectToolsButton, panToolButton, zoom-container, snippingToolButton, textSelectToolButton, the ribbon group default-ribbon-group, searchPanelToggle, and menuButton (opens the MainMenuFlyout).
tools-headerThe grouped items of the active toolbar group.
page-editor-headerThe header shown in the page editor (cancel, save, save as).
compare-headerThe header shown in document comparison.

Ribbon items and grouped items ​

Each ribbon item in default-ribbon-group names a toolbar group and the grouped items it shows:

Ribbon itemGrouped items
toolbarGroup-View—
toolbarGroup-AnnotateannotateGroupedItems
toolbarGroup-FormsformsGroupedItems
toolbarGroup-MeasuremeasureGroupedItems
toolbarGroup-FillAndSignfillAndSignGroupedItems
toolbarGroup-SecuritysecurityGroupedItems
toolbarGroup-RedactredactionGroupedItems
toolbarGroup-Compare—
toolbarGroup-EditTexteditTextGroupedItems
toolbarGroup-Document—
toolbarGroup-Separation—

When the ribbon does not fit, the items that do not fit are collected into a "···" menu.

Item types ​

Items are created with the classes in instance.UI.Components:

ClasstypeRenders as
CustomButtoncustomButtonA button that runs onClick.
ToolButtontoolButtonA button that activates the tool toolName.
ToggleElementButtontoggleButtonA button that opens/closes toggleElement.
StatefulButtonstatefulButtonA button with several states.
PresetButtonpresetButtonA built-in button (UI.PRESET_BUTTON_TYPES), such as downloadButton or printButton.
GroupedItemsgroupedItemsA group of items.
RibbonGroup / RibbonItemribbonGroup / ribbonItemThe toolbar group switcher and its items.
Flyout / MainMenuflyoutA drop-down menu.
Zoom, PageControls, ViewControls, Labelzoom, pageControls, viewControls, labelBuilt-in widgets and a text label.
CustomElementcustomElementAny element returned by render().
DividerdividerA vertical separator.

Every item accepts hidden: true hides it, and an array of screen sizes (desktop, tablet, mobile, small-mobile) hides it only on those sizes. An empty GroupedItems with grow: 1 works as a spacer that pushes the following items to the other side.

See Customize the Navigation Bar for the properties of each item.

How setItems works internally ​

Headers, grouped items, ribbon groups and their items are stored as plain data in the viewer store, keyed by dataElement. setItems(items) stores each item under its dataElement (replacing an item with the same dataElement) and writes the list of dataElement values back to the header or group. The header components re-render from the store. Grouped items are rendered only when they are active, or when alwaysVisible is true.

Examples ​

Remove a button by data-element ​

javascript
const header = UI.getModularHeader('default-top-header');
header.setItems(header.getItems().filter(item => item.dataElement !== 'snippingToolButton'));

Insert a custom button before an existing one ​

javascript
const header = UI.getModularHeader('default-top-header');
const items = header.getItems();
const index = items.findIndex(item => item.dataElement === 'searchPanelToggle');
items.splice(index, 0, new UI.Components.CustomButton({
  dataElement: 'myAction',
  label: 'My Action',
  onClick: () => console.log('clicked'),
}));
header.setItems(items);

Replace an entire group's items ​

javascript
const { Components } = UI;
UI.getGroupedItems('annotateGroupedItems').setItems([
  new Components.ToolButton({ dataElement: 'highlightToolButton', toolName: 'AnnotationCreateTextHighlight' }),
  new Components.ToolButton({ dataElement: 'rectangleToolButton', toolName: 'AnnotationCreateRectangle' }),
]);

Add a custom toolbar group ​

javascript
const { Components } = UI;
const myTools = new Components.GroupedItems({
  dataElement: 'myGroupedItems',
  items: [
    new Components.ToolButton({ dataElement: 'myRectangleButton', toolName: 'AnnotationCreateRectangle', img: 'RectangleTool' }),
  ],
});
const toolsHeader = UI.getModularHeader('tools-header');
toolsHeader.setItems([...toolsHeader.getItems(), myTools]);

const ribbonGroup = UI.getRibbonGroup('default-ribbon-group');
ribbonGroup.setItems([
  ...ribbonGroup.items,
  new Components.RibbonItem({
    dataElement: 'toolbarGroup-Shapes',
    label: 'Shapes',
    toolbarGroup: 'toolbarGroup-Shapes',
    groupedItems: ['myGroupedItems'],
  }),
]);
UI.setToolbarGroup('toolbarGroup-Shapes');

Add an item to the main menu ​

javascript
const mainMenu = UI.Flyouts.getFlyout('MainMenuFlyout');
mainMenu.setItems([
  ...mainMenu.items,
  { dataElement: 'aboutButton', label: 'About', onClick: () => alert('ComPDFKit WebViewer') },
]);