前回の記事ではPowerShell+iTextSharp、前々回の記事ではVBA+Adobe ReaderでPDFファイルのページ数を取得するコードを紹介しましたが、今回はAcrobatを利用してPDFファイルのページ数を取得するコードを紹介します。
Option Explicit
Public Sub Sample()
Dim num As Long
num = GetPDFPages("C:\Test\001.pdf")
If num = 0& Then
Debug.Print "Err."
Else
Debug.Print "ページ数:" & num
End If
End Sub
Public Function GetPDFPages(ByVal PdfPath As String) As Long
Dim ret As Long
ret = 0& '初期化
With CreateObject("AcroExch.AVDoc")
If .Open(PdfPath, vbNullString) Then
ret = CLng(.GetPDDoc.GetNumPages)
.Close 1&
End If
End With
GetPDFPages = ret
End Function
Acrobatが必要になりますが、非常に簡単なコードでページ数を取得することができます。
しかもPDF編集アプリケーションの本家本元であるAcrobatを利用しているため、信頼性も高く安心感があります。
個人的には、PDFファイル関連の処理をマクロで行う場合は、Acrobatを利用することをお薦めします。
■ 関連Webページ:
・Adobe Readerを利用してPDFファイルのページ数を取得するVBAマクロ
//www.ka-net.org/blog/?p=2314
・[PowerShell]iTextSharpを使ってPDFファイルのページ数を取得する
//www.ka-net.org/blog/?p=2317



















この記事へのコメントはありません。