Skip to content

PDF Generation Template Editor

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

View on GitHub

Tools ​

A tool decides what happens when the user works on the page: panning, selecting text, creating a highlight, drawing a rectangle, creating a form field, and so on. Tools are Core objects; the UI shows them as tool buttons.

Tool registry ​

Each DocumentViewer keeps a map of tool name → tool object. The built-in tool names are in instance.Core.Tools.ToolNames, for example:

ConstantTool name
PANPan
EDITAnnotationEdit
TEXT_SELECTTextSelect
HIGHLIGHTAnnotationCreateTextHighlight
RECTANGLEAnnotationCreateRectangle
LINEAnnotationCreateLine
FREETEXTAnnotationCreateFreeText
STICKYAnnotationCreateSticky
TEXT_FORM_FIELDTextFormFieldCreateTool
SIGNATUREAnnotationCreateSignature
CONTENT_EDITContentEditTool
SNIPPINGSnippingTool

instance.Core.documentViewer.getTool(toolName) returns the tool object.

UI APIs ​

UI.setToolMode(toolName, options?) ​

Activate a tool.

javascript
/**
 * @method UI.setToolMode
 * @param {string} toolName A tool name, such as 'AnnotationCreateRectangle'.
 * @param {object} [options]
 * @param {boolean} [options.continueAdding] Keep the tool active after an annotation is created.
 */
UI.setToolMode('AnnotationCreateRectangle');

The tool button of the active tool is shown as active. documentViewer fires toolModeUpdated.

UI.getToolMode() ​

Return the active tool object.

javascript
const tool = UI.getToolMode();
console.log(tool.name); // 'AnnotationCreateRectangle'

Tool defaults and styles ​

Read the default style of a tool from tool.defaults, and change it with tool.setStyles(styles):

javascript
const { documentViewer, Annotations } = instance.Core;
const rectangleTool = documentViewer.getTool('AnnotationCreateRectangle');
console.log(rectangleTool.defaults.StrokeColor);
rectangleTool.setStyles({
  StrokeColor: new Annotations.Color(255, 0, 0),
  StrokeThickness: 2,
});

UI.registerTool(tool) ​

Register a custom tool so it can be activated with setToolMode and shown by a ToolButton.

javascript
/**
 * @method UI.registerTool
 * @param {object} tool
 * @param {string} tool.toolName      Unique name of the tool.
 * @param {object} tool.toolObject    The tool object.
 * @param {string} [tool.buttonImage] Icon of the tool button.
 * @param {string} [tool.buttonName]  dataElement of the tool button.
 * @param {string} [tool.tooltip]     Tooltip of the tool button.
 */
const { documentViewer, Tools } = instance.Core;
UI.registerTool({
  toolName: 'MyRectangleTool',
  toolObject: new Tools.RectangleCreateTool(documentViewer),
  buttonImage: 'RectangleTool',
  buttonName: 'myRectangleToolButton',
  tooltip: 'My rectangle',
});

Related APIs:

APIPurpose
UI.updateTool(toolName, { buttonImage, buttonName, tooltip })Change the button information of a tool.
UI.unregisterTool(toolName)Remove a registered tool.
UI.disableTools(toolNames) / UI.enableTools(toolNames)Disable / enable tools and their buttons.
UI.isToolDisabled(toolName)Whether a tool is disabled.

Tool style presets ​

When the UI starts, it sets the default styles of the built-in tools (colors, line widths, fonts). Styles changed by the user in the style panel are remembered per tool, so the next annotation created with the tool uses the same style. UI.enableToolDefaultStyleUpdateFromAnnotationPopup() / disableToolDefaultStyleUpdateFromAnnotationPopup() control whether editing a selected annotation also updates the default style of its tool.

Tool categories ​

CategoryToolsToolbar group
ViewPan, TextSelect, SnippingTooltoolbarGroup-View
Annotationhighlight, underline, strikeout, squiggly, free hand, shapes, line, arrow, free text, callout, sticky note, stamp, linktoolbarGroup-Annotate
Formstext field, check box, radio button, list box, combo box, push button, signature fieldtoolbarGroup-Forms
Measurementdistance, perimeter, area, arctoolbarGroup-Measure
SignaturessignaturetoolbarGroup-FillAndSign
RedactionredactiontoolbarGroup-Redact
Content editingContentEditTool, add text, add imagetoolbarGroup-EditText

Examples ​

Activate the highlight tool and set its color ​

javascript
const { documentViewer, Annotations } = instance.Core;
UI.setToolbarGroup('toolbarGroup-Annotate');
UI.setToolMode('AnnotationCreateTextHighlight');
documentViewer.getTool('AnnotationCreateTextHighlight').setStyles({
  StrokeColor: new Annotations.Color(255, 205, 69),
});

Register a custom tool and add its button ​

javascript
const { documentViewer, Tools } = instance.Core;
UI.registerTool({
  toolName: 'MyEllipseTool',
  toolObject: new Tools.EllipseCreateTool(documentViewer),
  buttonImage: 'EllipseTool',
  tooltip: 'My ellipse',
});
const grouped = UI.getGroupedItems('annotateGroupedItems');
grouped.setItems([
  ...grouped.items,
  new UI.Components.ToolButton({ dataElement: 'myEllipseToolButton', toolName: 'MyEllipseTool' }),
]);
UI.setToolbarGroup('toolbarGroup-Annotate');
UI.setToolMode('MyEllipseTool');