Repository: dotemacs/pdfboxing
Pull request #8 (2015-04-25 17:06:42)

More info keys for document
Added keys that 'creation-date', 'modification-date'.

Thanks


Patch:
diff --git a/src/pdfboxing/info.clj b/src/pdfboxing/info.clj
index 4ad5df6..5d6f4a6 100644
--- a/src/pdfboxing/info.clj
+++ b/src/pdfboxing/info.clj
@@ -14,16 +14,37 @@
 (defn- getter
   "private helper function to be used by about-doc"
   [obj method-str]
-  (let [get-method (str "get" (string/capitalize method-str))]
+  (let [get-method (str "get"
+                        (string/join
+                         (map string/capitalize
+                              (string/split method-str #"-"))))]
     (Reflector/invokeInstanceMethod obj get-method (to-array []))))
 
 (defn about-doc
   "get the basic information about the given document
    the values provided are for the fields defined by
    info-fields"
-  [pdfdoc]
+  [pdfdoc & {:keys [keys]
+             :or {keys ["title" "author" "subject" "keywords"
+                        "creator" "producer" "trapped" "metadata-keys"
+                        "creation-date" "modification-date"]}}]
   (with-open [doc (common/load-pdf pdfdoc)]
     (let [info (.getDocumentInformation doc)
-          info-fields ["title" "author" "subject" "keywords" "creator" "producer"]]
+          info-fields keys]
       (into {}
             (map #(hash-map (str %1) (getter info %1)) info-fields)))))
+
+(defn metadata-value
+  "get the value of a custom metadata information field for the document."
+  [pdfdoc field-name]
+  (with-open [doc (common/load-pdf pdfdoc)]
+    (.. doc getDocumentInformation (getCustomMetadataValue field-name))))
+
+(defn metadata-values
+  "get all values of a custom metadata information field for the document."
+  [pdfdoc]
+  (with-open [doc (common/load-pdf pdfdoc)]
+    (let [info (.. doc getDocumentInformation)]
+      (into {}
+            (map #(hash-map (str %1) (.. info (getCustomMetadataValue %1)))
+                 (.. info getMetadataKeys))))))


Test patch:
diff --git a/test/pdfboxing/info_test.clj b/test/pdfboxing/info_test.clj
index 5bb542b..47009ff 100644
--- a/test/pdfboxing/info_test.clj
+++ b/test/pdfboxing/info_test.clj
@@ -6,15 +6,43 @@
   (testing "page count"
     (is (= 1 (page-number "test/pdfs/interactiveform.pdf"))))
   (testing "field count"
-    (is (= 6 (count (about-doc "test/pdfs/interactiveform.pdf")))))
+    (is (= 10 (count (about-doc "test/pdfs/interactiveform.pdf")))))
   (testing "for the key names"
-    (is (= ["title" "author" "subject" "keywords" "creator" "producer"]
-           (keys (about-doc "test/pdfs/interactiveform.pdf")))))
+    (is (= #{"title" "author" "subject" "keywords" "creator" "producer"
+             "trapped" "metadata-keys" "creation-date"
+             "modification-date"}
+           (set (keys (about-doc "test/pdfs/interactiveform.pdf"))))))
   (testing "if the full document info matches"
     (is (= {"title" "Example of an Interactive PDF Form",
             "author" nil,
             "subject" "PDF forms",
             "keywords" "forms, flat, fillable",
+            "trapped" nil,
+            "metadata-keys" #{"AAPL:Keywords" "CreationDate"
+                              "Creator" "Keywords" "ModDate" "Producer"
+                              "Subject" "Title"}
             "creator" "Adobe InDesign CS3 (5.0.4)",
             "producer" "Mac OS X 10.8.5 Quartz PDFContext"}
-           (about-doc "test/pdfs/interactiveform.pdf")))))
+           (dissoc (about-doc "test/pdfs/interactiveform.pdf")
+                   "creation-date" ;#inst "2013-12-14T13:44:39.000+00:00"
+                   "modification-date")))) ;#inst "2013-12-14T13:44:39.000+00:00"
+  (testing "only requested keys value matches"
+    (is (= {"title" "Example of an Interactive PDF Form",
+            "subject" "PDF forms"}
+           (about-doc "test/pdfs/interactiveform.pdf"
+                      :keys ["title" "subject"]))))
+  (testing "for the metadata value"
+    (is (= "Example of an Interactive PDF Form"
+           (metadata-value "test/pdfs/interactiveform.pdf" "Title")))
+    (is (= "D:20131214134439Z00'00'"
+           (metadata-value "test/pdfs/interactiveform.pdf"
+                           "CreationDate")))
+    (is (= {"AAPL:Keywords" nil,
+            "CreationDate" "D:20131214134439Z00'00'",
+            "Creator" "Adobe InDesign CS3 (5.0.4)",
+            "Keywords" "forms, flat, fillable",
+            "ModDate" "D:20131214134439Z00'00'",
+            "Producer" "Mac OS X 10.8.5 Quartz PDFContext",
+            "Subject" "PDF forms",
+            "Title" "Example of an Interactive PDF Form"}
+           (metadata-values "test/pdfs/interactiveform.pdf")))))