Content edit

WebViewer 10.3 adds support for WYSIWYG PDF content editing which allows you to edit text and images directly on PDF files.

Apryse Docs Image

Content editing is a new add-on that isn't part of existing license keys. To test it out you can comment out your license key and try in demo mode.

This feature only works with PDF files. If you want to edit Word documents you can try our DOCX Editor. For other type of files you can call the loadDocument function with the loadAsPDF: true option. For example documentViewer.loadDocument('myfile.xlsx', { loadAsPDF: true })

Enable content editing feature with the WebViewer UI

To use the content editing feature with the WebViewer UI you need to enable the content edit feature in the UI.

JavaScript

1WebViewer({
2 // options
3}, document.getElementById('viewer'))
4.then((instance) => {
5 // Enable the tools to edit the PDF content
6 instance.UI.enableFeatures([instance.UI.Feature.ContentEdit]);
7});

You will see the "Edit Text" toolbar group after the UI is loaded.

Apryse Docs Image

To proceed, click the "Edit Text" button or switch to content edit mode programmatically:

JavaScript

1instance.UI.setToolbarGroup(instance.UI.ToolbarGroup.EDIT_TEXT);

The editable text and images in the document will be displayed with a dashed box around them and a side panel will be open with the text editing tools.

Apryse Docs Image

Editing content from the UI

Text and Images

To edit the text you can either:

  • Double click on the box

or

  • Single click on the box to select it
  • Click the edit button in the annotation popup menu

After this the text will become editable and once you've finished making changes you can click outside the box to have the text updated on the page.

Both text and images can be moved, resized and deleted. You can do this by selecting a box and then moving or resizing it like a normal annotation. You can also press the delete button to delete the content.

Adding Paragraphs

To add a new paragraph, you can select the Add Paragraph tool button and then click and drag to create a rectangle that your new text will go inside. You can then start typing to add text and use the available tools in the right panel to adjust properties of the text.

Apryse Docs Image

Adding Images

To add a new image, you can select the Add Image tool button and click on the document to choose where to place the image. The system file picker will open and you can select an image. After this, you can resize it, change its location or delete it.

Using content editing with the WebViewer Core

If you use the WebViewer Core directly then there are some APIs provided to interact with content editing. As an example you can check out the WebViewer Custom UI Github repo which makes use of these APIs.

To begin editing content you'll have to call the method startContentEditMode.

JavaScript

1const contentEditManager = Core.getDocumentViewer().getContentEditManager();
2contentEditManager.startContentEditMode();

To add new paragraphs, there is a tool called AddParagraphTool that you can switch into. For example:

JavaScript

1const addParagraphTool = myDocumentViewer.getTool(Core.Tools.ToolNames.ADD_PARAGRAPH);
2myDocumentViewer.setToolMode(addParagraphTool);

You can also add images switching to the AddImageContentTool.

JavaScript

1const addImageContentTool = myDocumentViewer.getTool(Core.Tools.ToolNames.ADD_IMAGE_CONTENT);
2myDocumentViewer.setToolMode(addImageContentTool);

If you need to finalize the editing content mode, you can call the endContentEditMode method.

JavaScript

1const contentEditManager = Core.getDocumentViewer().getContentEditManager();
2contentEditManager.endContentEditMode();

The first time the tool mode is switched into the content edit mode WebAssembly files will be loaded. If you'd like to do this earlier you can use the preloadWorker method.

JavaScript

1Core.ContentEdit.preloadWorker(myDocumentViewer);

Working with content edit placeholder annotations

You can tell if an annotation is a content edit placeholder annotation by using the annotation.isContentEditPlaceHolder API. With those annotations you can use the getDocumentContent and updateDocumentContent APIs. For example:

JavaScript

1// is the currently selected annotation a placeholder
2const selectedAnnotation = annotationManager.getSelectedAnnotations()[0];
3if (selectedAnnotation && selectedAnnotation.isContentEditPlaceholder()) {
4 const content = await Core.ContentEdit.getDocumentContent(selectedAnnotation);
5
6 // pass content to library that can display rich text, for example Quill
7}
8
9
10// later after the content has been updated this will update it on the page
11await Core.ContentEdit.updateDocumentContent(annotation, newContent);

If you want to check whether the annotation is for text or an image then you can use getContentEditType. For example:

JavaScript

1if (annotation.getContentEditType() === Core.ContentEditTypes.TEXT) {
2 // this has text that can be updated
3} else if (annotation.getContentEditType() === Core.ContentEditTypes.OBJECT) {
4 // this has the image that can be updated
5}

Moving and deleting content

When the annotations representing the content are moved or deleted in the DocumentViewer then the content will automatically be updated on the page. You can also use the normal annotation APIs.

JavaScript

1annotationManager.deleteAnnotation(myContentEditAnnotation);
2
3myOtherContentEditAnnotation.X = 50;
4annotationManager.trigger(Core.AnnotationManager.Events.ANNOTATION_CHANGED, ['modify', [myOtherContentEditAnnotation], {}]);

Setting the text style programatically

You have the ability to programmatically set the editor to apply a specific font style to the next characters. This style will remain active until it is updated by a subsequent selection or another call to the API.

JavaScript

