0

I want to write a function that jumps to a given string in a pdf document. I am using pdf-tools. Naively, I thought this would work:

(defun goto-string-in-pdf (pdffile)
  "Jump to string 999 in pdffile"
  (let ((win (get-buffer-window pdffile)))
    (with-selected-window win 
      (pdf-view-goto-page 1)
      (search-forward "999"))))

But it seems search-forward doesn't work in PDFView-mode. So how do I make this work?

2

Apart from choosing the right buffer, this function suggests the necessary steps.

(defun pdf-search-forward (string)
  (interactive "sString: ")
  ;; Expect a pdf-window, else the highlighting won't work.
  (pdf-util-assert-pdf-window)
  ;; Get a list of matches, discarding all but the first.
  (let-alist (car (pdf-info-search-string
                   "Number"
                   (cons (pdf-view-current-page) 0)))
    (unless .page
      (error "Not found: %s" string))
    (pdf-view-goto-page .page)
    ;; Scale the edges of the match according to the displayed
    ;; page-size.
    (let ((pixel-edges
           (pdf-util-scale-relative-to-pixel .edges))
          (pdf-isearch-batch-mode t))
      ;; Highlight and focus the match.
      (pdf-isearch-hl-matches pixel-edges nil t)
      (pdf-isearch-focus-match-batch pixel-edges))))
  • Thanks, that was very helpful. From your code I derived that (pdf-view-goto-page (alist-get 'page (car (pdf-info-search-string myString)))) solves my problem, that is, jumps to the page in the pdf with the first occurence of myString. – StarBug Oct 4 '19 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.