How to highlight synctex output values on ng2-pdf-viewer

1.2k Views Asked by At

I have an angular 6 application for tex compilation and pdf output. Ace editor is used for creating tex files. It can be compiled at the realtime using pdflatex. Output pdf is shown using ng2-pdf-viewer . Inorder to implement synctex (sync between editor content and pdf) i have used "synctex view" command for forward search and it returns some output values such as h,v,x,y,width,height. I need to highlight these points in ng2-pdf-viewer. I found a canvas tag inside pdf viewer and i created and appended a new canvas with the same height and width and was able to draw shapes over the pdf. But iam unable to plot it exactly where the points are. Does anyone have any idea about this, how this synctex output point works and how can we highlight pdf using the canvas draw function.

1

There are 1 best solutions below

1
On BEST ANSWER

After hours of research i found a solution for synctex highlight in ng2-pdf-viewer. Rather than drawing the synctex output values over canvas, we can create a div element and adjust its top,left,width,height according to those values. Before that we need to recalculate the values relative to our pdf-viewer.

Result of "synctex view" command returns v,h,width,height,page number values. Then parse as follows.

var pageno     = parseInt(datas['pageNum']);
//i'm using ng2-pdf-viewer, ViewChild('pdfviewer');
var pageView   = this.pdfviewer.pdfViewer._pages[pageno - 1];
//datas - array returning from server contains synctex output values
var left       = parseInt(datas['h']);
var top        = parseInt(datas['v']);
var width      = parseInt(datas['width']);
var height     = parseInt(datas['height']);
//recalculating top value
    top        = pageView.viewport.viewBox[3] - top;
var valueArray = [left,top,left + width,top + height];
let rect       = pageView.viewport.convertToViewportRectangle(valueArray);
    rect       = pdfjs.Util.normalizeRect(rect);
var x          = Math.min(rect[0], rect[2]), width = Math.abs(rect[0] - rect[2]);
var y          = Math.min(rect[1], rect[3]), height = Math.abs(rect[1] - rect[3])
const element  = document.createElement('div');
      element.setAttribute("class", "overlay-div-synctex");
      element.style.left     = x + 'px';
      element.style.top      = y + 'px';
      element.style.width    = width + 'px';
      element.style.height   = height + 'px';
      element.style.position = 'absolute';
      element.style.backgroundColor = 'rgba(255,255,0,0.5)';
      //jquery is used here, replace it with javascript
      $('*[data-page-number="'+pageno+'"]').append(element);
      this.pdfviewer.pdfViewer._scrollIntoView({
          pageDiv: pageView.div
      });

If your synctex command returns multiple h,v.. values then loop over and append the highlight element.

Hope it helps.