Solved Adding a page to a pdf (2 Viewers)

Sun_Force

Active member
Local time
Today, 20:13
Joined
Aug 29, 2020
Messages
396
I think I'm expecting too much of VBA, but is there any way to add a page to a pdf file?
Let's say I have a pdf file, and I want to add another pdf as a new page to it. Or simply combine these two pdfs.

Is there any chance I can do it with vba?
Both files' path is saved in a table in MS Access.

Thanks for any kind of thought/idea/advice.

Edit : Microsoft 365
Adobe Acrobat Pro
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:13
Joined
Feb 28, 2001
Messages
27,148
Since you have Adobe Acrobat Pro, there is a good chance you have a library file that you can use. That question isn't whether Access can do what you want. It is whether Access can persuade Adobe Acrobat Pro to do what you want. I have no direct experience with this, but the general idea would be to (a) figure out to do what you want manually (and take notes), then (b) open Adobe as an application object and have Access perform the appropriate commands from VBA. That is because usually, if you have a .DLL or other type of library that exposes Component Object Model automation, that automation mirrors the Windows UI.

This article suggests that such a library exists and demonstrates how to start using it from VBA.


You might have to research more articles online, but this extremely strongly suggests that what you want is doable. Sadly, at this point I must step away because I don't have that product and can't offer much in the way of guidance beyond that starting point.
 

sxschech

Registered User.
Local time
Today, 04:13
Joined
Mar 2, 2010
Messages
792
PDFtk Server works well for me. Here is a code sample snippet to merge files. The first part of the code checks if pdftk is installed so that it won't try to run if not available.


Code:
...
...
    If InStr(Environ("Path"), "pdftk") = 0 Then
        MsgBox "It appears that PDF tool kit is not installed on this computer.  " & _
                "Please refer to user guide for instructions on how to install it. " & _
                "This program is needed in order to merge pdf files.", vbExclamation + vbOKOnly, "Missing Program PDFtk"
        MergeRFI = ""
        Exit Function
    End If


    stMergeFiles = Chr(34) & PDFName1 & Chr(34) & " " & Chr(34) & PDFName2 & Chr(34) & " cat output " & Chr(34) & Replace(PDFName1, ".pdf", "M.pdf" & Chr(34))


    retVal = Shell("pdftk " & stMergeFiles, vbMaximizedFocus)
        MsgBox "Please wait for pdftk 'BLACK SCREEN' to close and then press OK to continue." & vbCrLf & vbCrLf & "Note:Big files may take longer to merge.  Please be patient", vbOKOnly + vbExclamation, "Files Merged"
        DoEvents
...
...
 

Sun_Force

Active member
Local time
Today, 20:13
Joined
Aug 29, 2020
Messages
396
I was able to do the task without any addin, toolkit or plugin.

In case anyone has the same question in future, it's the necessary code.

Preparation : Add a reference to adobe

SQL:
Sub AcroExch_PDDoc_InsertPages()

    Dim AcroPDDocNew    As New Acrobat.AcroPDDoc
    Dim AcroPDDocAdd    As New Acrobat.AcroPDDoc
    Dim lRet            As Long
    Dim lGetNumPages    As Long
    Dim lPages          As Long
    
    lPages = 0

    ' Create a blank pdf
    lRet = AcroPDDocNew.Create()
    
    ' Add the first file to the blank pdf
    lRet = AcroPDDocAdd.Open("D:\First.pdf")
    lGetNumPages = AcroPDDocAdd.GetNumPages()
    lRet = AcroPDDocNew.InsertPages(lPages - 1, AcroPDDocAdd, 0, lGetNumPages, True)
    lRet = AcroPDDocAdd.Close()
    
    lPages = lPages + lGetNumPages
    
    ' add the second file to the pdf file
    lRet = AcroPDDocAdd.Open("D:\Second.pdf")
    lGetNumPages = AcroPDDocAdd.GetNumPages()
    lRet = AcroPDDocNew.InsertPages(lPages - 1, AcroPDDocAdd, 0, lGetNumPages, True)
    lRet = AcroPDDocAdd.Close()
    
    ' save the result
    lRet = AcroPDDocNew.Save(1, "D:\Result.pdf")
    lRet = AcroPDDocNew.Close()
    
    ' close the object
    Set AcroPDDocAdd = Nothing
    Set AcroPDDocNew = Nothing

End Sub
 

morsy_soliman

New member
Local time
Today, 06:13
Joined
Apr 26, 2017
Messages
5
PDFtk Server works well for me. Here is a code sample snippet to merge files. The first part of the code checks if pdftk is installed so that it won't try to run if not available.


Code:
...
...
    If InStr(Environ("Path"), "pdftk") = 0 Then
        MsgBox "It appears that PDF tool kit is not installed on this computer.  " & _
                "Please refer to user guide for instructions on how to install it. " & _
                "This program is needed in order to merge pdf files.", vbExclamation + vbOKOnly, "Missing Program PDFtk"
        MergeRFI = ""
        Exit Function
    End If


    stMergeFiles = Chr(34) & PDFName1 & Chr(34) & " " & Chr(34) & PDFName2 & Chr(34) & " cat output " & Chr(34) & Replace(PDFName1, ".pdf", "M.pdf" & Chr(34))


    retVal = Shell("pdftk " & stMergeFiles, vbMaximizedFocus)
        MsgBox "Please wait for pdftk 'BLACK SCREEN' to close and then press OK to continue." & vbCrLf & vbCrLf & "Note:Big files may take longer to merge.  Please be patient", vbOKOnly + vbExclamation, "Files Merged"
        DoEvents
