Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/docs/examples/pdf-portfolio.js @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 // List and extract embedded files in a PDF document. | |
| 2 | |
| 3 if (scriptArgs.length != 1 && scriptArgs.length != 3) { | |
| 4 print("usage: mutool run pdf-portfolio.js input.pdf [index filename]"); | |
| 5 print(" List embedded files, or extract an embedded file from a PDF document.") | |
| 6 quit(); | |
| 7 } | |
| 8 | |
| 9 var doc = Document.openDocument(scriptArgs[0]); | |
| 10 var Root = doc.getTrailer().Root; | |
| 11 if (Root.Names == undefined || !("EmbeddedFiles" in Root.Names)) { | |
| 12 print("Document has no embedded files!"); | |
| 13 quit(); | |
| 14 } | |
| 15 | |
| 16 function mapNameTree(N, fn) { | |
| 17 function mapNameTreeNames(NN) { | |
| 18 var i, n = NN.length; | |
| 19 for (i = 0; i < n; i += 2) | |
| 20 fn(NN[i], NN[i+1]); | |
| 21 } | |
| 22 function mapNameTreeKids(NK) { | |
| 23 var i, n = NK.length; | |
| 24 for (i = 0; i < n; ++i) | |
| 25 mapNameTree(NK[i], fn) | |
| 26 } | |
| 27 if ("Names" in N) | |
| 28 mapNameTreeNames(N.Names); | |
| 29 if ("Kids" in N) | |
| 30 mapNameTreeKids(N.Kids); | |
| 31 } | |
| 32 | |
| 33 function fileNameFromFS(fs) { | |
| 34 if ("UF" in fs) return fs.UF.asString(); | |
| 35 if ("F" in fs) return fs.F.asString(); | |
| 36 if ("Unix" in fs) return fs.Unix.asString(); | |
| 37 if ("DOS" in fs) return fs.DOS.asString(); | |
| 38 if ("Mac" in fs) return fs.Mac.asString(); | |
| 39 return "Untitled"; | |
| 40 } | |
| 41 | |
| 42 if (scriptArgs.length == 1) { | |
| 43 var idx = 1; | |
| 44 mapNameTree(Root.Names.EmbeddedFiles, function (name,fs) { | |
| 45 print(idx, name.asString()); | |
| 46 print("\tFilename:", fileNameFromFS(fs)); | |
| 47 if ("Desc" in fs) | |
| 48 print("\tDescription:", fs.Desc.asString()); | |
| 49 ++idx; | |
| 50 }); | |
| 51 } | |
| 52 | |
| 53 if (scriptArgs.length == 3) { | |
| 54 var idx = 1; | |
| 55 mapNameTree(Root.Names.EmbeddedFiles, function (name,fs) { | |
| 56 if (idx == scriptArgs[1]) { | |
| 57 print("Saving embedded file", idx, "as:", scriptArgs[2]); | |
| 58 fs.EF.F.readStream().save(scriptArgs[2]); | |
| 59 } | |
| 60 ++idx; | |
| 61 }); | |
| 62 } |
