Get the Number of Pages in a PDF Document using Power Automate Desktop
    • Dark
      Light
    • PDF

    Get the Number of Pages in a PDF Document using Power Automate Desktop

    • Dark
      Light
    • PDF

    Article Summary

    I was recently asked if it was possible to get the number of pages in a PDF document. In this case the person had a Power Automate Desktop (PAD) license so using PAD was an option. Other options include using the Adobe PDF Tools Connector, but that would require an additional subscription to the Adobe service.

    The use case involved receiving a PDF via email and having it parsed using AI Builder. However, for some transactions, people were combining multiple transactions within the same file. Typically, the PDF has a fixed number of pages in it. So, if there were more than that number, we know that this needs to be handed off to a human. I didn’t immediately know how to solve this problem, but a thought about how I would solve this problem without tools and then tried to emulate those steps in PAD.

    Pre-requisites

    In addition to having access to Power Automate Desktop, I set my default PDF reader to be Microsoft Edge. You may be able to solve this using a similar approach with other PDF readers but I am using Microsoft Edge in this case.

    Approach

    As mentioned earlier, our approach will be to retrieve the number of pages by automating what would normally be manual steps. These means that we will:

    • Open our PDF file (using a CMD session).

    • We will then scroll to the bottom of the page by using short cut keys (Shift + End)

    • When we navigate to the end of the document, we will see our of value increments. There happens to be a short cut key of CTRL + ALT + G that will get us into this field where we can retrieve this value by placing it in our clipboard using another shortcut key of CTRL + C
      1-pagetotal

    • We can then use the Get clipboard text action to retrieve this value and store it as an output that can be sent back to a cloud flow.

    • Our last step is to then close our web browser using another short cut key of ALT + F4

    Our complete script is as follows.

    Note, I have included a couple short Wait statements just to ensure the script doesn’t try to move too quickly before the PDF document actually responds. I have also included a message box that is useful for testing.
    2-script

    Testing

    We can test our script locally or we can wire it up to a cloud flow. For the purpose of this post, we will just use local debugging.
    3-testing

    Conclusion

    Building successful RPA solutions is all about having sound strategies. Often, there are many ways to solve a particular problem and certain vintages of an application will likely dictate how best to solve the problem. But, when in doubt, revert to how you would solve this problem manually and then find ways to automate it.


    Was this article helpful?