xlogI125’s blog

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

Acrobat JavaScript練習 用紙サイズ変更

メモ

  • 用紙サイズを変更する方法の例
    • setPageBoxes
    • newDoc + newPage + addWatermarkFromFile
  • 加工後のPDFに不都合な点は無いか確認するのが手間
  • 現実的にはPDFを印刷し、スキャンした画像をPDFに変換する方法が無難な気がします

使い捨てスクリプト

  • 入力チェック、用紙の向き、回転は考慮しない
  • 新規作成されたPDFで細線が発生する場合があります
// Acrobat Standard DC (2022年8月頃), Windows 11
// JavaScript デバッガー の コンソール から実行
// 実行方法はテキストを範囲選択して Ctrl + Enter

// 加工後のPDFに不都合な点は無いか十分に確認してください

var strCropBox = "" + 
(() => {
  // 入力チェック、用紙の向き、回転は考慮しない

  const paperWidth = Number(app.response({
    cQuestion: "変更後における\r用紙の幅[mm]を半角数字で入力", 
    cTitle: "paperWidth", 
    cDefault: "420", bPassword: false, 
    cLabel: "幅[mm]"
  }));

  const paperHeight = Number(app.response({
    cQuestion: "変更後における\r用紙の高さ[mm]を半角数字で入力", 
    cTitle: "paperHeight", 
    cDefault: "297", bPassword: false, 
    cLabel: "高さ[mm]"
  }));

  const doc = this;

  var pageNum;
  var xll, yll, xur, yur;
  var marginLeft, marginBottom;
  var xllNew, yllNew, xurNew, yurNew;

  for (pageNum = 0; pageNum < doc.numPages; pageNum++) {
    [xll, yur ,xur ,yll] = doc.getPageBox({
      cBox: "Crop", nPage: pageNum
    }).map(x => 25.4 / 72 * x);

    marginLeft = (paperWidth - (xur - xll)) / 2;
    marginBottom = (paperHeight - (yur - yll)) / 2;

    xllNew = xll - marginLeft;
    yllNew = yll - marginBottom;
    xurNew = xllNew + paperWidth;
    yurNew = yllNew + paperHeight;

    doc.setPageBoxes({
      cBox: "Crop", nStart: pageNum, nEnd: pageNum, 
      rBox: [xllNew, yurNew, xurNew, yllNew].map(
        x => 72 / 25.4 * x
      )
    });

    doc.setPageBoxes({
      cBox: "Media", nStart: pageNum, nEnd: pageNum, 
      rBox: [0, yurNew - yllNew, xurNew - xllNew, 0].map(
        x => 72 / 25.4 * x
      )
    });

    ["Art", "Trim", "Bleed"].forEach(box => 
      doc.setPageBoxes({
        cBox: box, nStart: pageNum, nEnd: pageNum
      })
    );
  }
}).toString();

var strWatermark = "" + 
(() => {
  // 新規作成されたPDFで細線が発生する場合があります

  // 入力チェック、用紙の向き、回転は考慮しない

  const paperWidth = Number(app.response({
    cQuestion: "新規PDFにおける\r用紙の幅[mm]を半角数字で入力", 
    cTitle: "paperWidth", 
    cDefault: "297", bPassword: false, 
    cLabel: "幅[mm]"
  }));

  const paperHeight = Number(app.response({
    cQuestion: "新規PDFにおける\r用紙の高さ[mm]を半角数字で入力", 
    cTitle: "paperHeight", 
    cDefault: "210", bPassword: false, 
    cLabel: "高さ[mm]"
  }));

  const scale = Number(app.response({
    cQuestion: "scale = -1 または 0 < scale", 
    cTitle: "scale", 
    cDefault: "0.67", bPassword: false, 
    cLabel: "scale"
  }));

  var pageNum;
  // var xll, yll, xur, yur;
  var xllNew, yllNew, xurNew, yurNew;

  const docThis = this;

  const docNew = app.newDoc();

  for (pageNum = 1; pageNum < docThis.numPages; pageNum++) {
    docNew.newPage();
  }

  for (pageNum = 0; pageNum < docThis.numPages; pageNum++) {

    // [xll, yur ,xur ,yll] = docThis.getPageBox({
    //   cBox: "Crop", nPage: pageNum
    // }).map(x => 25.4 / 72 * x);

    xllNew = 0;
    yllNew = 0;
    xurNew = paperWidth;
    yurNew = paperHeight;

    docNew.setPageBoxes({
      cBox: "Crop", nStart: pageNum, nEnd: pageNum, 
      rBox: [xllNew, yurNew, xurNew, yllNew].map(
        x => 72 / 25.4 * x
      )
    });

    docNew.setPageBoxes({
      cBox: "Media", nStart: pageNum, nEnd: pageNum, 
      rBox: [0, yurNew - yllNew, xurNew - xllNew, 0].map(
        x => 72 / 25.4 * x
      )
    });

    ["Art", "Trim", "Bleed"].forEach(box => 
      docNew.setPageBoxes({
        cBox: box, nStart: pageNum, nEnd: pageNum
      })
    );

    docNew.addWatermarkFromFile({
      cDIPath: docThis.path, 
      nSourcePage: pageNum, 
      nStart: pageNum, nEnd: pageNum, 
      bOnTop: true, bOnScreen: true, bOnPrint: true, 
      nHorizAlign: app.constants.align.center, 
      nVertAlign: app.constants.align.center, 
      nHorizValue: 72 / 25.4 * 0, 
      nVertValue: 72 / 25.4 * 0, 
      bPercentage: false, 
      nScale: scale, 
      bFixedPrint: false, nRotation: 0, nOpacity: 1.0
    });
  }
}).toString();

var srcCropBox = util.printf("(%s)();", strCropBox);
var srcWatermark = util.printf("(%s)();", strWatermark);

app.addSubMenu({
  cName: "myTest", 
  cUser: "JavaScript練習", 
  cParent: "Edit", nPos: 0
});

app.addMenuItem({
  cName: "myCropBox", 
  cUser: "全ページでページボックスを変更", 
  cParent: "myTest", nPos: 0, 
  cExec: srcCropBox, 
  cEnable: 'event.rc = (event.target != null);', 
  cMarked: 'event.rc = false;', 
  bPrepend: false
});

app.addMenuItem({
  cName: "-", 
  cParent: "myTest", nPos: 1, 
  cExec: '', bPrepend: false
});

app.addMenuItem({
  cName: "myWatermark", 
  cUser: "PDFを新規作成して全ページを透かしとして入れる", 
  cParent: "myTest", nPos: 2, 
  cExec: srcWatermark, 
  cEnable: 'event.rc = (event.target != null);', 
  cMarked: 'event.rc = false;', 
  bPrepend: false
});

app.addMenuItem({
  cName: "-", 
  cParent: "myTest", nPos: 3, 
  cExec: '', bPrepend: false
});

app.addMenuItem({
  cName: "myDebugger", 
  cUser: "JavaScriptデバッガーのウィンドウを表示", 
  cParent: "myTest", nPos: 4, 
  cExec: '\
    (() => {\
      console.show();\
    })();\
  ', 
  cEnable: 'event.rc = true;', 
  cMarked: 'event.rc = false;', 
  bPrepend: false
});