Add text to pdf file from vb

Posted on 2005-05-07
Medium Priority
Last Modified: 2016-09-24
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?
Question by:akimura
  • 5
  • 4
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 13952215
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.

Author Comment

ID: 13962005
I have adobe acrobat 5.0.
I won't use it in a server environment.
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 13962480
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 13962840
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
LVL 44

Accepted Solution

Karl Heinz Kremer earned 2000 total points
ID: 13971818
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.

Author Comment

ID: 13992337
Do I need to add a DLL or something?
It's not recognizing the addField method.
I get a mismatch error.

LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 13992375
It's possible that this does not work with Acrobat 5. Let me check the documentation.
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 13992389
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.

Author Comment

ID: 13997200
Oh, never mind. My compiler was doing something strange.
Thanks khkreme. I was a lot of help!

Expert Comment

by:Helena Diaz
ID: 41814350
Useful piece - my business partners last year encountered <code>https://goo.gl/zuC8Ux</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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ability to edit PDF documents can be useful, however it may not be a straight forward process. Many non-technical people don't realise that a PDF document is basically an image rather than a text file, even if it contains nothing but text. If…
This article focuses on how to remove password security from multiple PDF files by Adobe Acrobat program. Sometimes it is essential to access the stored data items and to print, edit as well as copy content from Portable Document Format files in abs…
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question