xlogI125’s blog

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

Acrobat IAC練習 addWatermarkFromText

メモ

透かしを追加(テキスト)

使い捨てスクリプト

Acrobat JavaScript

// Acrobat Standard DC (2022年3月頃), Windows 11
// JavaScript デバッガー の コンソール から実行

(() => {
  this.addWatermarkFromText({
    cText: "テキスト 1行目\rline 2行目\rstr 3行目", 
    nTextAlign: app.constants.align.left, 
    cFont: "MSGothic", 
    // cFont: "MSMincho", 
    nFontSize: 10.5, 
    aColor: ["RGB", 192 / 255, 0 / 255, 0 / 255], 
    nStart: this.pageNum, 
    nEnd: this.pageNum, 
    bOnTop: true, 
    bOnScreen: true, 
    bOnPrint: true, 
    nHorizAlign: app.constants.align.left, 
    nVertAlign: app.constants.align.top, 
    nHorizValue: 30 * 72 / 25.4, 
    nVertValue: -35 * 72 / 25.4, 
    bPercentage: false, 
    nScale: 1.0, 
    bFixedPrint: false, 
    nRotation: 0, 
    nOpacity: 1.0
  });
})();

VBA

' Excel 2019, Acrobat Standard DC (2022年3月頃), Windows 11

' 参照設定
' Acrobat (Adobe Acrobat 10.0 Type Library)

Option Explicit

Public Sub JsoAddWatermarkFromText( _
    ByVal jso As Object, _
    ByVal cText As String, ByVal nTextAlign As Long, ByVal cFont As String, _
    ByVal nFontSize As Double, ByRef aColor() As Variant, _
    ByVal nStart As Long, ByVal nEnd As Long, _
    ByVal bOnTop As Boolean, _
    ByVal bOnScreen As Boolean, ByVal bOnPrint As Boolean, _
    ByVal nHorizAlign As Long, ByVal nVertAlign As Long, _
    ByVal nHorizValue As Double, ByVal nVertValue As Double, _
    ByVal bPercentage As Boolean, _
    ByVal nScale As Double, _
    ByVal bFixedPrint As Boolean, _
    ByVal nRotation As Long, ByVal nOpacity As Double _
  )

  jso.AddWatermarkFromText _
    cText, nTextAlign, cFont, _
    nFontSize, aColor, _
    nStart, nEnd, _
    bOnTop, _
    bOnScreen, bOnPrint, _
    nHorizAlign, nVertAlign, _
    nHorizValue, nVertValue, _
    bPercentage, _
    nScale, _
    bFixedPrint, _
    nRotation, nOpacity

End Sub


Public Sub Main()

#Const DEBUG_ = False

#If DEBUG_ Then
  Dim avApp As Acrobat.AcroApp
  Dim avDoc As Acrobat.AcroAVDoc
  Dim pdDoc As Acrobat.AcroPDDoc
#Else
  Dim avApp As Object
  Dim avDoc As Object
  Dim pdDoc As Object
#End If

  Dim jso As Object
  Dim pageNum As Long

  On Error GoTo ErrorHandler

  Set avApp = VBA.Interaction.CreateObject(Class:="AcroExch.App")
  Set avDoc = avApp.GetActiveDoc()
  Set pdDoc = avDoc.GetPDDoc()
  Set jso = pdDoc.GetJSObject()
  pageNum = avDoc.GetAVPageView().GetPageNum()

  Dim color(0 To 3) As Variant
  color(0) = "RGB"
  color(1) = 192 / 255
  color(2) = 0 / 255
  color(3) = 0 / 255

  JsoAddWatermarkFromText _
    jso:=jso, _
    cText:= _
      "テキスト 1行目" & VBA.Constants.vbCrLf & _
      "line 2行目" & VBA.Constants.vbCrLf & _
      "str 3行目", _
    nTextAlign:=jso.app.Constants.Align.Left, _
    cFont:="MSGothic", _
    nFontSize:=10.5, _
    aColor:=color, _
    nStart:=pageNum, _
    nEnd:=pageNum, _
    bOnTop:=True, _
    bOnScreen:=True, _
    bOnPrint:=True, _
    nHorizAlign:=jso.app.Constants.Align.Left, _
    nVertAlign:=jso.app.Constants.Align.Top, _
    nHorizValue:=30 * 72 / 25.4, _
    nVertValue:=-35 * 72 / 25.4, _
    bPercentage:=False, _
    nScale:=1, _
    bFixedPrint:=False, _
    nRotation:=0, _
    nOpacity:=1

ErrorHandler:
  Set jso = Nothing
  Set pdDoc = Nothing
  Set avDoc = Nothing
  Set avApp = Nothing
  Debug.Print VBA.Conversion.Error(VBA.Information.Err.Number)

End Sub