How to add a text to PDF? (1 Viewer)

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
I've been searching for a way to add a text to a pdf file for more than a week. But unfortunately no luck to find any.
Everything that I've found, adds a form and then a textbox and fills the textbox. I don't want to add a form to pdf. I need to add a text.
  • I don't need a form. I need to add a normal text to pdf
  • I know acrobat uses javascript for automating. I can run a javascript from vba. So even javascript code satisfies me.
  • I have Acrobat Pro.

I really appreciate any insight on this.
Thank you.
 
Last edited:

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
Hi. Are you saying your PDF file is not a fillable PDF form?
yes, it's not a fillable pdf form.
It's a scanned page.
Adding a form and then a textbox with JavaScript works. (both from Access and Adobe). But I don't want this method.
Editing the file and adding a text manually works too. I just need to automate adding this text.

Thanks for taking your time and trying to help.
 
Last edited:

AngelSpeaks

Active member
Local time
Yesterday, 21:13
Joined
Oct 21, 2021
Messages
252
One method i tried but it's a pain to line up the controls was to convert the scanned page to an image file and then use that as a background image. I also tried adding the image to a report and placing the controls on top of that. It was difficult to line up. I ended up creating a report with all the pdf info on it and then printed that report to a pdf.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:13
Joined
Feb 28, 2001
Messages
22,792
If you cannot find a .DLL file that exposes the contents of a PDF then you need something like the full Adobe Acrobat package. Or you need to rebuild the original data and supplement it, then write the result as a .PDF file... as described by AngelSpeaks.
 

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
One method i tried but it's a pain to line up the controls was to convert the scanned page to an image file and then use that as a background image. I also tried adding the image to a report and placing the controls on top of that. It was difficult to line up. I ended up creating a report with all the pdf info on it and then printed that report to a pdf.
I really don't understand what all those you kindly explained mean.
At present I manually open a pdf, click edit button, add a text at top left corner, save and close it. I'm just trying to do it with a button on a form.
Is it possible with your solution? If yes, can you direct me to a page that explains how to achieve it?

Note : The PDFs are engineering drawings. So, they can't be recreated in reports. And there's no need to line up anything. I only need to add a simple text above them.

Thanks for trying to help.
 

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
If you cannot find a .DLL file that exposes the contents of a PDF then you need something like the full Adobe Acrobat package.
I don't know why I need a DLL. I'm simply trying to add a text to a pre-saved pdf file. Is the full adobe acrobat package your suggesting different with purchased Adobe Acrobat Pro?

Or you need to rebuild the original data and supplement it, then write the result as a .PDF file... as described by AngelSpeaks.
The original data is a drawing. Can not be rebuilt. Sorry. I had to explained it first.

At present I can click a button on a form. The on-click event of the button, opens a pdf, adds a form to the pdf, then adds a textbox, fills the textbox, save and close it.
I only want to add a text. Not a textbox.

If necessary, I can show the code.

Thanks for trying to help.
 

sonic8

AWF VIP
Local time
Today, 04:13
Joined
Oct 27, 2015
Messages
593
At present I can click a button on a form. The on-click event of the button, opens a pdf, adds a form to the pdf, then adds a textbox, fills the textbox, save and close it.
I only want to add a text. Not a textbox.
Are you using the Adobe JavaScript API to do this?
If yes, then check out the Doc.addAnnot method of that API. I believe, it should do what you want.
 

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
Are you using the Adobe JavaScript API to do this?
@sonic8 thanks for helping.

If I can understand you correctly no, I don't use API. I create an object of Acrobat, Open a file, create a JS code and runs it.
It's all done with pure VBA.

Here's the code if you want to see it.

