pdf-extract-rich-media.js 1.8 KB

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