https://teratail.com/questions/s6q59hwdpz1m64 の
「vbaでpdfのink annotationを作成、変更、移動したいです」ですが、
vbaとjsoのハイブリッドで、inkを描画できるようになりました。
「vbaでpdfのink annotationを作成、変更、移動したいです」は、
解決にしてしまいましたので、別に立てます。
良かったら、試してみてください。
Option Explicit
Sub F00xxインクアノテーション3()
'http://pdf-file.nnn2.com/?p=758
On Error Resume Next
Const FN = "C:\ほげほげ\テスト.pdf"
Dim bEnd As Boolean
Dim bRet As Boolean
Dim sRet As Boolean
Dim CN As String
Dim dts As String
Dim i As Integer
Dim icomboN As Integer
Dim iPageNum As Integer
Dim oApp As New Acrobat.AcroApp
Dim oAVDoc As New Acrobat.AcroAVDoc
Dim oFApp As New AFORMAUTLib.AFormApp
Dim oPDDoc As Acrobat.AcroPDDoc
Dim oPDPage As Acrobat.AcroPDPage
Dim objAFormApp As AFORMAUTLib.AFormApp
Dim objF As AFORMAUTLib.Fields
Dim jso As Object
Dim pageRect As Object
Dim annot As Object
Dim props As Object
Dim prop As Object
Dim FN_new As String
Dim color1(3) As Variant
Dim Aarray(7, 1) As Integer
Dim pn As Integer
Dim aname As String
Dim jsw As String
Dim x As Integer
'https://www.tipsfound.com/vba/02018
Const Red As Integer = 1
Const Green As Integer = 2
Const Blue As Integer = 4
Const Black As Integer = 0
Const White As Integer = 7
Dim color11 As Integer
Dim color12 As Integer
Dim color13 As Integer
bEnd = True
If Dir$(FN, vbNormal) = "" Then
MsgBox FN & vbCrLf & _
"ファイルが存在しない。", _
vbOKOnly + vbCritical, "実行時のエラー"
Exit Sub
End If
oApp.CloseAllDocs
bRet = oAVDoc.Open(FN, "")
If bRet = False Then
MsgBox "AVDocオブジェクトはOpen出来ません", _
vbOKOnly + vbCritical, "処理エラー"
bEnd = False
GoTo Skip_:
End If
Set oPDDoc = oAVDoc.GetPDDoc
iPageNum = oPDDoc.GetNumPages
For i = 0 To iPageNum - 1
Set oPDPage = oPDDoc.AcquirePage(i)
' Set oPoint = oPDPage.GetSize
dts = GetDateTimer()
For icomboN = 0 To 4
CN = "Ink" & icomboN & "_" & dts & "_p" & Right("0000" & (i + 1), 4)
color11 = (icomboN + 1) And Red
color12 = (icomboN + 1) And Green
color13 = (icomboN + 1) And Blue
color1(0) = "RGB"
color1(1) = color11
color1(2) = color12
color1(3) = color13
Set jso = oPDDoc.GetJSObject
Set annot = jso.AddAnnot()
Set props = annot.getProps()
props.Type = "Ink"
props.Page = i
props.Name = CN
annot.setProps props
props.strokeColor = color1
props.Width = 5
props.opacity = 0.5
annot.setProps props
Set objAFormApp = CreateObject("AFormAut.App")
Set objF = objAFormApp.Fields
Aarray(0, 0) = 10 + icomboN * 50: Aarray(0, 1) = 100
Aarray(1, 0) = 110 + icomboN * 50: Aarray(1, 1) = 200
Aarray(2, 0) = 180 + icomboN * 50: Aarray(2, 1) = 300
Aarray(3, 0) = 10 + icomboN * 50: Aarray(3, 1) = 100
Aarray(4, 0) = 20 + icomboN * 50: Aarray(4, 1) = 300
Aarray(5, 0) = 210 + icomboN * 50: Aarray(5, 1) = 500
Aarray(6, 0) = 80 + icomboN * 50: Aarray(6, 1) = 300
Aarray(7, 0) = 20 + icomboN * 50: Aarray(7, 1) = 100
pn = i
aname = CN
jsw = ""
jsw = jsw & "var gestureArr = new Array();" & vbCrLf
For x = 0 To UBound(Aarray)
jsw = jsw & "gestureArr[" & x & "] = [" & Aarray(x, 0) & "," & Aarray(x, 1) & "];" & vbCrLf
Next
jsw = jsw & "var annot = this.getAnnots(" & pn & ");" & vbCrLf
jsw = jsw & "for (var b=0; b < annot.length; b++) {" & vbCrLf
jsw = jsw & "if (annot[b].name =='" & aname & "'){" & vbCrLf
jsw = jsw & "var p = annot[b].getProps();" & vbCrLf
jsw = jsw & " for ( o in p ) {" & vbCrLf
jsw = jsw & "if (o == 'gestures'){" & vbCrLf
jsw = jsw & "p[o]=[gestureArr];" & vbCrLf
jsw = jsw & "var q = annot[b].setProps(p);" & vbCrLf
jsw = jsw & "}" & vbCrLf
jsw = jsw & "}" & vbCrLf
jsw = jsw & "}" & vbCrLf
jsw = jsw & "};" & vbCrLf
Debug.Print jsw
sRet = objF.ExecuteThisJavascript(jsw)
Set objF = Nothing
Set objAFormApp = Nothing
Next
Next
'PDFファイルを別名で保存
FN_new = Replace(FN, ".pdf", "_new6.pdf")
bRet = oPDDoc.Save(1, FN_new)
If bRet = False Then
MsgBox "PDFファイルへ保存出来ませんでした", _
vbOKOnly + vbCritical, "エラー"
bEnd = False
End If
'PDFファイルを閉じる
bRet = oAVDoc.Close(False)
If bRet = False Then
MsgBox "AVDocオブジェクトはClose出来ませんでした", _
vbOKOnly + vbCritical, "処理エラー"
bEnd = False
End If
Skip_:
On Error Resume Next
'変更しないで閉じます。
bRet = oAVDoc.Close(False)
'Acrobatアプリケーションの終了
oApp.Hide
oApp.Exit
'オブジェクトの開放
打ち消し線Set oPDPage = Nothing
Set oPDDoc = Nothing
Set props = Nothing
Set annot = Nothing
Set jso = Nothing
Set oFApp = Nothing
Set oAVDoc = Nothing
Set oApp = Nothing
If bEnd = True Then
MsgBox "処理は正常に終了しました。", _
vbOKOnly + vbInformation, "正常終了"
End If
Exit Sub
Err:
MsgBox Err.Number & vbCrLf & Err.Description, _
vbOKOnly + vbCritical, "実行時のエラー"
bEnd = False
GoTo Skip_:
End Sub
'-------------------------------
' 時分秒ミリ秒取得
'-------------------------------
Public Function GetDateTimer() As String
'https://qiita.com/Milhi315/items/254ba33a21f29aabd2dd
Dim dt As Date
Dim ye As String
Dim mon As String
Dim dat As String
Dim dts As String
Dim ms2 As Double
Dim ms3 As String
Dim timeHmsS: timeHmsS = Timer
dt = Now()
ye = Year(dt)
mon = Right("00" & Month(dt), 2)
dat = Right("00" & Day(dt), 2)
ms2 = (timeHmsS - Int(timeHmsS))
ms3 = Right("0000" & Int(ms2 * 1000), 4)
Dim timeHms: timeHms = Int(timeHmsS)
Dim timeS: timeS = timeHmsS - timeHms
Dim hourm: hourm = Int(timeHms / (60 * 60))
Dim minutem: minutem = Int((timeHms - (hourm * 60 * 60)) / 60)
Dim secondm: secondm = timeHms - (hourm * 60 * 60 + minutem * 60)
GetDateTimer = ye & mon & dat & Format(hourm, "00") & Format(minutem, "00") & Format(secondm, "000") & ms3
Debug.Print ye & mon & dat & Format(hourm, "00") & Format(minutem, "00") & Format(secondm, "000") & ms3
End Function
あなたの回答
tips
プレビュー