VB6:使用Acrobat Type Library在VB6中合并PDF

时间:2012-10-12 17:57:15

标签: vb6 adobe acrobat

我继承了一个使用Crystal Reports 8.5来运行报告的VB6程序。然后将输出导出为PDF。然后,它使用Adobe Acrobat 5.0类型库将生成的PDF合并为单个PDF文档。我们的问题是我们需要升级Acrobat 5.0类型库,但似乎最新版本的Acrobat不提供可与VB6一起使用的类型库。有谁知道VB6支持的最新版本的Acrobat?此外,有没有人有任何建议如何升级,而无需将整个应用程序升级到.Net?提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

我向pdftk致敬。手册页中的示例:

  

将两个或多个PDF合并为新文档

     

pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf

请注意,如果您的应用是分发式且非GPL,那么您需要获得商业许可,但目前仅需24美元。

答案 1 :(得分:0)

15年前,我有同样的要求,并在vb6中创建了一个mod来做到这一点:

modMergePDF

公共功能合并PDFFiles

我最近更新了代码以处理Acrobat 10.0类型库,因此您需要将2020年8月1日之前的最新版本Acrobat DC Pro安装到...

  1. 使用以下代码
  2. 使用编译后的MergePDF.exe

此外,mod使用许多PDF的文件名添加书签,并使用清理代码 摆脱一些丑陋的文件名(如果您需要 将文件名擦洗为书签名),将其压缩为单个pdf文件。

还包括一个用于生成批处理文件代码的功能:

公共功能BuildBatchFileCode

调用传递命令的MergePDF.exe 这行由Many pdf目录和单个pdf合并文件目录组成 和文件名。您还可以传递一个标志来对CaseSensitive(任何大写字母 文件名将以小写字母排序),您可以传入另一个标志以在书签名称中保留.pdf扩展名。

在git上找到MergePDF.exe并在此处找到所有支持代码:

https://github.com/Brad-Skidmore/MergePDF

注意:错误处理参考goUtil.utErrorLog,您也可以在GitHub上找到它,也可以将其替换为自己的错误处理。 这是Mod代码:modMergePDF

'  http://www.xlsure.com 2020.07.30
' *********************************************************************
'  You are free to use this code within your own applications, but you
'  are expressly forbidden from selling or otherwise distributing this
'  source code without prior written consent.
'  Merge PDF Files - modMergePDF
' *********************************************************************

Option Explicit
'PDF documents must be declared in general declaration space and not local!
Private moMainDoc As Acrobat.AcroPDDoc
Private moTempDoc As Acrobat.AcroPDDoc

Private Property Get msClassName() As String
    msClassName = "modMergePDF"
End Property

