Advertisement
Guest User

Untitled

a guest
Jun 6th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns pdf-test.core
  2.   (:require [pdfboxing.form :as form]
  3.             [pdfboxing.common :as common])
  4.   (:import [org.apache.pdfbox.pdmodel PDDocumentCatalog PDPage PDPageContentStream]))
  5.  
  6. (declare draw-line)
  7.  
  8. (draw-line :input-pdf "edited.pdf" :output-pdf "neco2.pdf"
  9.            :coordinates (merge {:page-number 2}
  10.                                {:x 220 :y 395 :x1 560 :y1 395}))
  11.  
  12. (defn- get-form-fields
  13.   []
  14.   (let [fields (remove #(clojure.string/includes? % "souhlas") (keys (form/get-fields "cs_fo.pdf")))]
  15.     (form/set-fields "cs_fo.pdf" "edited.pdf" (zipmap fields fields))))
  16.  
  17. (defn- rename-fields
  18.   []
  19.   (let [fields (keys (form/get-fields "ek_ico.pdf"))]
  20.     (nth (clojure.data/diff fields (remove #(clojure.string/includes? % "souhlas" fields))) 1)))
  21.     ; (form/rename-fields "plna_moc.pdf" "renamed.pdf" (zipmap (sort-by #(Integer/parseInt (subs % 12 (count %))) fields) new-names))))
  22.     ; (form/set-fields "ek_form.pdf" "renamed.pdf" (zipmap new-names fields))))
  23.  
  24. (defn get-catalog
  25.   "Get a catalog from a PDF document"
  26.   [document]
  27.   (.getDocumentCatalog document))
  28.  
  29. (defn get-page
  30.   "Get a particular page from a catalog"
  31.   [catalog page-number]
  32.   (.get (.getPages catalog) page-number))
  33.  
  34. (defn get-content-stream
  35.   "Take the read in PDF document and a page number and return content
  36.  stream which will be manipulated"
  37.   [document page-number]
  38.   (let [catalog (get-catalog document)
  39.         page (get-page catalog page-number)
  40.         content-stream (PDPageContentStream. document page true true)]
  41.     content-stream))
  42.  
  43. (defn use-rgb-colour
  44.   "Take content-stream and use RGB colour to draw on it.
  45.   By default black colour will be used."
  46.   [content-stream & {:keys [r g b]
  47.                      :or {r 0
  48.                           g 0
  49.                           b 0}}]
  50.   (.setStrokingColor content-stream r g b))
  51.  
  52. (defn set-line-width
  53.   "The width of the line to be drawn.
  54.   By default it's 3."
  55.   [content-stream & {:keys [line-width]
  56.                      :or {line-width 3}}]
  57.   (.setLineWidth content-stream line-width))
  58.  
  59. (defn draw-line-at-coordinates
  60.   "Take content-stream and coordinates for line to be drawn.
  61.   Coordinates are a hash map in the format of: x, y, x1, y1."
  62.   [content-stream coordinates]
  63.   (.drawLine content-stream
  64.              (:x coordinates)
  65.              (:y coordinates)
  66.              (:x1 coordinates)
  67.              (:y1 coordinates)))
  68.  
  69. (defn draw-line
  70.   "Draw a line on input-pdf's page, producing a output-pdf document.
  71.   It takes three arguments input-pdf, output-pdf and
  72.   coordinates. Where coordinates have: page-number, x, y, x1, y1
  73.   points where the line should be drawn."
  74.   [& {:keys [input-pdf output-pdf coordinates]}]
  75.   (with-open [document (common/obtain-document input-pdf)]
  76.     (with-open [content-stream (get-content-stream document (:page-number coordinates))]
  77.       (use-rgb-colour content-stream)
  78.       (set-line-width content-stream)
  79.       (draw-line-at-coordinates content-stream coordinates))
  80.     (.save document output-pdf)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement