Add rich text PDF annotations

WebViewer supports rich text content for freetext annotations that allows users to set the font styles for specific characters to be bold, italic, underline, strikeout, or change them to have different colors. The rich text content is implemented according to the PDF specification, which means it will work with other PDF viewers.

In WebViewer 7.3 the rich text popup is disabled by default in the WebViewer UI because if you are merging XFDF with the Apryse SDK server side then rich text appearances will be lost (though the rich text data is still there). If you are not merging XFDF using the Apryse SDK on the server side then you can safely re-enable this using the enableElements API (see section).

Apryse Docs Image

Relevant APIs and events

WebViewer provides some APIs and useful events for customizing the default behaviors.

enableElements

Just like most of other elements in the viewer, the rich text popup or each element in the rich text popup can be enabled by using enableElements.

1WebViewer(...)
2 .then(instance => {
3 instance.UI.enableElements([
4 'richTextPopup',
5 // elements specific to rich text popup
6 'richTextUnderlineButton',
7 'richTextItalicButton',
8 'richTextColorPalette',
9 ]);
10 });

disableElements

Apryse Docs Image

Just like most of other elements in the viewer, each element in the rich text popup can be disabled by using disableElements.

1WebViewer(...)
2 .then(instance => {
3 instance.UI.disableElements([
4 'richTextUnderlineButton',
5 'richTextItalicButton',
6 'richTextColorPalette',
7 ]);
8 });

Use the approach talked about in this guide to find all the available dataElements in the popup.

setColorPalette

It is also possible to configure the color palette to use a different set of colors by using the setColorPalette API.

1WebViewer(...)
2 .then(instance => {
3 const { Tools } = instance.Core;
4
5 instance.UI.setColorPalette({
6 toolNames: [Tools.ToolNames.FREETEXT],
7 colors: [
8 '#DDDDDD',
9 '#9de8e8',
10 '#A6A1E6',
11 '#E2A1E6',
12 '#EF1234',
13 '#FF8D00',
14 '#FFCD45',
15 ],
16 });
17 });

getEditBoxManager

The edit box manager controls all the editor instances that belong to freetext annotations. It also exposes some useful events that can be used to check the changes in an editor.

1WebViewer(...)
2 .then(instance => {
3 const editBoxManager = instance.Core.annotationManager.getEditBoxManager();
4
5 editBoxManager.addEventListener('editorFocus', (editor, annotation) => {...})
6 editBoxManager.addEventListener('editorBlur', (editor, annotation) => {...})
7 editBoxManager.addEventListener('editorTextChanged', () => {...})
8 editBoxManager.addEventListener('editorSelectionChanged', (range, oldRange) => {...})
9 });

getEditor

Each freetext annotation holds an editor instance which provides some low level APIs for configuring the editor.

JavaScript

1WebViewer(...)
2 .then(instance => {
3 const editor = freetextAnnot.getEditor();
4
5 // set the active editor color to be #EF1234
6 editor.format('color', '#EF1234');
7
8 const format = editor.getFormat();
9 console.log(format); // { color: '#EF1234' }
10 });

To find more editor APIs, take a look at the documentation

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales