Function: pdf-view-extract-region-image

pdf-view-extract-region-image is an interactive and natively compiled function defined in pdf-view.el.

Signature

(pdf-view-extract-region-image REGIONS &optional PAGE SIZE OUTPUT-BUFFER NO-DISPLAY-P)

Documentation

Create a PNG image of REGIONS.

REGIONS should have the same form as pdf-view-active-region(var)/pdf-view-active-region(fun), which see. PAGE and SIZE are the page resp. base-size of the image from which the image-regions will be created; they default to pdf-view-current-page resp. pdf-view-image-size.

Put the image in OUTPUT-BUFFER, defaulting to "*PDF region image*" and display it, unless NO-DISPLAY-P is non-nil.

In case of multiple regions, the resulting image is constructed by joining them horizontally. For this operation (and this only) the convert program is used.

Key Bindings

pdf-view-mode-map C-c TAB

Source Code

;; Defined in /nix/store/63h5z2vmzn0mfavcxi2idi23g5xcgkbl-emacs-packages-deps/share/emacs/site-lisp/elpa/pdf-tools-20260102.1101/pdf-view.el
(defun pdf-view-extract-region-image (regions &optional page size
                                              output-buffer no-display-p)
  ;; TODO: what is "resp."? Avoid contractions.
  "Create a PNG image of REGIONS.

REGIONS should have the same form as `pdf-view-active-region',
which see.  PAGE and SIZE are the page resp. base-size of the
image from which the image-regions will be created; they default
to `pdf-view-current-page' resp. `pdf-view-image-size'.

Put the image in OUTPUT-BUFFER, defaulting to \"*PDF region
image*\" and display it, unless NO-DISPLAY-P is non-nil.

In case of multiple regions, the resulting image is constructed
by joining them horizontally.  For this operation (and this only)
the `convert' program is used."

  (interactive
   (list (if (pdf-view-active-region-p)
             (pdf-view-active-region t)
           '(,(pdf-view-current-page) (0 0 1 1)))))
  (unless page
    (setq page (car regions)))
  (unless size
    (setq size (pdf-view-image-size nil nil page)))
  (unless output-buffer
    (setq output-buffer (get-buffer-create "*PDF image*")))
  (let* ((images (mapcar (lambda (edges)
                           (let ((file (make-temp-file "pdf-view"))
                                 (coding-system-for-write 'binary))
                             (write-region
                              (pdf-info-renderpage
                               page (car size)
                               :crop-to edges)
                              nil file nil 'no-message)
                             file))
                         (cdr regions)))
         result)
    (unwind-protect
        (progn
          (if (= (length images) 1)
              (setq result (car images))
            (setq result (make-temp-file "pdf-view"))
            ;; Join the images horizontally with a gap of 10 pixel.
            (pdf-util-convert
             "-noop" ;; workaround limitations of this function
             result
             :commands `("("
                         ,@images
                         "-background" "white"
                         "-splice" "0x10+0+0"
                         ")"
                         "-gravity" "Center"
                         "-append"
                         "+gravity"
                         "-chop" "0x10+0+0")
             :apply '((0 0 0 0))))
          (with-current-buffer output-buffer
            (let ((inhibit-read-only t))
              (erase-buffer))
            (set-buffer-multibyte nil)
            (insert-file-contents-literally result)
            (image-mode)
            (unless no-display-p
              (pop-to-buffer (current-buffer)))))
      (dolist (f (cons result images))
        (when (file-exists-p f)
          (delete-file f))))))