質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

PDF

PDF(Portable Document Format)とはISOによって国際標準として制定されている電子ドキュメント用の拡張子です。

Q&A

0回答

538閲覧

vbaでpdfのink annotationを作成、変更、移動したいです その後

snowmansnow

総合スコア9

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

PDF

PDF(Portable Document Format)とはISOによって国際標準として制定されている電子ドキュメント用の拡張子です。

0グッド

0クリップ

投稿2023/01/24 10:46

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

snowmansnow

2023/01/25 00:10

問題・課題は、前回の質問を見れば自明なように jsoを使わないvbaネイティブのgesturesの代入です。 前回も含めて良く読んでから指摘して下さい。
snowmansnow

2023/01/25 00:11

~.ExecuteThisJavascriptによるjsoでした。
snowmansnow

2023/01/25 00:20

今回は、前回の未解決の質問の続き(途中経過)でしかありません。 ちゃちゃを入れないで、アドバイスや回答を入れて下さい。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問