1const fontStyle = {
2 "bold": false,
3 "italic": false,
4 "underline": true,
5 "fontName": "Cousine",
6 "fontSize": "12",
7 "fontColor": "#FF00E8"
8}
9Core.ContentEdit.setTextAttributes(fontStyle);

By using the provided code snippet, you can effortlessly define the desired font style, such as bold, italic, and underline, along with the font name and size. The changes will be applied to the text within the editor, maintaining the style until further modifications are made through subsequent selections or API calls.

Getting the text style from an active selection within a Content Edit Box

When you are editing text within a Content Edit Box, you have the ability to retrieve the current Text Attributes of your selection. This can be useful if you want to update your UI or confirm text properties as a user navigates the text.

First, attach a function to the contentBoxEditStarted event that will store the reference to the active Content Box editor.

JavaScript

1const handleEditorStarted = ({ editor }) => {
2 // Store this editor reference for use later.
3 // We will assume that reference is a variable 'contentEditorRef'.
4 contentEditorRef = editor;
5};
6contentEditManager.addEventListener('contentBoxEditStarted', handleEditorStarted);

Secondly, attach a function to the contentEditSelectionChange event. This function should call the referenced editor we stored earlier to retrieve the Text Attributes with the getTextAttributes method. Once we obtain the attributes from the current selection, we can extract the various properties.

JavaScript

1const handleSelectionChange = async () => {
2 if (contentEditorRef) {
3 const attribute = await contentEditorRef.getTextAttributes();
4 const { fontSize, fontName, fontColor, bold, italic, underline, textAlign } = attribute;
5 }
6};
7contentEditManager.addEventListener('contentEditSelectionChange', handleSelectionChange);

Getting the text style from a selected Content Edit Box

You can extract the Text Style (e.g., font size, color) from a Content Edit Box that isn't in edit mode. This process fetches the first instance of each property. For instance, if a box has both 12-point and 30-point fonts, with the 12-point appearing first, the output will be 12 points.

To do this:

  • Select an Annotation
  • Obtain the contentEditBoxId
  • Request the Content Box Attributes from the Content Edit Manager

JavaScript

1const contentBoxId = annotation.getCustomData('contentEditBoxId');
2const contentEditManager = Core.getDocumentViewer().getContentEditManager();
3const textAttributes = await contentEditManager.getContentBoxAttributes(contentBoxId);

Undo / Redo changes programmatically

You have the capability to programmatically undo or redo changes made during PDF text edits. This functionality encompasses actions such as character additions/deletions, style modifications, and adjustments to content boxes. These changes are managed through the Content Edit History Manager.

JavaScript

1// to undo a change
2await Core.getDocumentViewer().getContentEditHistoryManager().undo();
3// to redo a change
4await Core.getDocumentViewer().getContentEditHistoryManager().redo();
5// to check if there is an available change to undo
6const canUndo = Core.getDocumentViewer().getContentEditHistoryManager().canUndo();
7// to check if there is an availabe change to redo
8const canRedo = Core.getDocumentViewer().getContentEditHistoryManager().canRedo();

Using the provided code snippet, you can easily initiate undo and redo operations programmatically. The undo() function allows you to reverse the most recent change, while the redo() function re-applies a previously undone change. Additionally, you can utilize the canUndo() and canRedo() functions to check if there are available changes to be undone or redone, respectively. This grants you control over the revision history and enables a seamless editing experience.

Starting with WebViewer 10.7, we've introduced an enhanced feature that displays a link annotation popup. This popup includes link information and an unlink button, which can be triggered simply by hovering over any link annotations within a document.

Apryse Docs Image

Currently, there is no direct API to remove a hyperlink that has been added in Content Edit Mode. However, an alternative approach is available, allowing you to detect the link annotation you've added, store it, and then perform the deletion action.

Utilize the mouseMove event in conjunction with the available API getAnnotationsByMouseEvent to identify link annotations.

Here is the example of an onHover for annotations:

JavaScript

1WebViewer({
2 initialDoc: 'https://myserver.com/myfile.pdf'
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { documentViewer, annotationManager } = instance.Core;
6
7 documentViewer.addEventListener('mouseMove', evt => {
8 const annotations = Core.getAnnotationManager().getAnnotationsByMouseEvent(evt, true);
9 const linkAnnot = annotations.find((annot) => annot instanceof window.Core.Annotations.Link);
10 });
11 });

The annotations include links from within WebViewer, both in and outside of Content Edit mode, or from embedded URLs in the document Regardless of the link annotations obtained, you can use the deleteAnnotations method to remove them from the document. Please note that, in the current implementation, the link style will still persist.

Here's an example of removing link annotations generated by WebViewer:

JavaScript

1WebViewer({
2 initialDoc: 'https://myserver.com/myfile.pdf'
3}, document.getElementById('viewer'))
4 .then(instance => {
5 const { annotationManager } = instance.Core;
6 const linkAnnotations = annotationManager.getAnnotationsList().filter((annot) => annot instanceof window.Core.Annotations.Link);
7 // add {'source': 'unlink'} to let content edit worker know to refresh cached data
8 annotationManager.deleteAnnotations(linkAnnotations, { 'source': 'unlink' });
9 });

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales