首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB6:使用Acrobat类型库合并VB6中的PDF

VB6:使用Acrobat类型库合并VB6中的PDF
EN

Stack Overflow用户
提问于 2012-10-12 17:57:14
回答 2查看 2.6K关注 0票数 2

我继承了一个VB6程序,它使用水晶Reports 8.5来运行报表&然后将输出导出到PDF中。然后,它使用AdobeAcrobat5.0类型库将结果PDF合并到一个PDF文档中。我们的问题是我们需要升级Acrobat5.0类型库,但是最新版本的Acrobat似乎没有提供与VB6一起工作的类型库。有人知道VB6中支持的最新版本的Acrobat吗?另外,有没有人对如何在不将整个应用程序升级到.Net的情况下进行升级有任何建议?提前感谢您提供的任何帮助。

EN

回答 2

Stack Overflow用户

发布于 2012-11-08 16:17:11

我会向pdftk支付费用。手册页中的示例:

将两个或多个PDF合并到一个新文档中 pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf

请注意,如果您的应用程序是分布式和非GPL,您将需要一个商业许可证,但它目前只需24美元。

票数 0
EN

Stack Overflow用户

发布于 2020-08-01 21:45:25

15年前,我也有同样的要求,并在vb6中创建了一个模块来实现这个目标:

modMergePDF

公共功能MergePDFFiles

我最近更新了处理Acrobat10.0类型库的代码,因此您需要在2020年8月1日安装最新的Acrobat DC Pro,以便.

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

此外,mod使用多个PDF的文件名添加书签,并使用省去代码消除一些丑陋的文件名(如果需要将文件名删除为书签),则将其添加到单个pdf文件中。

还包括一个生成批处理文件代码的函数:

公共功能BuildBatchFileCode

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

在git上查找包含所有支持代码的MergePDF.exe:

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

注意:错误处理指的是goUtil.utErrorLog,您也可以在GitHub上找到它,或者您可以用自己的错误处理替换它。下面是Mod代码: modMergePDF

代码语言:javascript
复制
'  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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12864597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档