PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
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.
Each DocumentViewer keeps a map of tool name → tool object. The built-in tool names are in instance.Core.Tools.ToolNames, for example:
| Constant | Tool name |
|---|---|
PAN | Pan |
EDIT | AnnotationEdit |
TEXT_SELECT | TextSelect |
HIGHLIGHT | AnnotationCreateTextHighlight |
RECTANGLE | AnnotationCreateRectangle |
LINE | AnnotationCreateLine |
FREETEXT | AnnotationCreateFreeText |
STICKY | AnnotationCreateSticky |
TEXT_FORM_FIELD | TextFormFieldCreateTool |
SIGNATURE | AnnotationCreateSignature |
CONTENT_EDIT | ContentEditTool |
SNIPPING | SnippingTool |
instance.Core.documentViewer.getTool(toolName) returns the tool object.
UI.setToolMode(toolName, options?) Activate a tool.
/**
* @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.
const tool = UI.getToolMode();
console.log(tool.name); // 'AnnotationCreateRectangle'Read the default style of a tool from tool.defaults, and change it with tool.setStyles(styles):
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.
/**
* @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:
| API | Purpose |
|---|---|
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. |
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.
| Category | Tools | Toolbar group |
|---|---|---|
| View | Pan, TextSelect, SnippingTool | toolbarGroup-View |
| Annotation | highlight, underline, strikeout, squiggly, free hand, shapes, line, arrow, free text, callout, sticky note, stamp, link | toolbarGroup-Annotate |
| Forms | text field, check box, radio button, list box, combo box, push button, signature field | toolbarGroup-Forms |
| Measurement | distance, perimeter, area, arc | toolbarGroup-Measure |
| Signatures | signature | toolbarGroup-FillAndSign |
| Redaction | redaction | toolbarGroup-Redact |
| Content editing | ContentEditTool, add text, add image | toolbarGroup-EditText |
const { documentViewer, Annotations } = instance.Core;
UI.setToolbarGroup('toolbarGroup-Annotate');
UI.setToolMode('AnnotationCreateTextHighlight');
documentViewer.getTool('AnnotationCreateTextHighlight').setStyles({
StrokeColor: new Annotations.Color(255, 205, 69),
});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');