xlogI125’s blog

パソコン作業を効率化したい

Acrobat Reader: ページ回転角度 / しおり移動先ページ番号 をJavaScriptで表示

メモ

  • ページの回転角度を取得
    • this.getPageRotation(this.pageNum);
  • しおりのアクションを実行
    • this.bookmarkRoot.children[0].execute();
  • 注釈のプロパティ名を表示
    • for (propName in this.selectedAnnots[0].getProps()) console.println(propName);

使い捨てスクリプト

  • 内容
    • 選択された注釈のプロパティをコンソールに表示
    • 全ページの回転とページサイズをコンソールに表示
    • しおりの移動先ページ番号をコンソールに表示
  • テスト環境
  • 保存先フォルダ
    • %APPDATA%\Adobe\Acrobat\Privileged\DC\JavaScripts
  • 保存先ファイル名
    • *.js
  • エンコード
    • ANSI
console.show();
console.println('\
// コンソールでの実行方法はテキストを範囲選択して Ctrl + Enter \r\
\r\
// folder level script 保存先フォルダ\r\
// app.getPath("user", "javascript");\
');

// メニュー → ヘルプ にサブメニュー「テスト」を追加

// 区切り
app.addMenuItem({cName: "-", cParent: "Help", cExec: "", bPrepend: false});

// サブメニュー
app.addSubMenu({cName: "testSubMenu", cUser: "テスト", cParent: "Help"});

// メニュー → ヘルプ → テスト に追加

// コンソールを表示
app.addMenuItem({
  cName: "test0", cUser: "コンソールを表示(&J)", cParent: "testSubMenu", 
  cExec: "(() => { console.clear(); console.show(); })();", 
  cEnable: "event.rc = true;", bPrepend: false
});

// 区切り
app.addMenuItem({cName: "-", cParent: "testSubMenu", cExec: "", bPrepend: false});

// 選択された注釈のプロパティをコンソールに表示
(function() {
  const f = (() => {
    const doc = this;
    const annots = doc.selectedAnnots;
    console.clear();
    console.show();
    if (annots === undefined) {
      console.println("// 注釈が選択されていません");
      return;
    }
    annots.forEach((annot, i) => {
      var propname, str = "";
      str += util.printf("%d/%d ... %s\r", i + 1, annots.length, annot["type"]);
      str += util.printf("----------------------------------------\r");
      for (propname in annot.getProps()) {
        str += util.printf("%s: %s\r", propname, annot[propname]);
      }
      str += util.printf("----------------------------------------\r");
      str += util.printf('// this.getAnnot(%d, "%s").setProps({author: "", subject: ""});\r\r', annot.page, annot.name);
      console.println(str);
    });
  });

  const str = util.printf("(%s)();", f.toString());

  app.addMenuItem({
    cName: "test1", 
    cUser: "選択された注釈のプロパティをコンソールに表示(&K)", 
    cParent: "testSubMenu", 
    cExec: str, cEnable: "event.rc = (event.target != null);", bPrepend: false
  });
})();

// 区切り
app.addMenuItem({cName: "-", cParent: "testSubMenu", cExec: "", bPrepend: false});

// 全ページの回転とページサイズをコンソールに表示
(function() {
  const f = (() => {
    const doc = this, arrStr = new Array();
    var rot, x0mm, y0mm, x1mm, y1mm, str = "";
    arrStr.push(util.printf("page | rot | wC[mm], hC[mm] | wM[mm], hM[mm] | wA[mm], hA[mm] | wT[mm], hT[mm] | wB[mm], hB[mm]\r"));
    for (var pageNum = 0; pageNum < doc.numPages; pageNum++) {
      rot = doc.getPageRotation({nPage: pageNum});
      str += util.printf("%4d | %3d | ", pageNum + 1, rot);
      [x0mm, y1mm, x1mm, y0mm] = doc.getPageBox({cBox: "Crop", nPage: pageNum}).map(x => x * 25.4 / 72);
      str += util.printf("%3.2f, %3.2f | ", x1mm - x0mm, y1mm - y0mm);

      // エラー「コンソールへの出力を続行できません。」が表示される場合は表示項目を減らしてください
      [x0mm, y1mm, x1mm, y0mm] = doc.getPageBox({cBox: "Media", nPage: pageNum}).map(x => x * 25.4 / 72);
      str += util.printf("%3.2f, %3.2f | ", x1mm - x0mm, y1mm - y0mm);
      [x0mm, y1mm, x1mm, y0mm] = doc.getPageBox({cBox: "Art", nPage: pageNum}).map(x => x * 25.4 / 72);
      str += util.printf("%3.2f, %3.2f | ", x1mm - x0mm, y1mm - y0mm);
      [x0mm, y1mm, x1mm, y0mm] = doc.getPageBox({cBox: "Trim", nPage: pageNum}).map(x => x * 25.4 / 72);
      str += util.printf("%3.2f, %3.2f | ", x1mm - x0mm, y1mm - y0mm);
      [x0mm, y1mm, x1mm, y0mm] = doc.getPageBox({cBox: "Bleed", nPage: pageNum}).map(x => x * 25.4 / 72);
      str += util.printf("%3.2f, %3.2f\r", x1mm - x0mm, y1mm - y0mm);

      if ((pageNum + 1) % 250 == 0 || (pageNum + 1) == doc.numPages) {
        arrStr.push(str);
        str = "";
      }
    }
    console.clear();
    console.show();
    arrStr.forEach(s => console.println(s));
  });

  const str = util.printf("(%s)();", f.toString());

  app.addMenuItem({
    cName: "test2", 
    cUser: "全ページの回転とページサイズをコンソールに表示(&L)", 
    cParent: "testSubMenu", 
    cExec: str, cEnable: "event.rc = (event.target != null);", bPrepend: false
  });
})();

// 区切り
app.addMenuItem({cName: "-", cParent: "testSubMenu", cExec: "", bPrepend: false});

// しおりの移動先ページ番号をコンソールに表示
(function() {
  const f = (() => {
    const doc = this;
    var str = "lv | page | name\r";
    const func = ((lv, bkm) => {
      var pageNumBefore;
      if (lv == 0 && bkm.children == null) {
        str = "// しおりが存在しません";
        return;
      }
      if (lv != 0) {
        pageNumBefore = doc.pageNum;
        doc.pageNum = 0;
        bkm.execute();  // 他のPDFファイルへの移動は発生しないものとします
        str += util.printf("%2d | %4d | %s\r", lv, doc.pageNum + 1, "|".repeat(lv - 1) + bkm.name);
        doc.pageNum = pageNumBefore;
      }
      if (bkm.children != null) {
        for (var i = 0; i < bkm.children.length; i++) {
          func(lv + 1, bkm.children[i]);
        }
      }
    });
    func(0, doc.bookmarkRoot);
    console.clear();
    console.show();
    console.println(str);
  });

  const str = util.printf("(%s)();", f.toString());

  app.addMenuItem({
    cName: "test3", 
    cUser: "しおりの移動先ページ番号をコンソールに表示(&M)", 
    cParent: "testSubMenu", 
    cExec: str, cEnable: "event.rc = (event.target != null);", bPrepend: false
  });
})();