Wednesday, August 24, 2016

Count the Number of Words using JavaScript in Adobe Acrobat

This is the best hack that I've come across in the last several years.

Many times I wanted to count the number of words in a PDF document. The normal way to do this task is to save the file as a Word doc and then open it in MS Word that calculates the count. You can also upload your file to an online site that will count it (although most companies would probably frown upon sending your precious data to some random 3rd party site).

Today I stumbled across an Adobe forum thread that said that Acrobat can run JavaScript. Hmmm... And then I found another site that explains how to create a custom tool in Acrobat. Put these two together and you get a toolbar button in Adobe Acrobat to count the number of words in the doc.

Here's how to make it work. Note that this is for Acrobat DC.
  1. Open Acrobat.
  2. Click Tools.
  3. Click Action Wizard.
  4. In the button list under the toolbar, click New Custom Command.
    This window opens:

  5. Click Execute JavaScript.
  6. Change the Label to something useful, like "Count words in document".
  7. Change the Tool tip to something useful, like "Counts the number of words in the current document".
  8. Click Command Options...
  9. Paste in the following code:
    /* Count number of words in document */
    var cnt = 0;
    for (var p = 0; p < this.numPages; p++) cnt += getPageNumWords(p);
    app.alert("There are " + cnt + " words in this file.");
  10. Click OK.
  11. Click Don't show options, options are always the same.
  12. Click OK.
You now have a custom command. All that's left to do is make the button that calls it.
  1. Right-click in a blank part of the toolbar.
  2. Click Customize Quick Tools.
  3. Expand the Action Wizard section.
  4. Find your new command and select it.
  5. Click the Add to Toolbar button on the right side of the window.
  6. Click Save.
That's it! You can now open any PDF and just click the button to get a word count.

[Source: KHKonsulting and Adobe]

12 comments:

  1. Not bad, but if you just want a subsection of the document, word really shines. But thanks for trying. Now I have to extract the pages I want to check and then run the counter. Similar hassle.

    ReplyDelete
  2. Word is easily better than Acrobat at counting words. But, the issue that I regularly have is to find out how many words are in a PDF, such as for submitting a document for translation when I don't have any other source (like Word). I can't recall ever having to count words in a section of a doc.

    I took a quick look and there doesn't seem to be a way to grab a selection using JavaScript in a PDF doc :( If you find one, let me know and I'll update the post.

    ReplyDelete
  3. Thank you! Works perfectly :-) Tommy of Finland

    ReplyDelete
  4. Great blog. All posts have something to learn. Your work is very good and i appreciate you and hopping for some more informative posts.

    Word Counter

    ReplyDelete
  5. Blogger’s brilliant Efforts are really appreciable keep sharing word count tool

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. Thanks for sharing this news ! Nice Blog It is full of creative information.
    Word Count Software



    ReplyDelete
    Replies

    1. /* Count number of words in document */
      var cnt = 0;
      for (var p = 0; p < this.numPages; p++) cnt += getPageNumWords(p);
      app.alert("There are " + cnt + " words in this file.");
      ReferenceError: cnt is not defined
      1:Console:Exec
      undefined
      -------------------
      Please help!

      Delete
    2. Hi. At my current job I don't have Adobe Acrobat, so I don't have any way to check if it still works. Sorry!

      Delete
  8. Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any suggestions? Many thanks! read

    ReplyDelete
    Replies
    1. Thanks for the feedback. As for tips... it depends what type of writing you want to do. I am a technical writer, so I need to know about technology, tools, and writing in a style that conveys information succinctly and accurately.
      Try and find Slack channels (such as Docs as Code). And remember to keep learning, whether you learn about new technology or learn about new tools, just keep learning.

      Delete