Add text to pdf file from vb

I need to add a line of text to several hundred pdf files in the same spot.

I have the code to open the files but I don't know how to add the text. Can somebody please advice?
Who is Participating?
Karl Heinz KremerCommented:
The following code snippet will add a text form field, will assign a string to this text field, and will flatten the document so that the interactive form field gets converted to "normal" PDF text:
        Dim App As CAcroApp
        Dim PDDoc As CAcroPDDoc
        Dim jso As Object
        Dim i As Long
        Dim FileName As String
        Dim field As Object
        Dim rect(3) As Integer

        FileName = "c:\test.pdf"

        Set App = CreateObject("AcroExch.App")
        Set PDDoc = CreateObject("AcroExch.PDDoc")

        If PDDoc.Open(FileName) Then
            Set jso = PDDoc.GetJSObject
            rect(0) = 100  ' x lower left
            rect(1) = 100  ' y lower left
            rect(2) = 300  ' x upper right
            rect(3) = 200  ' y upper right
            ' add a form field
            Set field = jso.addField("myFormField", "text", 0, rect)
            field.Value = "some dummy text"
            i = PDDoc.Save(PDSaveIncremental, FileName)
        End If

You already have the file open, so you can start by just getting the JSObject from the pdDoc. The rect describes the rectangle for your form field, and the 3rd parameter of addField specifies the page number you apply the field to. Acrobat uses a zero based page number.
Karl Heinz KremerCommented:
Do you have the full version of Acrobat? If so, which version?

This is possible by automating Acrobat. I would open the file, add a form field, assign the text you want to add to the form field, and then flatten the form field to make it "normal" PDF content.

Once I know which version of Acrobat you are using, I can give you some pointers. How are you opening the files?

However, if you want to use this on a server, you cannot use Acobat (the EULA does not allow this). There are 3rd party tools that you can use to do this (e.g. stamper applications). Let me know if you need a solution for a server environment, I can make some recommendations. I've used Appligent's StampPDF, but there are alternatives.
akimuraAuthor Commented:
I have adobe acrobat 5.0.
I won't use it in a server environment.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Karl Heinz KremerCommented:
How are you opening the files? You said that you have code. Is this VBA or JavaScript? Can you please either provide the code that you are using, or a description of how you are opening the files.
akimuraAuthor Commented:
This is how I open the files.

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal Hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton1_Click()
Dim acroApp As CAcroApp
Dim pdDoc As CAcroPDDoc
Dim avDoc As CAcroAVDoc
Dim pdTextSelect As CAcroPDTextSelect
Dim acroRect As CAcroRect
Dim intPage As Integer
Dim bFileOpen As Boolean
Dim bSelectSet As Boolean
Dim bShowSelect As Boolean

'Creating the objects
Set acroApp = CreateObject("AcroExch.App", "")
Set pdDoc = CreateObject("AcroExch.PDDoc", "")
Set avDoc = CreateObject("AcroExch.AVDoc", "")
Set acroRect = CreateObject("AcroExch.Rect", "")

'Setting the booleans
bFileOpen = False
bSelectSet = False
bShowSelect = False

'Show the Application

'Open the test document
' Insert while for looping through files
bFileOpen = avDoc.Open("c:\1159b.pdf", "test file")
'if the test file opened
If (bFileOpen) Then
    'Get the PDDoc
    Set pdDoc = avDoc.GetPDDoc
    'Set to the first page
    intPage = 0
    'Define the area
    acroRect.bottom = 0
    acroRect.Left = 0
    acroRect.Top = 150
    acroRect.Right = 150
   ' Add code for adding text in pdf file
    avDoc.Close (False)
End If

'clean up
Set pdTextSelect = Nothing
Set acroRect = Nothing
Set pdPage = Nothing
Set pdDoc = Nothing
Set avDoc = Nothing
Set acroApp = Nothing
End Sub
akimuraAuthor Commented:
Do I need to add a DLL or something?
It's not recognizing the addField method.
I get a mismatch error.

Karl Heinz KremerCommented:
It's possible that this does not work with Acrobat 5. Let me check the documentation.
Karl Heinz KremerCommented:
This should work with 5.0. The addField() command was added in 5.0 according to the 6.0 JavaScript manual (you can check for yourself, the JavaScript manual is part of the default installation of Acrobat 5.0, and you can find it under the Help menu).

Are you running 5.0.0 or a later version? You should be at least at 5.0.5.
akimuraAuthor Commented:
Oh, never mind. My compiler was doing something strange.
Thanks khkreme. I was a lot of help!
Helena DiazCommented:
Useful piece - my business partners last year encountered <code></code>to sign pdf . It's kind of uncomplicated to try and it's practical , I was notified that they might have a 7 day trial now
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.