Some test text!

Customizing header

In this document
chevron_rightHeader composition
chevron_rightHeader items
chevron_rightActionButton
chevron_rightStatefulButton
chevron_rightToggleElementButton
chevron_rightToolButton
chevron_rightToolGroupButton
chevron_rightCustomElement
chevron_rightSpacer
chevron_rightDivider
chevron_rightRelevant APIs

There are a number of ways you may want to customize the header. To name a few:

  • Removing all annotation tool buttons
  • Reordering annotation tool buttons
  • Replacing the existing view controls buttons with custom buttons
  • Adding a custom annotation tool button
  • Replace the entire header with different items in smaller screens

The WebViewer UI provides a flexible API to easily handle each of these cases and more.

linkHeader composition

The header is represented by an array of header items and you can edit the array to add/remove/reorder them however you want. The API, setHeaderItems, will provide a header object as a function argument.

For cases where the user wants to replace the entire header items back and forth, the header items are grouped, and displayed only when the group is active:

linkHeader items

Header items are objects with certain properties. If you wish to add a header item, it is important for you to understand what type it is and what properties should be used.

linkActionButton

ActionButton triggers an action. The button has no active state.

Properties

  • type (string) - Must be set to actionButton.
  • img (string) - Path to an image or base64 data.
  • onClick (function) - Function to be triggered on click.
  • title (string, optional) - Tooltip of the button.
  • dataElement (string, optional) - Option to set data-element value of the button element. It can be used to disable/enable the element.
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

const newActionButton = {
  type: 'actionButton',
  img: 'path/to/image',
  onClick: () => alert('Hello world!'),
  dataElement: 'alertButton',
  hidden: [ 'mobile' ]
};

linkStatefulButton

StatefulButton is a customizable button. You can decide how many states it has, what state is active and when to update the state.

Properties

  • type (string) - Must be set to statefulButton.
  • states (object) - Object in the shape of: { nameOfState1: state1, nameOfState2: state2, ... }

    • Properties of a state:
    • img (string, optional): Path to an image or base64 data.
    • getContent (function, optional): Function to be called when you update the state. Define this property if you don't use the img property for this button. Argument: activeState.
    • onClick (function): Function to be triggered on click. Arguments: update, activeState.
    • Any other properties you need.
  • mount (function) - Function that returns the name of initial state. Argument: update
  • unmount (function, optional) - Function to be called before the button is unmounted.
  • dataElement (string, optional) - String to set data-element value of the button element. It can be used to disable/enable the element.
  • title (string, optional) - Tooltip of the button.
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

A stateful button that shows the count. And when you click it, it will increment the counter by 1.

const countButton = {
  type: 'statefulButton',
  states: {
    Count: {
      number: 1,
      getContent: (activeState) => activeState.number,
      onClick: (update, activeState) => {
        activeState.number += 1;
        update();
      }
    }
  },
  mount: () => {
    return 'Count';
  },
  dataElement: 'countButton'
};

A stateful button that shows the current page number. And when you click it, the document will go to next page. If you are already at the last page, the document will go to the first page.

const nextPageButton = {
  type: 'statefulButton',
  states: {
    Page: {
      // Checkout https://www.pdftron.com/documentation/web/guides/ui/apis to see more APIs related with viewerInstance
      getContent: () => viewerInstance.getCurrentPageNumber(),
      onClick: () => {
        const currentPage = viewerInstance.getCurrentPageNumber();
        const totalPages = viewerInstance.getPageCount();
        const atLastPage = currentPage === totalPages;

        if (atLastPage) {
          viewerInstance.goToFirstPage();
        } else {
          viewerInstance.goToNextPage();
        }
      }
    }
  },
  mount: (update) => {
    // Checkout https://www.pdftron.com/api/web/CoreControls.DocumentViewer.html to see more APIs and events with docViewer
    viewerInstance.docViewer.on('pageNumberUpdated.nextPageButton', () => {
      // We want to update this button when page number changes so it can have the correct content;
      update();
    });

    // Since this stateful button has only one state, we just return the name of that state
    return 'Page';
  },
  unmount: () => {
    viewerInstance.docViewer.off('pageNumberUpdated.nextPageButton')
  },
  dataElement: 'nextPageButton'
};

A stateful button that changes the fit mode of the document.

This button is in our built-in UI, checkout it out at https://www.pdftron.com/webviewer/demo/.

