copy

Wednesday, 31 July 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 Files\Adobe\Acrobat xx.0\Acrobat\acrobat.tlb" – if you have 32bit operating system, or on "C:\Program Files (x86)\Adobe\Acrobat xx.0\Acrobat\acrobat.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).

Did you like this post? If yes, then share it with your friends. Thank you!


Categories:


Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: e-mail, Facebook, Twitter, Google+ and Linkedin. More info