Public Function MergePDFFiles(psRawPDFFilesDir As String, _
                                psSinglePDFOutputDir As String, _
                                psSinglePDFOutputName As String, _
                                Optional ByVal pbRemovePdfExtFromBookMark As Boolean = True, _
                                Optional pbCaseSensitiveSort As Boolean = False, _
                                Optional ByVal pbShowError As Boolean = False) As Boolean
    On Error GoTo EH
    
    Dim bFirstDoc As Boolean
    Dim sRawPDFFilesDir As String
    Dim sSinglePDFOutputDir As String
    Dim sSinglePDFOutputName As String
    Dim saryFileSort() As String
    Dim sBMName As String
    'Track pos of things
    Dim lBMPageNo As Long
    Dim lPos As Long
    Dim lFile As Long
    Dim lInsertPageAfter As Long
    Dim lNumPages As Long
    Dim lRet As Long
    'Need to use Adobe internal Java Object
    'in order to Add Book marks
    Dim oJSO As Object 'JavaScript Object
    Dim oBookMarkRoot As Object
    'File I/O
    Dim oFolder As Scripting.Folder
    Dim oFile As Scripting.File
    Dim oFSO As Scripting.FileSystemObject
    
    
    sRawPDFFilesDir = psRawPDFFilesDir
    'ensure backslash for the 2 b merged PDF files directory
    If StrComp(Right(sRawPDFFilesDir, 1), "\", vbBinaryCompare) <> 0 Then
        sRawPDFFilesDir = sRawPDFFilesDir & "\"
        psRawPDFFilesDir = sRawPDFFilesDir
    End If

    sSinglePDFOutputDir = psSinglePDFOutputDir
    sSinglePDFOutputName = psSinglePDFOutputName
    
    'ensure .pdf for the PDFOutputName (If it's CAP .PDF should be okay)
    If StrComp(Right(sSinglePDFOutputName, 4), ".pdf", vbTextCompare) <> 0 Then
        sSinglePDFOutputName = sSinglePDFOutputName & ".pdf"
        psSinglePDFOutputName = sSinglePDFOutputName
    End If

    Set oFSO = New Scripting.FileSystemObject
    
    Set oFolder = oFSO.GetFolder(sRawPDFFilesDir)
    
    bFirstDoc = True

    If oFolder.Files.Count = 0 Then
        Exit Function
    End If
    
    'Because the FSO folder files collection does not allow for
    'Native sorting, need to plug all the files into an array and sort that motha
    ReDim saryFileSort(1 To oFolder.Files.Count)
    lFile = 0
    For Each oFile In oFolder.Files
        lFile = lFile + 1
        saryFileSort(lFile) = oFile.Name
    Next
    
    'Once they is all in der sor the array
    'Sort is Case Sensitive
    If pbCaseSensitiveSort Then
        goUtil.utBubbleSort saryFileSort
    End If
    
    For lFile = 1 To UBound(saryFileSort, 1)
        If LCase(Right(saryFileSort(lFile), 4)) = ".pdf" Then
            If bFirstDoc Then
                bFirstDoc = False
                Set moMainDoc = CreateObject("AcroExch.PDDoc") 'New AcroPDDoc
                lRet = moMainDoc.Open(sRawPDFFilesDir & saryFileSort(lFile))
                Set oJSO = moMainDoc.GetJSObject
                Set oBookMarkRoot = oJSO.BookMarkRoot
                sBMName = saryFileSort(lFile)
                lPos = InStr(1, sBMName, "_{", vbBinaryCompare)
                If lPos > 0 Then
                    sBMName = left(sBMName, lPos - 1) & ".pdf"
                End If
                If pbRemovePdfExtFromBookMark Then
                    sBMName = Replace(sBMName, ".pdf", vbNullString, , , vbTextCompare)
                End If
                lRet = oBookMarkRoot.CreateChild(sBMName, "this.pageNum =0", lFile - 1)
            Else
                Set moTempDoc = CreateObject("AcroExch.PDDoc") 'New AcroPDDoc
                lRet = moTempDoc.Open(sRawPDFFilesDir & saryFileSort(lFile))
                'get the Book mark page number before the actual instert of new pages
                lBMPageNo = moMainDoc.GetNumPages
                lInsertPageAfter = lBMPageNo - 1
                lNumPages = moTempDoc.GetNumPages
                lRet = moMainDoc.InsertPages(lInsertPageAfter, moTempDoc, 0, lNumPages, 0)
                moTempDoc.Close
                If lRet = 0 Then
                    sBMName = saryFileSort(lFile)
                    lPos = InStr(1, sBMName, "_{", vbBinaryCompare)
                    If lPos > 0 Then
                        sBMName = left(sBMName, lPos - 1) & ".pdf"
                    End If
                    'Need to copy the errored document over to be included in the enitre document
                    goUtil.utCopyFile sRawPDFFilesDir & saryFileSort(lFile), sSinglePDFOutputDir & "\" & sBMName
                    sBMName = "PDF Insert Page Error_" & sBMName
                Else
                    sBMName = saryFileSort(lFile)
                    lPos = InStr(1, sBMName, "_{", vbBinaryCompare)
                    If lPos > 0 Then
                        sBMName = left(sBMName, lPos - 1) & ".pdf"
                    End If
                End If
                If pbRemovePdfExtFromBookMark Then
                    sBMName = Replace(sBMName, ".pdf", vbNullString, , , vbTextCompare)
                End If
                lRet = oBookMarkRoot.CreateChild(sBMName, "this.pageNum =" & lBMPageNo, lFile - 1)
            End If
        End If
    Next
    
    lRet = moMainDoc.Save(1, sSinglePDFOutputDir & "\" & sSinglePDFOutputName)
    moMainDoc.Close
    
    MergePDFFiles = True
    
CLEAN_UP:
    Set oFolder = Nothing
    Set oFile = Nothing
    Set oFSO = Nothing
    Set oBookMarkRoot = Nothing
    Set oJSO = Nothing
    Set moMainDoc = Nothing
    Set moTempDoc = Nothing
    
    Exit Function
EH:
    goUtil.utErrorLog Err, App.EXEName, msClassName, "Public Function MergePDFFiles", pbShowError
End Function

Public Function BuildBatchFileCode(psRawPDFFilesDir As String, _
                                    psSinglePDFOutputDir As String, _
                                    psSinglePDFOutputName As String, _
                                    pbRemovePdfExtFromBookMark As Boolean, _
                                    pbCaseSensitiveSort As Boolean) As String
    
    On Error GoTo EH
    
    Dim sRawPDFFilesDir As String: sRawPDFFilesDir = psRawPDFFilesDir
    Dim sSinglePDFOutputDir As String: sSinglePDFOutputDir = psSinglePDFOutputDir
    Dim sSinglePDFOutputName As String: sSinglePDFOutputName = psSinglePDFOutputName
    Dim bRemovePdfExtFromBookMark As Boolean: bRemovePdfExtFromBookMark = pbRemovePdfExtFromBookMark

    'ensure backslash for the 2 b merged PDF files directory
    If StrComp(Right(sRawPDFFilesDir, 1), "\", vbBinaryCompare) <> 0 Then
        sRawPDFFilesDir = sRawPDFFilesDir & "\"
        psRawPDFFilesDir = sRawPDFFilesDir
    End If

    'ensure .pdf for the PDFOutputName (If it's CAP .PDF should be okay)
    If StrComp(Right(sSinglePDFOutputName, 3), ".pdf", vbTextCompare) <> 0 Then
        sSinglePDFOutputName = sSinglePDFOutputName & ".pdf"
        psSinglePDFOutputName = sSinglePDFOutputName
    End If
    
    Dim sCommandLine As String
    
    sCommandLine = "RawPDFFilesDir|" & sRawPDFFilesDir _
                    & "|SinglePDFOutputDir|" & sSinglePDFOutputDir _
                    & "|SinglePDFOutputName|" & sSinglePDFOutputName _
                    & "|RemovePdfExtFromBookMark|" & CStr(bRemovePdfExtFromBookMark) _
                    & "|CaseSensitiveSort|" & CStr(pbCaseSensitiveSort)

    BuildBatchFileCode = """" & App.Path & "\" & App.EXEName & ".exe"" """ & sCommandLine
    Exit Function
EH:
    goUtil.utErrorLog Err, App.EXEName, msClassName, "Public Function BuildBatchFileCode"
End Function