Count The Pages, Lines And Words Of A PDF File Using VBA

Share this

July 31, 2013

PDF Pages, Lines & Words

Introduction


I have received many requests from blog readers regarding Adobe Professional. One of these requests was a complaint about Adobe Professional’s inability to calculate the number of words and lines of an opened PDF document.

I worked a little bit on this request and I ended up with the workbook that you will find in the download section below. In the particular workbook you can select a PDF file and the code will loop through all the pages of the document and will count the number of lines, as well as the number of words that are included in each page. Finally, it will calculate the total pages, lines and words that are contained in the entire document.

The VBA code opens the PDF file, reads the data page by page and uses a custom VBA function to split the data in lines and in words. Of course the results are not 100% accurate – especially if there are many tables, but, still, the code gives a good approximation.


Prerequisites


It should be noted that the code works ONLY with Adobe Professional. In particular, there is a library check routine at the open event of the workbook. If there is no reference to Adobe library the routine will try to add one; the required library path should be either on “C:Program FilesAdobeAcrobat xx.0Acrobatacrobat.tlb” – if you have 32bit operating system, or on “C:Program Files (x86)AdobeAcrobat xx.0Acrobatacrobat.tlb” – if you have 64bit operating system, where xx is the version of your Adobe Professional (the latest version is 11.0). If for some reason you try to use this workbook with Adobe Reader, you will receive an error message and the workbook will close.

Moreover, due to the library check you should have enabled the option “Trust access to VBA project object model”. How you will do this? For Office 2013/2010/2007 go to Files → Options → Trust Center Tab → press the Trust Center Settings button → Macro Settings tab and → check the Trust access to VBA project object model checkbox. For Office 2003 go to Tools → Macro → Security → select the Trusted Publishers tab and → check the Trust access to Visual Basic project checkbox.


Demonstration video


The short video below demonstrates the use of the workbook.


Download it from here


The zip file contains two workbooks, one that can be opened with Excel 2013/2010/2007 and another one that can be opened with the older Excel 2003. Please remember to enable macros and to “Trust access to VBA project object model” (see instructions above).

Page last modified: January 6, 2019

Christos Samaras

Hi, I am Christos, a Mechanical Engineer by profession (Ph.D.) and a Software Developer by obsession (10+ years of experience)! I founded this site back in 2011 intending to provide solutions to various engineering and programming problems.

Christos E. Samaras

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Add Content Block
>