Sub ExtractPDFText()Dim pdfApp As Acrobat.AcroAppDim pdfDoc As Acrobat.CAcroPDDocDim pdfPage As Acrobat.AcroPDPageDim txtData As StringDim i As IntegerDim filePath As StringDim outputFolder As StringDim outputFileName As String' 初始化Acrobat对象Set pdfApp = CreateObject("AcroExch.App")pdfApp.Show' 设置输入和输出文件夹filePath = "C:\path\to\your\pdf\files\" ' 修改为PDF文件所在文件夹路径outputFolder = "C:\path\to\output\folder\" ' 修改为输出文本文件的文件夹路径' 创建文件夹如果它不存在If Not Dir(outputFolder, vbDirectory)<> vbNullString ThenMkDir outputFolderEnd If' 获取PDF文件列表filePath = filePath & Dir("*.pdf")While filePath <>""' 打开PDF文档Set pdfDoc = CreateObject("AcroExch.PDDoc")pdfDoc.Open(filePath)' 遍历文档中的每一页For i =0 To pdfDoc.GetNumPages()-1Set pdfPage = pdfDoc.AcquirePage(i)txtData = pdfPage.GetText()' 这里可以添加代码处理txtData,例如保存到文件' 提取文本并保存到文件outputFileName = outputFolder &"\" & GetFilenameFromPath(filePath) & "-" & i & ".txt"SaveTextToFile txtData, outputFileName' 释放页对象pdfDoc.ReleasePage(pdfPage)Next i' 关闭文档并释放对象pdfDoc.Close()Set pdfDoc = Nothing' 获取下一个PDF文件filePath = Dir()Wend' 关闭Acrobat对象pdfApp.ExitSet pdfApp = Nothing
End Sub' 获取文件名称不包含路径
Function GetFilenameFromPath(filePath As String) As StringGetFilenameFromPath = Right(filePath, Len(filePath)- InStrRev(filePath, "\"))
End Function' 将文本保存到文件
Sub SaveTextToFile(textData As String, filePath As String)Dim fileNum As IntegerfileNum = FreeFile()Open filePath For Output As #fileNumPrint #fileNum, textDataClose #fileNum
End Sub