Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/docs/examples/pdf-extract-rich-media.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 rich media in a PDF document. | |
| 2 | |
| 3 if (scriptArgs.length != 1 && scriptArgs.length != 3) { | |
| 4 print("usage: mutool run pdf-extract-rich-media.js input.pdf [index filename]"); | |
| 5 print(" List embedded rich media, or extract an embedded rich media file from a PDF document.") | |
| 6 quit(); | |
| 7 } | |
| 8 | |
| 9 var doc = Document.openDocument(scriptArgs[0]); | |
| 10 | |
| 11 function mapNameTree(N, fn) { | |
| 12 function mapNameTreeNames(NN) { | |
| 13 var i, n = NN.length; | |
| 14 for (i = 0; i < n; i += 2) | |
| 15 fn(NN[i], NN[i+1]); | |
| 16 } | |
| 17 function mapNameTreeKids(NK) { | |
| 18 var i, n = NK.length; | |
| 19 for (i = 0; i < n; ++i) | |
| 20 mapNameTree(NK[i], fn) | |
| 21 } | |
| 22 if ("Names" in N) | |
| 23 mapNameTreeNames(N.Names); | |
| 24 if ("Kids" in N) | |
| 25 mapNameTreeKids(N.Kids); | |
| 26 } | |
| 27 | |
| 28 function fileNameFromFS(fs) { | |
| 29 if ("UF" in fs) return fs.UF.asString(); | |
| 30 if ("F" in fs) return fs.F.asString(); | |
| 31 if ("Unix" in fs) return fs.Unix.asString(); | |
| 32 if ("DOS" in fs) return fs.DOS.asString(); | |
| 33 if ("Mac" in fs) return fs.Mac.asString(); | |
| 34 return "Untitled"; | |
| 35 } | |
| 36 | |
| 37 function mapRichMediaAssets(fn) { | |
| 38 var pageCount = doc.countPages(); | |
| 39 var page, annots, a; | |
| 40 for (page = 0; page < pageCount; ++page) { | |
| 41 annots = doc.findPage(page).Annots; | |
| 42 if (annots && annots.length > 0) { | |
| 43 for (a = 0; a < annots.length; ++a) { | |
| 44 if (annots[a].Subtype == "RichMedia") | |
| 45 mapNameTree(annots[a].RichMediaContent.Assets, fn); | |
| 46 } | |
| 47 } | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 if (scriptArgs.length == 1) { | |
| 52 var idx = 1; | |
| 53 mapRichMediaAssets(function (name, fs) { | |
| 54 print(idx, name.asString()); | |
| 55 print("\tFilename:", fileNameFromFS(fs)); | |
| 56 if ("Desc" in fs) | |
| 57 print("\tDescription:", fs.Desc.asString()); | |
| 58 ++idx; | |
| 59 }); | |
| 60 } | |
| 61 | |
| 62 if (scriptArgs.length == 3) { | |
| 63 var idx = 1; | |
| 64 mapRichMediaAssets(function (name, fs) { | |
| 65 if (idx == scriptArgs[1]) { | |
| 66 print("Saving embedded file", idx, "as:", scriptArgs[2]); | |
| 67 fs.EF.F.readStream().save(scriptArgs[2]); | |
| 68 } | |
| 69 ++idx; | |
| 70 }); | |
| 71 } |
