Skip to main content
Participating Frequently
June 30, 2017
Answered

Using Excel VBA to add text to a PDF file

  • June 30, 2017
  • 2 replies
  • 27964 views

I am trying to open a PDF file with Excel 2013 by using VBA and then adding text to specific locations on the PDF.

I have Adobe Acrobat 9 Pro installed on a Windows 7 PC with Office 2013.

Thanks in advance.

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

@lrosenth is certainly right about Acrobat 9 no longer being supported by Adobe (neither is Acrobat X), but that does not mean that you are on your own. This forum is frequented mostly by users of Adobe's applications, and that means we don't care much about Adobe's support policy.

The good news is that whatever works with Acrobat DC and VBA should also work with Acrobat 9 (at least regarding the task you want to perform).

The bad news is that there is no functionality in the IAC (Interapplication Communication) API that would allow you to create static PDF content outright. If you want to do that, you need to use the Acrobat plug-in interface and create a plug-in (that requires C/C++). You can however create form fields and annotations (which are dynamic content), and then flatten this dynamic content to convert it to static PDF content. All this can be done using a combination of native IAC functionality and functionality from the JavaScript API that you access via the JSObject. See here for an example of how to use the  the JSObject:  Adobe Acrobat and VBA - An Introduction - KHKonsulting LLC

The only problem with this approach is that if your document already has some dynamic content, you would need to perform a selective flattening, and that complicates things quite a bit.

Take a look at these JavaScript methods:

Doc.addField() - Add a form field: Acrobat DC SDK Documentation

Doc.addAnnot() - Add an annotation: Acrobat DC SDK Documentation

Doc.flattenPage() - flatten one or more pages: Acrobat DC SDK Documentation 

2 replies

Karl Heinz  Kremer
Karl Heinz KremerCorrect answer
Adobe Expert
July 3, 2017

@lrosenth is certainly right about Acrobat 9 no longer being supported by Adobe (neither is Acrobat X), but that does not mean that you are on your own. This forum is frequented mostly by users of Adobe's applications, and that means we don't care much about Adobe's support policy.

The good news is that whatever works with Acrobat DC and VBA should also work with Acrobat 9 (at least regarding the task you want to perform).

The bad news is that there is no functionality in the IAC (Interapplication Communication) API that would allow you to create static PDF content outright. If you want to do that, you need to use the Acrobat plug-in interface and create a plug-in (that requires C/C++). You can however create form fields and annotations (which are dynamic content), and then flatten this dynamic content to convert it to static PDF content. All this can be done using a combination of native IAC functionality and functionality from the JavaScript API that you access via the JSObject. See here for an example of how to use the  the JSObject:  Adobe Acrobat and VBA - An Introduction - KHKonsulting LLC

The only problem with this approach is that if your document already has some dynamic content, you would need to perform a selective flattening, and that complicates things quite a bit.

Take a look at these JavaScript methods:

Doc.addField() - Add a form field: Acrobat DC SDK Documentation

Doc.addAnnot() - Add an annotation: Acrobat DC SDK Documentation

Doc.flattenPage() - flatten one or more pages: Acrobat DC SDK Documentation 

Participating Frequently
July 5, 2017

Karl,

Thank you very much. Your posting was most informative. I was able to successfully place text in multiple places on the PDF. I was even able place the same text on all pages of the PDF. However, I do have one more issue that I am not sure how to do. I need to add a Stamp on every page of the same PDF document. I only have one Stamp created but it is very large and I need not only to place a stamp on every page, I also need to scale the stamp down by 80% or so. Can you help me solve this issue? This is also done with VBA for Excel.

Karl Heinz  Kremer
Adobe Expert
July 7, 2017

The way I usually approach a problem like this is to solve it first in JavaScript. You can add stamps the same way you add any other annotation using the Doc.addAnnot() method (Acrobat DC SDK Documentation ) - you would first add the annotation, and then in a potential second step modify it's properties. When placing annotations, it's important to take any potential page rotation into account (see here for more information: https://acrobatusers.com/tutorials/auto_placement_annotations One of the annotation properties is the "rect", meaning the rectangle that outlines the annotation, you use that to specify it's size.

Once you've figured out how to do this in JavaScript, you can then work on translating the steps into VB/VBA using the JSObject.

lrosenth
Adobe Employee
Adobe Employee
June 30, 2017

Unfortunately, Adobe no longer supports Acrobat 9 (and hasn’t for many years now)

Participating Frequently
June 30, 2017

I am stuck using Acrobat 9 Pro. Does this mean it can't be done?

lrosenth
Adobe Employee
Adobe Employee
July 2, 2017

It just means that you are on your own – sorry.