Skip to content

Instantly share code, notes, and snippets.

@mattdeboard mattdeboard/render.clj Secret
Created Nov 26, 2012

Embed
What would you like to do?
(ns clj-itext.render
(:import [com.itextpdf.text BaseColor]
[com.itextpdf.text.pdf BaseFont PdfContentByte PdfStamper])
(:require [clj-itext.core :as itext]))
(def my-pdf (itext/pdf<- "/home/matt/courseload/stillwaiting.pdf"))
(def watermark "Copyright protected material!")
(def bgcolor (new BaseColor 255 255 255))
(def fontcolor (new BaseColor 0 0 0))
(def font (. BaseFont createFont
(. BaseFont HELVETICA)
(. BaseFont WINANSI)
(. BaseFont EMBEDDED)))
(def fontSize (float 9))
(def textAngle (float 0))
(def fontPadding (float 4))
(def rectangleHeight (+ fontSize fontPadding))
(def fontWidth (. font getWidthPoint watermark fontSize))
(def cropBox (. my-pdf getCropBox 5))
(def mediaBox
(let [pageSize (. my-pdf getPageSize 5)]
(if (and cropBox (or (< (. cropBox getWidth) (. mediaBox getWidth))
(< (. cropBox getHeight) (. cropBox getHeight))))
cropBox
pageSize)))
(defn fingerprint
[^PdfStamper s ^Integer i]
(let [oc (itext/over-content s i)]
;; Draw our bounding box in which our fingerprint will be stamped
(. oc setColorFill bgcolor)
(. oc moveTo
(+ (. mediaBox getLeft) rectangleHeight)
(. mediaBox getTop))
(. oc lineTo
(. mediaBox getLeft)
(. mediaBox getTop))
(. oc lineTo
(. mediaBox getLeft)
(- (. mediaBox getTop) rectangleHeight))
(. oc lineTo
(+ (. mediaBox getLeft) rectangleHeight)
(. mediaBox getTop))
(. oc fill)
;; Drop our watermark on top of the rectangle we just created
(. oc beginText)
(. oc setColorFill fontcolor)
(. oc setFontAndSize font fontSize)
(. oc showTextAligned
(. PdfContentByte ALIGN_LEFT)
watermark
(+ (. mediaBox getLeft) (/ fontPadding 2))
(- (. mediaBox getTop) fontSize)
textAngle)
(. oc endText)
(. s close)))
;; (defn stamp-pdf [output]
;; (let [pages (range 1 (+ (:pagecount (meta my-pdf)) 1))]
;; (for [i pages]
;; (with-open [os (clojure.java.io/output-stream output)]
;; (let [my-pdf (itext/pdf<- "/home/matt/courseload/stillwaiting.pdf")
;; stamper (itext/stamper my-pdf os)]
;; (fingerprint stamper i))))))
(defn stamp-pdf [output]
(let [pages (range 1 (+ (:pagecount (meta my-pdf)) 1))]
(doseq [i pages]
(with-open [os (clojure.java.io/output-stream output)]
(let [my-pdf (itext/pdf<- "/home/matt/courseload/stillwaiting.pdf")
stamper (itext/stamper my-pdf os)]
(fingerprint stamper i))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.