...
...
PDFtk Server works well for me. Here is a code sample snippet to merge files. The first part of the code checks if pdftk is installed so that it won't try to run if not available.


Code:
...
...
    If InStr(Environ("Path"), "pdftk") = 0 Then
        MsgBox "It appears that PDF tool kit is not installed on this computer.  " & _
                "Please refer to user guide for instructions on how to install it. " & _
                "This program is needed in order to merge pdf files.", vbExclamation + vbOKOnly, "Missing Program PDFtk"
        MergeRFI = ""
        Exit Function
    End If


    stMergeFiles = Chr(34) & PDFName1 & Chr(34) & " " & Chr(34) & PDFName2 & Chr(34) & " cat output " & Chr(34) & Replace(PDFName1, ".pdf", "M.pdf" & Chr(34))


    retVal = Shell("pdftk " & stMergeFiles, vbMaximizedFocus)
        MsgBox "Please wait for pdftk 'BLACK SCREEN' to close and then press OK to continue." & vbCrLf & vbCrLf & "Note:Big files may take longer to merge.  Please be patient", vbOKOnly + vbExclamation, "Files Merged"
        DoEvents
...
...
@sxschech

I have a Free PDF Toolkit. it is free I tried to use your sample snippet to merge files but it did not work for me. I am a beginner in VBA. I am trying to us MS Access 64 bit VBA to merge "a.pdf" and "b.pdf" file to "ab.pdf". Would you please help me?
 

sxschech

Registered User.
Local time
Today, 04:13
Joined
Mar 2, 2010
Messages
792
I have a Free PDF Toolkit PDF Toolkit
Since you mentioned the free toolkit, would like to check whether you installed the PDFtk Free (Graphical tool) or PDFtk Server which is also free.

The code works with the PDFtk Server.

If you are using the Server version then, did you use the code I provided and where is the error happening or what is the error message?
 

morsy_soliman

New member
Local time
Today, 06:13
Joined
Apr 26, 2017
Messages
5
Since you mentioned the free toolkit, would like to check whether you installed the PDFtk Free (Graphical tool) or PDFtk Server which is also free.

The code works with the PDFtk Server.

If you are using the Server version then, did you use the code I provided and where is the error happening or what is the error message?
I installed PDFtk Server. I used the Code below

...
...
If InStr(Environ("Path"), "pdftk") = 0 Then
MsgBox "It appears that PDF tool kit is not installed on this computer. " & _
"Please refer to user guide for instructions on how to install it. " & _
"This program is needed in order to merge pdf files.", vbExclamation + vbOKOnly, "Missing Program PDFtk"
MergeRFI = ""
Exit Function
End If


stMergeFiles = Chr(34) & PDFName1 & Chr(34) & " " & Chr(34) & PDFName2 & Chr(34) & " cat output " & Chr(34) & Replace(PDFName1, ".pdf", "M.pdf" & Chr(34))


retVal = Shell("pdftk " & stMergeFiles, vbMaximizedFocus)
MsgBox "Please wait for pdftk 'BLACK SCREEN' to close and then press OK to continue." & vbCrLf & vbCrLf & "Note:Big files may take longer to merge. Please be patient", vbOKOnly + vbExclamation, "Files Merged"
DoEvents
...
...

About the error:
1- All of "MergeRFI", "stMergeFiles" and "retVal" are not define. Should I define them as string?
2- Should I replace "PDFName1", "PDFName2" with my pdf file name? Also where should I replace the pdf files that I want to merge them?
3- There is no black appear after the message "Please wait for pdftk 'BLACK SCREEN' to close and then press OK to continue......"
4- is it possible to add the page number in the right side of the footer and add a custom number (such as report number and print date) on the left side of the footer?
5- Do you mind to attach an MS Access example with dummy pdf files in zipped folder?

Your help is very appreciated
 
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:13
Joined
Sep 12, 2006
Messages
15,642
I have just installed this very helpful and useful tool. I was able to rotate a page, and combine a number of pages into a single document. Many thanks to all.
 

sxschech

Registered User.
Local time
Today, 04:13
Joined
Mar 2, 2010
Messages
792

morsy_soliman

I copied and pasted code to the forum, as an example, so may have left in some bits that aren't relevant. Let's see about more specifics for your situation.

1. Remove MergeRFI = "" as you don't need that.
2a. Dim them all as string
Dim stMergeFiles as String
Dim PDFName1 as String
...
Dim retVal As Variant


2b.The PDFName(s) are variables and you would pass in the actual names from a form, function, query or file dialogue picker
3. The black screen is the command prompt window that opens up. There may be a setting that you would need to make it full screen
(the reason for that at least in my case, I needed to combine it with a message box so that access code would not continue until the merging process completed. Having full screen helps the user know that it is "doing something" and when they no longer see the window, it is ok to click the button to continue. If you don't see a black screen it could also be due to the merge code not running since it didn't find the pdfnames to merge and thus the window would open/close so fast you wouldn't see it.
4. I believe you can do the page number customization, however, haven't done it myself, other than using what access itself provides in the reporting tools.
This may be a starting point:
5. I could provide an example, however, if you provide an example of the steps how you want it to work for you, I can see where to modify the code to hopefully better meet your need.

May not be able to get back to it right away depending on my workload.


gemma-the-husky

Glad you were able to make use of pdftk server
 

Users who are viewing this thread

Top Bottom