Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 merge of PDF files. 1

Status
Not open for further replies.

denimined

Programmer
Sep 29, 2004
54
CA
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.)

Thanks.
 
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?
 
You might want to look at PDFtk Server. It provides a command line ability to merge PDF files, which you could shell from VB
 
>This wouldn't work if the pdfs have graphics.

Or layout, or typography ...
 
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.
 
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()

'NEED TO MODIFY TO WORK FOR UG------

    'Combined multiple PDF files into one
    'set a reference to Acrobat (Adobe Acrobat 7.0 Type Library)
    '[URL unfurl="true"]http://www.khk.net/wordpress/2009/03/04/adobe-acrobat-and-vba-an-introduction/[/URL]
    '30-JUL-2010
    
    'Added code to close the open pdf files (except for the final merged report)
    '09-Jun-2011
    
    '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
        Part2Document.Close
        
        'Close the open pdf files except for the Merged report
        'FollowHyperlink Replace(pdfsrc, "0", X - 1), , True, False
        'SendKeys "%{F4}", False
        DoEvents
        On Error Resume Next
'        Call WinClose(Replace(Mid(pdfsrc, InStrRev(pdfsrc, "\") + 1), "0", x - 1) & " - Adobe Acrobat Pro")
        DoEvents
        
                
        Part2Document.Open (Replace(pdfsrc, "0", x))
        'Debug.Print (Replace(pdfsrc, "0", x))
    Loop
    
    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
        
    Part1Document.Close
    Part2Document.Close
     
    AcroApp.Exit
    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
    '[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1252388[/URL]
    'modified from [URL unfurl="true"]http://diaryproducts.net/for/programmer/adobe_acrobat_pdf_scripting_visualbasic_javascript[/URL]
    
    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
        DoEvents
    
    'Initialize JavaScript
        Set template = pdDoc.GetJSObject
    
    'Place the template as a watermark
        template.addWatermarkFromFile BackgroundPDF
    
    'Save
        pdDoc.Save 1, OutputPDF
    
    'Close & Destroy Objects
        pdDoc.Close
        Set pdDoc = Nothing
        Set template = Nothing
        Set fso = Nothing
End Function
 
>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.

 
That's kind of why I decided to post in case it was of use to someone who already had acrobat.
 
I use Nitro Pro to merge as many PDS's as you wish into one.
 
>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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top