Code:
Public Sub AddFieldToPdf(ThisPDF As String, InsertThis As String)
       
    Dim App As Object
    Dim AVDoc As Object
    Dim AForm As Object
    Dim js As String

    Set App = CreateObject("Acroexch.app")
    Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set AForm = CreateObject("AFormAut.App")

    If AVDoc.Open(ThisPDF, "") Then
        js = "f = this.addField(""OrderPK" & """, ""text"", 0, [20,10,250,50]);" & vbLf _
        & "f.Value = """ & InsertThis & """; " & vbLf _
        & "f.textColor = color.Black;" & vbLf _
        & "f.textFont = Font.ZapfD;"
        '//execute the js code
        AForm.Fields.ExecuteThisJavaScript js
        App.MenuItemExecute ("Save")
    End If

End Sub

If changing to API will solve this and makes me able to add a text instead of a textbox, I'll do it. Just show me where I can find a sample code or how to starts.

thanks again.
 

sonic8

AWF VIP
Local time
Today, 04:13
Joined
Oct 27, 2015
Messages
593
Please note, I've got no experience with automating Adobe Acrobat!

If I can understand you correctly no, I don't use API. I create an object of Acrobat, Open a file, create a JS code and runs it.
It's all done with pure VBA.
The "create a JS code and runs it."-part sounds to me like you would use the JavaScript API and your code example also looks that way.
The JsObject of the AcroExch.AVDoc should also expose most, if not all, of the required methods directly to VBA.

Just show me where I can find a sample code or how to starts.
In the documentation. ;-)
Interapplication Communication with the Acrobat SDK
-> Page 31 - Working with annotations
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:13
Joined
Feb 28, 2001
Messages
22,792
Just to clarify, what Sonic8 suggested is the JS equivalent to using a .DLL in VBA. The current methods used here involve a code bundle that allows you to "get inside the head" of a particular file format. When you open an Excel Application Object from Access, you are using VBA to get into Excel via the Excel libraries. When you open a Word document through Access, you are using the Word libraries. These libraries are most often in the form of .DLL (Dynamically Linked Library) files. For JS, you use a different code bundle, one that Sonic8 suggested. But it is the same concept. You use one of these bundles to find routines that do what you want. If you have an Acrobat SDK, you probably have the Adobe bundle for getting into PDF files.

The thing about .PDF files is that they are built in a way such that if the file isn't set up to be modified (i.e. not set up like a fillable form) then you cannot open the file because you don't have a utility that understands the internals of .PDF files well enough to muck about in them. Just like Access cannot directly open a .DOCX file and Word cannot directly open a .XLSL file.

Sonic8's point and my point simply is that to Access, a .PDF is foreign unless you have one of those code bundles to allow Access to understand the format. Or that lets JS scripts to understand the format. If you have the required code bundle, you are as good as gold in your quest. If not, then it gets trickier. That's why I mentioned the .DLL files.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:13
Joined
Oct 29, 2018
Messages
18,972
@sonic8 thanks for helping.

If I can understand you correctly no, I don't use API. I create an object of Acrobat, Open a file, create a JS code and runs it.
It's all done with pure VBA.

Here's the code if you want to see it.

Code:
Public Sub AddFieldToPdf(ThisPDF As String, InsertThis As String)
       
    Dim App As Object
    Dim AVDoc As Object
    Dim AForm As Object
    Dim js As String

    Set App = CreateObject("Acroexch.app")
    Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set AForm = CreateObject("AFormAut.App")

    If AVDoc.Open(ThisPDF, "") Then
        js = "f = this.addField(""OrderPK" & """, ""text"", 0, [20,10,250,50]);" & vbLf _
        & "f.Value = """ & InsertThis & """; " & vbLf _
        & "f.textColor = color.Black;" & vbLf _
        & "f.textFont = Font.ZapfD;"
        '//execute the js code
        AForm.Fields.ExecuteThisJavaScript js
        App.MenuItemExecute ("Save")
    End If

End Sub

If changing to API will solve this and makes me able to add a text instead of a textbox, I'll do it. Just show me where I can find a sample code or how to starts.

thanks again.
Hi. Just curious, when you add the text to a pdf using the manual method, what are the steps you're taking. I imagine there are many ways to add text to a pdf, I just want to make sure I do it the way you want it done.
 

sxschech

Registered User.
Local time
Yesterday, 19:13
Joined
Mar 2, 2010
Messages
746
Couple other ideas, if it is a set of steps that are the same, you could try Autohotkeys
Another is pdftkserver and or stamptk which you could call from a button using vba.
 

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
when you add the text to a pdf using the manual method, what are the steps you're taking.

Sorry, I made this using a remote desktop. So the quality is not good. But I think you'll see how I do it.

2022-05-20_00-02-40.gif
 

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
Couple other ideas, if it is a set of steps that are the same, you could try Autohotkeys
Another is pdftkserver and or stamptk which you could call from a button using vba.
It seems promising. I'll give a try as soon as I'm back to office and will let you know the result
thanks for your help and the advice.
 

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
If you have an Acrobat SDK, you probably have the Adobe bundle for getting into PDF files.
I downloaded the SDK, but it's not helping at all. Too complicated. with 0 reference to VBA. I'm still looking into it to see if I can find how to do it in JS. Once I know how to automate it in JS then the problem is solved. I've mailed the help desk. No reply at all.

The thing about .PDF files is that they are built in a way such that if the file isn't set up to be modified (i.e. not set up like a fillable form) then you cannot open the file because you don't have a utility that understands the internals of .PDF files well enough to muck about in them.
I'm not sure if I can understand this. I'm able to use Access vba to add forms, textboxes, or any other object to a pdf. I'm also able to add page, delete page, import page or export page. I believe if VBA can do this, there should be a way to add a text too.

this code deletes pages from pdf :
This is all in vba. I haven't used JS at all.

Code:
Public Function DeletePages_FromPDF(ThisPDF As String, _
                                    DeleteFromAs Integer, _
                                    DeleteToAs Integer ) As String

    Dim PDDocSource As Object
    Set PDDocSource = CreateObject("AcroExch.PDDoc")

    If PDDocSource.Open(ThisPDF)  Then
        If PDDocSource.GetNumPages> 1 Then
            PDDocSource.deletepages(DeleteFrom, DeleteTo)
        End If
    End If

End Function
 
Last edited:

KitaYama

Active member
Local time
Today, 11:13
Joined
Jan 6, 2022
Messages
431
I imagine there are many ways to add text to a pdf, I just want to make sure I do it the way you want it done.
@theDBguy It really doesn't matter which method I want. Any possible way, as long as it adds a text and not a textbox (of a form) and I can control the font size, it suits me fine.

thanks again.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 21:13
Joined
Feb 28, 2001
Messages
22,792
I'm not sure if I can understand this. I'm able to use Access vba to add forms, textboxes, or any other object to a pdf. I'm also able to add page, delete page, import page or export page. I believe if VBA can do this, there should be a way to add a text too.

This line tells me you already have the required library.

Code:
Set PDDocSource = CreateObject("AcroExch.PDDoc")

That is an example of late library binding. That is why you can do what you describe. You are doing more or less what Sonic8 and I were talking about, using a special library that understands .PDF files, but you just didn't realize it.
 

Users who are viewing this thread

Top Bottom