Sometimes I want extract some pages from a pdf file, for example get out particular chapter from a book to focus on this chapter. Most PDF read software don't allow save part of the PDF as a new PDF. It should be called export PDF or save as, but allow the user to specify the page number range.

But it actually a easy job with library like iText PDF library. It will be nice we can do it in a REPL like Clojure REPL, so we will do it in Clojure.

First step is get iText Library Jar files, its open source and can download free.

Here is the function in Clojure.

(import '(com.itextpdf.text Document)
            '(com.itextpdf.text.pdf PdfReader PdfWriter PdfContentByte PdfImportedPage BaseFont PdfCopy)
            '( File FileInputStream FileOutputStream InputStream OutputStream))
(defn copy-pdf-range [src dest page-start page-end]
  (let [doc (Document.)
        os (FileOutputStream. dest)
        copy (PdfCopy. doc os)
        reader (PdfReader. src)
    (doto doc (.open))
    (loop [page page-start]
      (if (> page page-end)
          (.addPage copy (.getImportedPage copy reader page))
          (recur (inc page))
    (.close doc)
    (.close reader)
    (.close os)

How to use it, call the function with source pdf file, destination pdf file and start and end page number

    "output.pdf" 246 260)

In this example the core part is PdfCopy class provided by iText library, this function is actully translated from Java source code in the tutorial provided in iText PdfCopy documentation. But is obviously much easier to use in Clojure than Java.