20260425

[Acrobat Javascript] 左右見開きPDFを単ページに分割(右→左のRTL順分割)

[Acrobat Javascript] 左右見開きPDFを単ページに分割(右→左のRTL順分割)

NOTE記事一覧ですnote.com

サンプルソース(参考)
行番号ソース
001//コンソールを開く
002console.println("\n");
003//開いているPDFのファイルパス
004var strActivDocFilePath = this.path;
005//最後のカンマまでのキャラクタ数
006var strBaseFileName = strActivDocFilePath.lastIndexOf(".");
007//保存先ファイルパス
008var strSaveFilePath = strActivDocFilePath.substring(0, strBaseFileName) + ".分割済" + strActivDocFilePath.substring(strBaseFileName);
009//初期ページ数
010var numAllPage = this.numPages;
011//初期ページ数 0スタート
012for (var makePageNo = numAllPage - 1;
013  makePageNo >= 0;
014  //ページの複製
015  makePageNo--) {
016    this.insertPages({ cPath: strActivDocFilePath, nPage: makePageNo, nStart: makePageNo, nEnd: makePageNo });
017} var numAllPage = this.numPages;
018//ゼロスタートのページ数でループ
019for (var nPage = 0;
020  nPage < numAllPage;
021  nPage++) {
022  //ページの回転を取得して
023    var numPageRotation = this.getPageRotation(nPage);
024  //CROP BOXのRECTを取得
025  var rectCropBox = this.getPageBox("Crop", nPage);
026  //PageBoxはページの回転を反映している
027  var CropBoxSizeHeight = rectCropBox[1];
028  var CropBoxSizeWidth = rectCropBox[2];
029  //ゼロページに1足して実ページ番号
030  var strPrintPageNo = (nPage + 1);
031  //左右の分割はPageBOXが回転を反映後の値なので同じ内容で良いのですが
032  //処理の内容をわかりやすくするように記述している
033  //【0】ページの回転に合わせての分岐
034  if (numPageRotation == 0) {
035    console.println("ページ番号: " + strPrintPageNo + "\t回転: " + numPageRotation + "\t天地/上下");
036    //左右の分割
037    if (nPage % 2 === 0) {
038      var rectSetCropRectBox = [(CropBoxSizeWidth / 2), CropBoxSizeHeight, CropBoxSizeWidth, rectCropBox[3]];
039      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
040      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
041    } else {
042      var rectSetCropRectBox = [rectCropBox[0], CropBoxSizeHeight, (CropBoxSizeWidth / 2), rectCropBox[3]];
043      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
044      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
045    }
046    //【90】ページの回転に合わせての分岐
047  } else if (numPageRotation == 90) {
048    console.println("ページ番号: " + strPrintPageNo + "\t回転: " + numPageRotation + "\t天地/右左");
049    if (nPage % 2 === 0) {
050      var rectSetCropRectBox = [(CropBoxSizeWidth / 2), CropBoxSizeHeight, CropBoxSizeWidth, rectCropBox[3]];
051      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
052      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
053    } else {
054      var rectSetCropRectBox = [rectCropBox[0], CropBoxSizeHeight, (CropBoxSizeWidth / 2), rectCropBox[3]];
055      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
056      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
057    }
058    //【180】ページの回転に合わせての分岐
059  } else if (numPageRotation == 180) {
060    console.println("ページ番号: " + strPrintPageNo + "\t回転: " + numPageRotation + "\t天地/下上");
061    if (nPage % 2 === 0) {
062      var rectSetCropRectBox = [(CropBoxSizeWidth / 2), CropBoxSizeHeight, CropBoxSizeWidth, rectCropBox[3]];
063      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
064      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
065    } else {
066      var rectSetCropRectBox = [rectCropBox[0], CropBoxSizeHeight, (CropBoxSizeWidth / 2), rectCropBox[3]];
067      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
068      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
069    }
070    //【270】ページの回転に合わせての分岐
071  } else if (numPageRotation == 270) {
072    console.println("ページ番号: " + strPrintPageNo + "\t回転: " + numPageRotation + "\t天地/左右");
073    if (nPage % 2 === 0) {
074      var rectSetCropRectBox = [(CropBoxSizeWidth / 2), CropBoxSizeHeight, CropBoxSizeWidth, rectCropBox[3]];
075      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
076      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
077    } else {
078      var rectSetCropRectBox = [rectCropBox[0], CropBoxSizeHeight, (CropBoxSizeWidth / 2), rectCropBox[3]];
079      this.setPageBoxes({ cBox: "Trim", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
080      this.setPageBoxes({ cBox: "Crop", nStart: nPage, nEnd: nPage, rBox: rectSetCropRectBox });
081    }
082    //【Error】Acrobatは回転360は無いのでエラー
083  } else {
084    console.println("エラーPDFのページ回転構造に問題があります");
085    console.println("ページ番号: " + strPrintPageNo + "\t回転: " + numPageRotation);
086  }
087  //分割後のファイルを『別名で』保存 
088} this.saveAs({ cPath: strSaveFilePath, bCopy: true, bPromptToOverwrite: true });
089this.closeDoc({ bNoSave: true });
090app.openDoc({ cPath: strSaveFilePath });
AppleScriptで生成しました