xlogI125’s blog

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

Acrobat JavaScript練習 他のPDFを透かしで入れる

メモ

  • 新旧比較を行う資料を作成する際の使用を想定。

使い捨てスクリプト

addWatermarkFromFile

  • nHorizAlign を上手く調整すれば、複数ページを1枚に集約する手段として使えるかもしれない。
// Acrobat Pro DC (2022年1月頃), Windows 11
// JavaScript デバッガー の コンソール から実行

(() => {
  const doc = this;

  const folderPath 
  = "/C/Users/username/Desktop/新しいフォルダー/";

// /*
  const fileNames = [
    "添付資料1 A4サイズ縦1枚.pdf", 
    "添付資料2 A4サイズ縦1枚.pdf", 
    "添付資料3 A4サイズ縦1枚.pdf", 
    "添付資料4 A4サイズ縦1枚.pdf", 
    "添付資料5 A4サイズ縦1枚.pdf"
  ];

  const pageNum1Baseds = [1];
// */

/*
  const fileNames = [
    "添付資料 A4サイズ縦一式.pdf"
  ];

  // 連番の生成
  const pageNum1Baseds 
  = [...(new Array(5))].map( (_null, i) => i + 1);
  // #=> [1, 2, 3, 4, 5];

  // 特定のページを対象とする場合
  // const pageNum1Baseds = [1, 3, 5];
*/

  fileNames.forEach( (fileName, i) => {
    const filePath = folderPath + fileName;

    pageNum1Baseds.forEach( (_null, j, pageNums) => {
      doc.addWatermarkFromFile({
        cDIPath: filePath, 
        /* 0-based index */
        nSourcePage: 0 + (pageNums[j] - 1), 
        /* 0-based index */
        nStart: i + j, 
        nEnd: i + j, 
        bOnTop: true, 
        bOnScreen: true, 
        bOnPrint: true, 
        nHorizAlign: app.constants.align.left, 
        nVertAlign: app.constants.align.center, 
        nHorizValue: Math.round((72/25.4) * 0), 
        nVertValue: Math.round((72/25.4) * 0), 
        bPercentage: false, 
        nScale: 1.0, 
        bFixedPrint: false, 
        nRotation: 0, 
        nOpacity: 1.0
      });
    });
  });
})();

insertPages

  • ページを交互に並べ替える
// Acrobat Pro DC (2022年1月頃), Windows 11
// JavaScript デバッガー の コンソール から実行

(() => {
  const dstDoc = this;

  const srcDocNumPages = 5;

  var i;

  // この文書(dst)のページ順序 12345
  // 別の文書(src)のページ順序 ABCDE
  // dstにsrcを差し込んで 1A2B3C4D5E とする。

  for (i = 0; i < srcDocNumPages; i++) {
    dstDoc.insertPages({
      nPage: 2 * i, 
      cPath: "/C/Users/username/Desktop/別の文書.pdf", 
      nStart: i, 
      nEnd: i
    });
  }

})();