Can't Highlight text programmatically

WebViewer Version: 8.2.0

Can’t highlight text programmatically, this is the code snippet:

        instance.Core.documentViewer.addEventListener('documentLoaded', () => {

            instance.Core.Tools.Tool.enableTextSelection();

            const tool = new instance.Core.Tools.TextStrikeoutCreateTool(instance.Core.documentViewer);
            const annotation = new instance.Core.Annotations.TextStrikeoutAnnotation();
            annotation.PageNumber = 1;
            tool.annotation = annotation;

            const topLeft = { x: 400, y: 400, pageIndex: 0};
            const bottomRight = { x: 800, y: 800, pageIndex: 0};
            tool.pageCoordinates[0] = topLeft;
         
            tool.select(topLeft, bottomRight);

            instance.Core.annotationManager.addAnnotation(annotation);
            instance.Core.annotationManager.redrawAnnotation(annotation);
    });

Annotation is created and located upper left without any text highlighted.

Am I doing something wrong? Thanks!

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hello,

Thanks for providing your code!

The issue is really just the pageIndex property. The select function expects pageNumber and is a 1-based index. PDFTron WebViewer Class: DocumentViewer

instance.Core.documentViewer.addEventListener('documentLoaded', () => {
      const tool = new instance.Core.Tools.TextStrikeoutCreateTool(instance.Core.documentViewer);
      const annotation = new instance.Core.Annotations.TextStrikeoutAnnotation();
      annotation.PageNumber = 1;
      tool.annotation = annotation;

      const topLeft = { x: 400, y: 400, pageNumber: 1 };
      const bottomRight = { x: 800, y: 800, pageNumber: 1 };
      tool.pageCoordinates[0] = topLeft;
  
      tool.select(topLeft, bottomRight);

      instance.Core.annotationManager.addAnnotation(annotation);
      instance.Core.annotationManager.redrawAnnotation(annotation);
    });
1 Like

Hi Andy, it works, thank you very much.