Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


VB6 merge of PDF files.

VB6 merge of PDF files.

VB6 merge of PDF files.

Hi there.

Our existing software produces reports that can be e-mailed on to customers, managers, etc. We have a client that wants to attach their own (legal) document whenever a document is e-mailed out. When e-mailed, our program already writes out a PDF that can then be attached to the outgoing e-mail. What I would like to do is have the extra document as a PDF and just prior to e-mail, merge the two PDF's into one that can then be e-mailed on. Preferably all of this would happen without user intervention.

Does anyone have any suggestions on third party tools that can be incorporated into VB6 that would allow for background merging of two PDF files? (Also, our software is being upgraded into .NET, so hopefully any solution would be available to both platforms.)


RE: VB6 merge of PDF files.

I have seen routines for converting PDF to text.
You may be able to shell to these in turn to convert the original docs to text using VB6 file routines (maybe copy files to be created in a dedicated folder with nothing else first)
Then 'append' the files into one text file.
Then shell to a conversion routine to make a new pdf out of the compiled file.

This wouldn't work if the pdfs have graphics.

I notice there are heaps of on line sites that will convert your pdf to text on line but I wonder if they are all a scam where they troll through your private documents to find something they can use to defraud you or sell to a competitor?

RE: VB6 merge of PDF files.

You might want to look at PDFtk Server. It provides a command line ability to merge PDF files, which you could shell from VB

RE: VB6 merge of PDF files.

>This wouldn't work if the pdfs have graphics.

Or layout, or typography ...

RE: VB6 merge of PDF files.

And the solution is....

... avoid the problem.
We're going to go with - they create an image of their legal document and we'll add that at the end of the report generation - right before we send it off to be PDF generated. Problem solved. Er - avoided.

Thx all.

RE: VB6 merge of PDF files.

If you have full version of Adobe Acrobat, this might work:

Here is some code I ran several years ago in Access VBA based on above to merge also include code for doing customized watermark (background image). Perhaps with some modification would work with VB? I changed the parts that would need to refer to your file names, locations with "Your..." It has been a while since I used the code (previous job) so depending on your process you may not need to loop and the section after the loop that defines the merged pdf name may be streamlined if your naming conventions are more straightforward as this code was adjusting the name based on various conditions.

CODE -->

Sub MergePDF()


    'Combined multiple PDF files into one
    'set a reference to Acrobat (Adobe Acrobat 7.0 Type Library)
    'Added code to close the open pdf files (except for the final merged report)
    'May need to see if file exists when this is run more than once.  Will add if needed
    Dim AcroApp As Acrobat.CAcroApp
    Dim Part1Document As Acrobat.CAcroPDDoc
    Dim Part2Document As Acrobat.CAcroPDDoc
    Dim numPages As Integer
    Dim pdfsrc As String
    Dim x As Integer
    Dim stMergename As String
    Set AcroApp = CreateObject("AcroExch.App")
    Set Part1Document = CreateObject("AcroExch.PDDoc")
    Set Part2Document = CreateObject("AcroExch.PDDoc")
    pdfsrc = "\\Server\PDFfiles\YourPDFfile.pdf"
    x = 1
    Part1Document.Open (pdfsrc)
    Part2Document.Open (Replace(pdfsrc, "0", x))
    Do While x < 9
        ' Insert the pages of Part2 after the end of Part1
        numPages = Part1Document.GetNumPages()
        If Part1Document.InsertPages(numPages - 1, Part2Document, 0, Part2Document.GetNumPages(), True) = False Then
            MsgBox "Cannot insert pages for " & (Replace(pdfsrc, "0", x)) & ".  See if it is on the disk.  If not, please recreate."
            MsgBox "Close all open Adobe Acrobat windows, before reprinting these reports."
            Exit Sub
        End If
        x = x + 1
        'Close the open pdf files except for the Merged report
        'FollowHyperlink Replace(pdfsrc, "0", X - 1), , True, False
        'SendKeys "%{F4}", False
        On Error Resume Next
'        Call WinClose(Replace(Mid(pdfsrc, InStrRev(pdfsrc, "\") + 1), "0", x - 1) & " - Adobe Acrobat Pro")
        Part2Document.Open (Replace(pdfsrc, "0", x))
        'Debug.Print (Replace(pdfsrc, "0", x))
    If Me.chkFall = True Then stTerm = "F" Else stTerm = "S"
    stMergename = Replace(pdfsrc, "YourPDFfile0", "YourMergeNamePrefix" & Right(Me.txtCurrentYr, 2) & stTerm & "_" & Format(Me.txtRunDate, "YYYYMMDD") & "Full")
    If Part1Document.Save(PDSaveFull, stMergename) = False Then
        MsgBox "Cannot save the modified document"
    End If
    Set AcroApp = Nothing
    Set Part1Document = Nothing
    Set Part2Document = Nothing
    'FileCopy Replace(pdfsrc, "0", "1"), Replace(stMergename, "Full", "")
    FileCopy pdfsrc, Replace(stMergename, "Full", "")
    'Call WinClose("rptCover.pdf - Adobe Acrobat Pro")
    'Open merged file for review and printing
'    FollowHyperlink stMergename, , True, False
    MsgBox "Done"
End Sub

Private Function ApplyBackgroundToPDF(BasePDF As String, BackgroundPDF As String, OutputPDF As String)
    'Programmatically Add Watermark to PDF Document
    'thread222 -1252388
    'modified from http://diaryproducts.net/for/programmer/adobe_acrobat_pdf_scripting_visualbasic_javascript
    Dim pdDoc As Acrobat.AcroPDDoc
    Dim fso As New FileSystemObject
    Dim template As Variant
    'Check for existence of output file
    If fso.FileExists(OutputPDF) Then fso.DeleteFile OutputPDF
    'Open base document
        Set pdDoc = CreateObject("AcroExch.PDDoc")
        pdDoc.Open BasePDF
    'Initialize JavaScript
        Set template = pdDoc.GetJSObject
    'Place the template as a watermark
        template.addWatermarkFromFile BackgroundPDF
        pdDoc.Save 1, OutputPDF
    'Close & Destroy Objects
        Set pdDoc = Nothing
        Set template = Nothing
        Set fso = Nothing
End Function 

RE: VB6 merge of PDF files.

>If you have full version of Adobe Acrobat
Yep, if indeed. Mind you the whole tone of the OP suggests they do not have Arcrobat directly available, and that seems to be supported by the later "right before we send it off to be PDF generated". Still should be a valuable option for those people finding this thread who do have Acrobat.

RE: VB6 merge of PDF files.

That's kind of why I decided to post in case it was of use to someone who already had acrobat.

RE: VB6 merge of PDF files.

I use Nitro Pro to merge as many PDS's as you wish into one.

RE: VB6 merge of PDF files.

>Nitro Pro

How does one incorporate that into VB6, given it has no automation capability, lack of an API, and a somewhat poor commandline interface?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close