xlogI125’s blog

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

Acrobat IAC練習 注釈(FreeText, Square)

使い捨てスクリプト

注釈でテキストボックスと長方形を追加 (AddNewAnnot)

  • PDFを開いたウィンドウを表示させた状態でマクロを実行してください
  • 参照設定が必要です
' Excel 2019, Acrobat Standard DC (2022年3月頃), Windows 11

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

Option Explicit

Public Sub Main()
  Dim avApp As Acrobat.AcroApp
  Dim avDoc As Acrobat.AcroAVDoc
  Dim pdDoc As Acrobat.AcroPDDoc
  Dim pdPage As Acrobat.AcroPDPage
  Dim pdAnnot As Acrobat.AcroPDAnnot
  Dim rect As Acrobat.AcroRect
  Dim pageNum As Long

  On Error GoTo ErrorHandler

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

  Set rect = New Acrobat.AcroRect

  ' テキストボックス (FreeText)
  rect.Left = 10 * 72 / 25.4
  rect.bottom = 20 * 72 / 25.4
  rect.Right = 100 * 72 / 25.4
  rect.Top = 50 * 72 / 25.4

  Set pdAnnot = pdPage.AddNewAnnot(-1, "FreeText", rect)
  pdAnnot.SetColor &H80C0FF
  pdAnnot.SetTitle "プロパティ 一般タブ 作成者 FreeText"
  pdAnnot.SetContents "テキストボックス注釈"

  ' 長方形 (Square)
  rect.Left = 10 * 72 / 25.4
  rect.bottom = 70 * 72 / 25.4
  rect.Right = 150 * 72 / 25.4
  rect.Top = 100 * 72 / 25.4

  Set pdAnnot = pdPage.AddNewAnnot(-1, "Square", rect)
  pdAnnot.SetColor &HFFC080
  pdAnnot.SetTitle "プロパティ 一般タブ 作成者 Square"
  pdAnnot.SetContents ""

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

End Sub

フラット化 (flattenPages)

  • PDFを開いたウィンドウを表示させた状態でマクロを実行してください
' Excel 2019, Acrobat Standard DC (2022年3月頃), Windows 11

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

Option Explicit

Public Sub PdfFlattenPage()

#Const DEBUG_ = False

#If DEBUG_ Then
  Dim avApp As Acrobat.AcroApp
  Dim avDoc As Acrobat.AcroAVDoc
#Else
  Dim avApp As Object
  Dim avDoc 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 jso = avDoc.GetPDDoc().GetJSObject()
  pageNum = avDoc.GetAVPageView().GetPageNum()

  jso.syncAnnotScan

  ' 実行前に十分な注意が必要
  jso.flattenPages pageNum

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

End Sub

過去記事

dy100ms.hatenadiary.jp