Skip to main content

Notifications

Power Automate - Building Flows
Unanswered

Count number of pages of a PDF

(0) ShareShare
ReportReport
Posted on by

I built a flow to count the number of pages of a PDF using the expression below that I found in one of the forums:

 

sub(length(split(replace(base64ToString(outputs('Get_file_content')?['body']['$content']), 'Type/Pages', ''), 'Type/Page')),1)

 

It was working well until a while ago. Now the flow no longer counts the number of pages and neither is reading the PDF to include the DocuSign tabs. 

 

For context, I receive several documents from a supplier that I need to put the number of pages that each one has in an Excel and send for signature in the appropriate fields. Since January, the flow that I built in December and which was working very well, is no longer working and the only difference I noticed was that the PDF version that used to be 1.4 became 1.7.

 

Can anyone tell me why this is happening and how to resolve it?

 

I already tried to see some ways to convert these PDFs to 1.4 to see if it worked, but I couldn't find it.

  • Suggested answer
    CU28110220-0 Profile Picture
    CU28110220-0 2 on at
    Count number of pages of a PDF
    I was looking for a way to count pages in a PDF, and this tool is amazing! No downloads or complicated steps—just instant results. https://pdfwordcounter.io/pdf-page-counter/
  • Suggested answer
    HS-24091713-0 Profile Picture
    HS-24091713-0 2 on at
    Count number of pages of a PDF
    Below expression is not working for all PDF's, even for those pdf which have blank pages and not having Page No.s.
     
    sub(length(split(replace(base64ToString(outputs('Get_file_content')?['body']['$content']), 'Type/Pages', ''), 'Type/Page')),1)
  • MelodySJ Profile Picture
    MelodySJ 2 on at
    Re: Count number of pages of a PDF

    Hi @ThauanyMoraes ,

     

    I needed the same function and saw the post with this expression. Which did not work for me from the start. I saw your post and tried to dig into the expression a little. 

     

    I found that for my pdf documents there is a space between "Type" and "/Page"

     

    I adjsuted the expression and it works for me now: 

     

    sub(length(split(replace(base64ToString(outputs('Get_file_content')?['body']['$content']), 'Type /Pages', ''), 'Type /Page')),1)
     
    Hope it will continue to work for awhile... / power automate comes up with an out of the box solution >.<

     

    Regards,

    Sijing

  • Matthy79 Profile Picture
    Matthy79 4,178 on at
    Re: Count number of pages of a PDF

    Hello @DTV 

     

    I don't see an action "Extract Information from forms" at AI Builder. But just look at the output and search for information about what you are searching for.

  • DTV Profile Picture
    DTV 21 on at
    Re: Count number of pages of a PDF

    I have a flow that's using "Extract Information from forms" action of Ai builder to get the content of PDF documents - can I get the total page count from that itself ? Alternately if I add the "Recognize text in an image or a PDF document" step as you mentioned here, then will that increase the AI builder units consumed by my flow ?

     

    I did read somewhere to use "Prediction output - page count" or something like that but I don't see such a dynamic content available from the "Extract Information ..." mentioned above.

  • prathamesh26 Profile Picture
    prathamesh26 16 on at
    Re: Count number of pages of a PDF

    Well to be fair, I visited this thread last before your response and I hadn't seen your approach to it. Once I figured it out, I came back to respond with my solution. I never tested for whether it would work without having to pass the "result" attribute through a Parse JSON action, but my understanding at that time was that it would work with a Parse JSON action.

  • Matthy79 Profile Picture
    Matthy79 4,178 on at
    Re: Count number of pages of a PDF

    Isn't this exactly the same as I already suggested with an additional "Parse JSON" that is not needed?

  • prathamesh26 Profile Picture
    prathamesh26 16 on at
    Re: Count number of pages of a PDF

    Hi!

     

    I have figured out how to do this the smarter way. This solution works for all kinds of PDFs (scanned included) unlike other solutions listed here which work only for some but not all PDFs. 

     

    See flow below:

    prathamesh26_0-1699515901121.png

     

     

    Formula: length(body('Parse_JSON'))

     

    To get the schema for the Parse JSON action, build the flow up to the AI model action "Recognise text in an image or a PDF document". Run it and then go to the run history, copy the output of the "Results" property, come back to editing the flow and paste it in the "Generate from sample" section of the action.

     

    Voila! This works for all PDF types and returns the count of total pages in a PDF.

    Hope this helps! 

    prathamesh26_1-1699515900605.png

     

     

  • Matthy79 Profile Picture
    Matthy79 4,178 on at
    Re: Count number of pages of a PDF

    Hello @ThauanyMoraes,

     

    In case you haven't found a workaround already. You could also use "Recognize text in an image or a PDF document" and check for the length of the result.

  • AlexEncodian Profile Picture
    AlexEncodian 4,302 on at
    Re: Count number of pages of a PDF

    Hi @ThauanyMoraes 

    If the native solution is providing inconsistent results  for you, you can try Encodian's Get PDF Document Information action which will return the number of pages as well as other metadata (e.g. file size, author, creator, title, subject, keywords, page width and height, orientation, created/modified dates, PDF format and more).

    Hope that helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,445

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,741

Leaderboard