const fitButton = {
  type: 'statefulButton',
  states: {
    FitWidth: {
      img: 'path/to/fitWidth/image',
      onClick: () => viewerInstance.setFitMode(viewerInstance.FitMode.FitWidth),
    },
    FitPage: {
      img: 'path/to/fitPage/image',
      onClick: () => viewerInstance.setFitMode(viewerInstance.FitMode.FitPage),
    }
  },
  mount: (update) => {
    const fitModeToState = (fitMode) => {
      // the returned state should be the opposite of the new current state
      // as the opposite state is what we want to switch to when the button
      // is clicked next
      if (fitMode === viewerInstance.docViewer.FitMode.FitPage) {
        return 'FitWidth';
      } else if (fitMode === viewerInstance.docViewer.FitMode.FitWidth) {
        return 'FitPage';
      }
    };

    viewerInstance.docViewer.on('fitModeUpdated.fitbutton', (e, fitMode) => {
      update(fitModeToState(fitMode));
    });

    // if initial fit mode is zoom then default to FitPage
    return fitModeToState(viewerInstance.docViewer.getFitMode()) || 'FitPage';
  },
  unmount: () => {
    viewerInstance.docViewer.off('fitModeUpdated.fitbutton');
  },
  dataElement: 'fitButton',
  hidden: ['mobile']
};

linkToggleElementButton

ToggleElementButton opens/closes a UI element. The button is in an active state when the UI element is open.

Properties

  • type (string) - Must be set to toggleElementButton.
  • img (string) - Path to an image or base64 data.
  • element (string) - data-element attribute value of the UI element to be opened/closed.
  • dataElement (string, optional) - Option to set data-element value of the button element. It can be used to disable/enable the element.
  • title (string, optional) - Tooltip of the button.
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

const newToggleElementButton = {
  type: 'toggleElementButton',
  img: 'path/to/image',
  element: 'leftPanel',
  dataElement: 'leftPanelButton',
  hidden: [ 'mobile' ]
};

linkToolButton

ToolButton activates a WebViewer tool. The button is in an active state when the tool is activated. To learn more about customizing annotation tools and tool buttons, see customizing annotation tools.

Properties

  • type (string) - Must be set to toolButton.
  • toolName (string) - Name of the tool, which is either the key from toolModeMap or the name you registered your custom tool with. For custom tool registration, refer to registerTool.
  • title (string, optional) - Tooltip of the button.
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

const newToolButton = {
  type: 'toolButton',
  toolName: 'AnnotationCreateFreeText',
  hidden: [ 'mobile' ]
}

linkToolGroupButton

ToolGroupButton opens an overlay with the tools in the tool group. Unless the img option is set, the button displays the image of one of the group members. The button is in an active state when any tool in the group is active. To learn more about customizing annotation tools and tool buttons, see customizing annotation tools.

Properties

  • type (string) - Must be set to 'toolGroupButton'.
  • toolGroup (string) - Name of the tool group. In the default viewer, there are freeHandTools, textTools, shapeTools and miscTools.
  • img (string, optional) - Path to an image or base64 data.
  • dataElement (string, optional) - Option to set data-element value of the button element. It can be used to disable/enable the element.
  • title (string, optional) - Tooltip of the button.
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

const newToolGroupButton = {
  type: 'toolGroupButton',
  toolGroup: 'shapeTools',
  dataElement: 'shapeToolGroupButton',
  hidden: [ 'mobile' ]
};

linkCustomElement

CustomElement takes a render function and renders DOM elements or React components. You may want to use this when the buttons above don't suffice.

Properties

  • type (string) - Must be set to 'customElement'.
  • title (string, optional) - Tooltip of the button.
  • render (func) - Function that returns DOM elements or React components
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

const renderSlider = () => {
  const slider = document.createElement('input');
  slider.type = 'range';
  slider.oninput = () => {
    // Do something
  };

  return slider;
}

const newCustomElement = {
  type: 'customElement',
  render: renderSlider
};

In React jsx:

const Slider = () => {
  return (
    <input
      type="range"
      onInput={() => { /* Do something */}}
    >
    </input>
  );
}

const newCustomElement = {
  type: 'customElement',
  render: () => <Slider />
};

linkSpacer

Spacer is just a div with flex: 1 to occupy any remaining space. It is used to push the buttons to each side on the default header.

Properties

  • type (string) - Must be set to 'spacer'.
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

const newSpacer = {
  type: 'spacer',
  hidden: [ 'mobile' ]
};

linkDivider

Divider renders a vertical bar with some margin to separate item groups.

Properties

  • type (string) - Must be set to 'divider'.
  • hidden (array of strings, optional) - Option to hide the element at certain screen sizes. Accepted strings are desktop, tablet and mobile.

Example

const newDivider = {
  type: 'divider',
  hidden: [ 'mobile' ]
};

linkRelevant APIs

To add/remove/re-order header items, you can use the following API:

For ToolButton, make sure you register/unregister the tool using:

To switch a header group, you can use the following API: