summaryrefslogtreecommitdiff
path: root/docs/examples
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2018-11-12 16:52:09 +0100
committerTor Andersson <tor.andersson@artifex.com>2018-11-12 17:41:05 +0100
commiteb363682478a0c4b0539222d1e27a3dd252d8b62 (patch)
tree48af2b8749cfa59e81922013b82c48536243257a /docs/examples
parent3114c620177e4a8dc2f15d7f9d1f73deddaa6f07 (diff)
downloadmupdf-eb363682478a0c4b0539222d1e27a3dd252d8b62.tar.xz
Add example script to list and extract embedded files from PDF documents.
Diffstat (limited to 'docs/examples')
-rw-r--r--docs/examples/pdf-portfolio.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/docs/examples/pdf-portfolio.js b/docs/examples/pdf-portfolio.js
new file mode 100644
index 00000000..962f4bc2
--- /dev/null
+++ b/docs/examples/pdf-portfolio.js
@@ -0,0 +1,62 @@
+// List and extract embedded files in a PDF document.
+
+if (scriptArgs.length != 1 && scriptArgs.length != 3) {
+ print("usage: mutool run pdf-portfolio.js input.pdf [index filename]");
+ print(" List embedded files, or extract an embedded file from a PDF document.")
+ quit();
+}
+
+var doc = new PDFDocument(scriptArgs[0]);
+var Root = doc.getTrailer().Root
+if (!("EmbeddedFiles" in Root.Names)) {
+ print("Document has no embedded files!");
+ quit();
+}
+
+function mapNameTree(N, fn) {
+ function mapNameTreeNames(NN) {
+ var i, n = NN.length;
+ for (i = 0; i < n; i += 2)
+ fn(NN[i], NN[i+1]);
+ }
+ function mapNameTreeKids(NK) {
+ var i, n = NK.length;
+ for (i = 0; i < n; ++i)
+ mapNameTree(NK[i], fn)
+ }
+ if ("Names" in N)
+ mapNameTreeNames(N.Names);
+ if ("Kids" in N)
+ mapNameTreeKids(N.Kids);
+}
+
+function fileNameFromFS(fs) {
+ if ("UF" in fs) return fs.UF.asString();
+ if ("F" in fs) return fs.F.asString();
+ if ("Unix" in fs) return fs.Unix.asString();
+ if ("DOS" in fs) return fs.DOS.asString();
+ if ("Mac" in fs) return fs.Mac.asString();
+ return "Untitled";
+}
+
+if (scriptArgs.length == 1) {
+ var idx = 1;
+ mapNameTree(Root.Names.EmbeddedFiles, function (name,fs) {
+ print(idx, name.asString());
+ print("\tFilename:", fileNameFromFS(fs));
+ if ("Desc" in fs)
+ print("\tDescription:", fs.Desc.asString());
+ ++idx;
+ });
+}
+
+if (scriptArgs.length == 3) {
+ var idx = 1;
+ mapNameTree(Root.Names.EmbeddedFiles, function (name,fs) {
+ if (idx == scriptArgs[1]) {
+ print("Saving embedded file", idx, "as:", scriptArgs[2]);
+ fs.EF.F.readStream().save(scriptArgs[2]);
+ }
+ ++idx;
+ });
+}