Add text to pdf file from vb

Posted on 2005-05-07
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
    LVL 44

    Expert Comment

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

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

    Expert Comment

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

    Author Comment

    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

    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

    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
    It's possible that this does not work with Acrobat 5. Let me check the documentation.
    LVL 44

    Expert Comment

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

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

    Expert Comment

    by:Helena Diaz
    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

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    In a previous article published here at Experts Exchange, Signature Image with Transparent Background (, I explained how to cre…
    *Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
    In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…
    In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now