Solved Adding text to a PDF (2 Viewers)

Sun_Force

Active member
Local time
Today, 14:52
Joined
Aug 29, 2020
Messages
396
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, 01:52
Joined
Feb 19, 2002
Messages
42,981
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

AWF VIP
Local time
Yesterday, 22:52
Joined
Apr 13, 2010
Messages
1,401
Do a search, lots of info on this on the web:
Cheers,
Vlad
 

Sun_Force

Active member
Local time
Today, 14:52
Joined
Aug 29, 2020
Messages
396
@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

AWF VIP
Local time
Yesterday, 22:52
Joined
Apr 13, 2010
Messages
1,401
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

Active member
Local time
Today, 14:52
Joined
Aug 29, 2020
Messages
396
@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:
Local time
Today, 06:52
Joined
Sep 19, 2022
Messages
1
Hola,

He modificado PDSaveFull por un 1 y a funcionar, muchas gracias Sun_Force :)

He modificado para recorrer todas las hojas e indicar el mismo texto.

If AddToThisPage > PageCount Then
MsgBox "Can not write to page " & AddToThisPage
Exit Function
End If
Do While Not AddToThisPage = PageCount - 1
'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
AddToThisPage = AddToThisPage + 1
Loop


Rafael .:McPegasus:.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:52
Joined
Feb 19, 2002
Messages
42,981
Hola, Welcome aboard. Although this primarily an English language forum, you may find help if you post in a different language. But, it is better to post the text part of your question after running it through Google Translate. Then when you post code, please use the code tool to retain formatting.

And finally, in the future, please do not hijack an old thread. Please always start a new thread (not everyone will even look at an old thread with a lot of posts in it so you are less likely to get help). If you feel the old thread is relevant, just add a link.

Good luck.
 

Users who are viewing this thread

Top Bottom