Solved Adding text to a PDF (1 Viewer)

Sun_Force

Member
Local time
Tomorrow, 05:57
Joined
Aug 29, 2020
Messages
191
Note : Adobe Acrobat Pro is already installed.

Is there any way to add a text to a pdf file with VBA in Access or Excel?
I have several hundred pdf files and I have to open them one by one, add a text in large and bold fonts and save it. The text is the result of a DLookUP.
Is it possible to do this procedure with VBA?


thanks.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:57
Joined
Feb 19, 2002
Messages
31,764
To edit a PDF, you need to do it through Adobe Acrobat. If there is an API that supports VBA, you can use OLE automation but you'll have to check the Acrobat documentation. I don't own it so I don't know what editing features are available.
 

bastanu

Active member
Local time
Today, 13:57
Joined
Apr 13, 2010
Messages
755
Do a search, lots of info on this on the web:
Cheers,
Vlad
 

Sun_Force

Member
Local time
Tomorrow, 05:57
Joined
Aug 29, 2020
Messages
191
@bastanu

Thanks for taking your time and replying.
I've already seen those pages.
The first one from adobe forum, goes back to 2013 (8 years ago) and it seems that a lot of things have been changed since then. I receive several object doesn't support this method errors.
The second one from Stackoverflow, actually works if there's a form in the pdf file. My documents are scanned pdf files.
The last one works, but as it's explained under the code, the newly saved file by this method is 15 times larger in size.

I'm now working on adding a form first and then use the second method you suggested.
Million thanks for your time.
 

bastanu

Active member
Local time
Today, 13:57
Joined
Apr 13, 2010
Messages
755
Regarding the last one that increases the size try to find some more info on it as the guy did manage to do it in a slightly different way with no major increase in size.
EDIT: the reference to the solution is actually in the same thread at the very bottom.
Cheers,
Vlad
 

Sun_Force

Member
Local time
Tomorrow, 05:57
Joined
Aug 29, 2020
Messages
191
@bastanu

thanks again for your input.
The author says he managed to preserve the source file size if he manually save the document.
Yes, I just edited and saved the PDF manually and the original size was kept

Anyway, I ended up with the following code to
1- open the document
2- add a text field
3- fill the text box with a string.

( My function is much more complicated. To simplify things I cut off unnecessary parts. I hope I've not cut too much and necessary parts.)

SQL:
Public Function AddFieldToPdf(EditThisPDF As String, _
                          InsertText As String, _
                          Optional SaveAsPDF As String = "", _
                          Optional AddToThisPage As Integer = 0) As Boolean
          
    Dim OpenPath As String
    Dim SavePath As String
    Dim acroApp As Object
    Dim gpdDoc As Object
    Dim jso As Object
    Dim PageCount As Integer
    Dim Box1
  
    AddFieldToPdf = False
  
    ' if SaveAs is missed, over write the pdf
    If SaveAsPDF = "" Then
        SaveAsPDF = EditThisPDF
    End If
  
    'initialize the Acrobat interface
    Set acroApp = CreateObject("AcroExch.App")
    Set gpdDoc = CreateObject("AcroExch.PDDoc")
  
    'open the file
    If gpdDoc.Open(EditThisPDF) Then
        Set jso = gpdDoc.GetJSObject
    End If
  
    'get at the jso
    If Not jso Is Nothing Then
        Set jso = gpdDoc.GetJSObject
        PageCount = gpdDoc.GetNumPages - 1
        If AddToThisPage > PageCount Then
            messagebox "Can not write to page " & AddToThisPage
            GoTo ExitFunction
        End If
        'Followng array is upperleft x, upper-left y, lower-right x and lower-right y.       
        Set Box1 = jso.AddField("Any_FiledName_You_Like", "text", AddToThisPage, Array(20, 10, 250, 50))
        Box1.TextFont = "Helvetica"
        Box1.TextSize = 24
        Box1.value = InsertText
    End If
  
    If gpdDoc.Save(PDSaveFull, SavePath) = True Then
        AddFieldToPdf = True
    End If

End Function
 
Last edited:

Users who are viewing this thread

Top Bottom